Attention to these points, easy to configure Nginx + Tomcat's clustering and load balancing

Tomcat cluster is reached when a single server performance bottleneck, an effective means to improve overall system performance by laterally extensible manner. Nginx is a high-performance HTTP server and reverse proxy web server, Tomcat cluster load balancing can be achieved by a simple configuration.

Tomcat version 8.5.35, as used herein, Nginx version is 1.14.2. Next, look at the configuration of the process and the problems you might encounter, starting in the micro-channel public number "epiphany source."

1 Overview

For Web applications, the biggest problem is the cluster Session information sharing, generally have the following solutions:

  • Using sticky session , for example, using IP Hash load balancing policy, the current user requests are concentrated on a single server; disadvantage single point of failure, session loss
  • Use Session replication using Tomcat comes Session replication strategy session information synchronized to each node in the cluster; drawback is the cost of memory and bandwidth for small clusters
  • Using third-party caching middleware caches the entire cluster session information, such as Redis cache associated with the Session by the application control, you can also adapted Tomcat
  • Of course, the session information can be saved to a shared file system or database

In the process of Nginx configuration, you may experience the following problems:

  • You can not use the underscore when configuring the upstream name, such as tomcat_ha, or Tomcat will throw The character [_] is never valid in a domain name abnormalities
  • Nginx.exe kill all processes on windows,taskkill /fi "imagename eq nginx.exe" /f
  • There is a system process pid 4 will occupy 80 ports on the windows, so there will nginx replaced by 8000

Tomcat cluster in the configuration process, the issues that need attention:

  • Web.xml configuration to ensure that the <distributable /> element
  • Ensure the Context Manager Do not be replaced with standard Session Manager
  • Receiver.address not configured to auto, because the default may be bound 127.0.0.1; Receiver.port can be changed or not changed, Tomcat available port itself detects the range of 4000-4100, the automatic conflict
  • If on a different server, you need to turn off the firewall or open ports , as well as time synchronization

2. Nginx core configuration

Nginx using the default configuration, add and modify the core configuration is as follows:

http {
  ...
  #gzip  on;
  
  #设置负载均衡的服务器列表和权重
  upstream tomcat-ha {
      #ip_hash; 
      server 172.31.1.41:8080 weight=1;
      server 172.31.1.42:8080 weight=1;
  }
  
  server {
      listen       8000;
      server_name  localhost;

      #charset koi8-r;
      #access_log  logs/host.access.log  main;

      location / {
          root   html;
          index  index.html index.htm;
          #转发请求
          proxy_pass http://tomcat-ha;
          proxy_set_header Host $http_host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
      ...
  }
}

3. Tomcat cluster configuration

Enabling cluster configuration, add the following configuration at the <Engine> element:

<!-- channelSendOptions=6 同步复制 -->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
  <!-- 集群 Session 管理器 -->
  <Manager className="org.apache.catalina.ha.session.BackupManager"
             expireSessionsOnShutdown="false"
             notifyListenersOnReplication="true"
             mapSendOptions="6"/>
  <!--
  <Manager className="org.apache.catalina.ha.session.DeltaManager"
           expireSessionsOnShutdown="false"
           notifyListenersOnReplication="true"/>
  -->
  <!-- 集群内部通信配置 -->
  <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="192.168.10.2"
              port="5000"
              selectorTimeout="100"
              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.MessageDispatchInterceptor"/>
    <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
  </Channel>
  <!-- 此 vavle 拦截请求,并将 Session 信息发给内部节点 -->
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
         filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

Brief description of the working principle:

  1. nginx forwards the request to Tomcat1, requesting login authentication, create a session, generating Cookie, before the response is returned, copy the information to Tomcat2 Session
  2. When the request again, nginx will take forward the request to the Cookie session Tomcat2, Tomcat2 found to have successfully authenticated internal Session Session object associated pool, no longer returns the requested resource certification

4. Verify that load balancing and replication Session

4.1 Test Environment

  1. Using two PC deployment Tomcat, correspondence is: 172.31.1.41-Tomcat1,172.31.1.42-Tomcat2
  2. Based on the use Tomcat deployment comes SessionExample program, write a web application of a tomcat-benchmark
  3. Combined with built-in Tomcat Manager application, see the application has been deployed inside the Session pool

4.2 Load Balancing

Tomcat-benchmark modify context-param deployment descriptor file is "I'm Tomcat 1/2" for distinguishing between two Tomcat, and start Nginx Tomcat, access the browser request can be seen in two 172.31.1.42:8080 switching between servers:

req-balance

4.3 Session Copy

To facilitate understanding, here Nginx first load balancing policy settings to ip_hash:

  1. Nginx always assumed that the request to locate the Tomcat1, and then create a session on Tomcat1, add some attributes to the session
  2. Close Tomcat1 simulated fault, this time before the session with Nginx will forward the request to tomcat2 Cookie, the
  3. Whether there is a correlation with Cookie (JSESSIONID) on the View Tomcat2 Session information, if it indicates a copy success

The whole process is as follows:

session-copy

FIG moving just above described contrast can be seen from Tomcat2 Session information is copied to the Tomcat1.

5. Summary

Search Micro Signal "epiphany source," the reply "Tomcat", available engineering test used herein and Nginx and Tomcat configuration files.

Guess you like

Origin www.cnblogs.com/wskwbog/p/10934546.html