Tomcat+Nginx cluster and load balancing
Please indicate the source for reprinting: http://blog.csdn.net/l1028386804/article/details/48272857
Today we will work with you to implement clustering and load balancing of Tomcat and nginx
1. Tomcat cluster configuration Port planning
Note: This port is used to start two tomcats on the same machine. You need to modify the port so that it is not repeated. If a machine only deploys one tomcat, its port can be the same.
Serial number |
SHUTDOWN |
AJP |
http |
Cluster Receiver |
1 |
8005 |
8009 |
8080 |
5001 |
2 |
8015 |
8019 |
8081 |
5002 |
1. Tomcat cluster configuration
Modify in server.xml:
will:<Engine name="Catalina" defaultHost="localhost">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
where jvmRoute="jvm1" has different names in different tomcats.
Add the following content to the
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOpti <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessi notifyListeners/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" <!--可写你自己的ip地址--> port="5001" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
Note that on the same machine: Receiver node port: port="5001"or else Same.
2. Modify tomcat’s web.xml
You need to modify tomcat’s web.xml configuration parameters to truly realize the setting of session synchronization replication
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <distributable /> (在倒数第二行增加这个代码才能实现session同步复制功能) </web-app>
2. Optimization:
1. Memory optimization configuration
1) Windows:
set JAVA_OPTS = " -server -Xms512M -Xmx2048M -XX:MaxNewSize=256M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Djava.awt.headless=true"
2) Linux
JAVA_OPTS = " -server -Xms512M -Xmx2048M -XX:MaxNewSize=256M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Djava.awt.headless=true"
2. Other optimized configurations:
1) Add the following configuration 1
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="50" />
<Connector port="8080" protocol="HTTP/1.1" c redirectPort="8443" />
<Connector port="8080" executor="tomcatThreadPool" protocol="HTTP/1.1" c enableLookups="false" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8009" executor="tomcatThreadPool" c enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
2) or
to modify
<Connector port="8080" protocol="HTTP/1.1" c redirectPort="8443" />
<Connector port="8080" protocol="HTTP/1.1" maxThreads="500" acceptCount="50" c enableLookups="false" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8009" maxThreads="500" c enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
Note: The main difference between the two is: maxThreads="500" is ignored when the executor is enabled and associated.
3) Several attribute explanations:
- maxThreads
Tomcat uses threads to handle each request received. This value represents the maximum number of threads that Tomcat can create. Default value is 200. It can be adjusted according to the machine's period performance and memory size, generally between 400-500. The maximum can be around 800.
- acceptCount
Specifies the number of requests that can be placed in the processing queue when all available threads for processing requests are used. Requests exceeding this number will not be processed. Default value is 10.
- minSpareThreads
The number of threads created when Tomcat is initialized. Default value is 4.
- connnectionTimeout
Network connection timeout, default value 20000, unit: milliseconds. Setting it to 0 means it will never time out, which is a dangerous setting. Usually it can be set to 30000 milliseconds. (Due to the long timeout of the interface with the backend system, this system is set to 60000)
- enableLookups
Whether to reverse-check the domain name, the default value is true. To increase processing power, it should be set to false
- Executor node
represents a thread pool shared between Tomcat components.
- maxIdleTime
The number of milliseconds before an idle thread is shut down. The default value is 60000.
3. Nginx + Tomcat load balancing configuration
Add the following code in the configuration file /usr/local/nginx/conf/nginx.conf file
upstream tomcat { server 192.168.100.50:8080 weight=1; server 192.168.100.50:8081 weight=1; }
Modify the location in the Server node:
location / { root html; index index.html index.htm; }
location / { #root html; #index index.html index.htm; proxy_pass http://tomcat6; }
Note that the tomcat name in http://tomcat is the same as the above configuration.
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the Tomcat+Nginx cluster and load balancing, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To deploy a JAR project to Tomcat, follow these steps: Download and unzip Tomcat. Configure the server.xml file, set the port and project deployment path. Copies the JAR file to the specified deployment path. Start Tomcat. Access the deployed project using the provided URL.

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

To deploy multiple projects through Tomcat, you need to create a webapp directory for each project and then: Automatic deployment: Place the webapp directory in Tomcat's webapps directory. Manual deployment: Manually deploy the project in Tomcat's manager application. Once the project is deployed, it can be accessed by its deployment name, for example: http://localhost:8080/project1.

Tomcat installation directory: Default path: Windows: C:\Program Files\Apache Software Foundation\Tomcat 9.0macOS:/Library/Tomcat/Tomcat 9.0Linux:/opt/tomcat/tomcat9 Custom path: You can specify it during installation. Find the installation directory: use whereis or locate command.

How to check the number of concurrent Tomcat connections: Visit the Tomcat Manager page (http://localhost:8080/manager/html) and enter your user name and password. Click Status->Sessions in the left navigation bar to see the number of concurrent connections at the top of the page.

The Tomcat website root directory is located in Tomcat's webapps subdirectory and is used to store web application files, static resources, and the WEB-INF directory; it can be found by looking for the docBase attribute in the Tomcat configuration file.

The Tomcat port number can be viewed by checking the port attribute of the <Connector> element in the server.xml file. Visit the Tomcat management interface (http://localhost:8080/manager/html) and view the "Status" tab. Run "catalina.sh version" from the command line and look at the "Port:" line.

Running projects with different port numbers on the Tomcat server requires the following steps: Modify the server.xml file and add a Connector element to define the port number. Add a Context element to define the application associated with the port number. Create a WAR file and deploy it to the corresponding directory (webapps or webapps/ROOT). Restart Tomcat to apply changes.
