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 |
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.
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:
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"
JAVA_OPTS = " -server -Xms512M -Xmx2048M -XX:MaxNewSize=256M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Djava.awt.headless=true"
<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" />
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.
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.
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.
The number of threads created when Tomcat is initialized. Default value is 4.
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)
Whether to reverse-check the domain name, the default value is true. To increase processing power, it should be set to false
represents a thread pool shared between Tomcat components.
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; }
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.