Home > Backend Development > PHP Tutorial > Tomcat+Nginx cluster and load balancing

Tomcat+Nginx cluster and load balancing

WBOY
Release: 2016-07-29 09:16:01
Original
1100 people have browsed it

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">
Copy after login
is modified to:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">  
Copy after login

where jvmRoute="jvm1" has different names in different tomcats.

Add the following content to the node to implement Session replication and sharing:

<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>    
Copy after login

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>
Copy after login

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"  
Copy after login
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" 
Copy after login

2. Other optimized configurations:

1) Add the following configuration 1
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="500" minSpareThreads="50" />
Copy after login
to modify
<Connector port="8080" protocol="HTTP/1.1" c
               redirectPort="8443" />
Copy after login
to
<Connector port="8080"  executor="tomcatThreadPool" protocol="HTTP/1.1"
               c
               enableLookups="false" redirectPort="8443" />
Copy after login
:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Copy after login
Copy after login
to
<Connector port="8009" executor="tomcatThreadPool"
               c
               enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
Copy after login
2) or

to modify

<Connector port="8080" protocol="HTTP/1.1"
               c
               redirectPort="8443" />
Copy after login
is
<Connector port="8080"  protocol="HTTP/1.1"
               maxThreads="500"
               acceptCount="50" c
               enableLookups="false" redirectPort="8443" />
Copy after login
modified
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Copy after login
Copy after login
is
<Connector port="8009"
         maxThreads="500"  c
         enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
Copy after login

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;
  }
Copy after login
The tomcat name is arbitrary, pay attention to the IP address and port.
Modify the location in the Server node:
location / {
        root   html;
        index  index.html index.htm;
    }
Copy after login
is
location  / {
   #root   html;
   #index  index.html index.htm;
           proxy_pass  http://tomcat6;
}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template