tomcat load balancing and session

tomcat load balancing

nginx

1) Create two tomcat containers on docker

[root@ydong ~]# docker run --name tomcat -d --hostname ydong.com -v /data/ydong:/usr/local/ydong tomcat:9.0.37-jdk8-openjdk-slim-buster 

[root@ydong ~]# docker run --name tomcat2 -d --hostname ydong.com -v /data/ydong2:/usr/local/ydong tomcat:9.0.37-jdk8-openjdk-slim-buster

2) Prepare web pages for two containers

[root@ydong data]# ls ydong/ROOT/
class  index.jsp  lib  WEB-INF、

[root@ydong data]# ls ydong2/ROOT/
class  index.jsp  lib  WEB-INF


# 两个是同样的目录,但是index.jsp不一样

3) Copy the files in the storage volume directly to the webapps directory

[root@ydong data]# docker exec -it tomcat /bin/bash
root@ydong:/usr/local/tomcat# cp -r ../ydong/ROOT webapps

[root@ydong data]# docker exec -it tomcat2 /bin/bash
root@ydong:/usr/local/tomcat# cp -r ../ydong/ROOT webapps

4) Test access interface
Insert picture description here

Insert picture description here

5) Use nginx to reverse the generation, nginx and docker are on the same host

upstream tcservs {
    
    
         server 172.17.0.2:8080;
         server 172.17.0.3:8080;
      }

location / {
    
    
             proxy_pass http://tcservs;
          }

#添加上以上规则,由于是测试,所以没设置权重和关闭了长连接以及它的proxy_cache相关选项

First visit
Insert picture description here
refresh visit

Insert picture description here

httpd

httpd uses its proxy modules instead.
Example:

vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tcsrvs>
                    BalancerMember http://172.17.0.2:8080 loadfactor=1   
                    # loadfactor 权重
                    BalancerMember http://172.17.0.3:8080 loadfactor=1
                    ProxySet lbmethod=byrequests
                    #lbmethod:访问方式,byrequests 加权轮询
               </Proxy>

               <VirtualHost *:80>
                    ServerName www.ydong.com
                    ProxyVia On
                    ProxyRequests Off
                    ProxyPreserveHost On
  		            keepalive off
                    <Proxy *>
                         Require all granted
                    </Proxy>
                    ProxyPass / balancer://tcsrvs/
                    ProxyPassReverse / balancer://tcsrvs/
                    <Location />
                         Require all granted
                    </Location>
               </VirtualHost>    

Session retention

Session stickiness

nginx

There are several ways of nginx session sticky

  • ip_hash
  • hash key

Nginx is directly modified in nginx.conf

Simple example:

 upstream tcservs{
    
    
        ip_hash;
        server 172.17.0.2:8080;
        server 172.17.0.3:8080;
    }


        location / {
    
    
          proxy_pass http://tcservs;
        }

According to the source address hash, as long as the source address is accessed, they are sent to the same back-end server

Example of consistent hash:

 upstream tcservs{
    
    
        hash $request_uri consistent;
        server 172.17.0.2:8080;
        server 172.17.0.3:8080;
    }

 location / {
    
    
          proxy_pass http://tcservs;
        } 

The URI of the binding request is sent to the same server on the backend as long as the same uri is accessed.

httpd

If httpd implements session sticky, cookies are needed

Example:

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<proxy balancer://tcsrvs>
                    BalancerMember http://172.17.0.2:8080  route=TomcatA  loadfactor=1
                    BalancerMember http://172.17.0.3:8080  route=TomcatB  loadfactor=1
                    ProxySet lbmethod=byrequests
		    ProxySet stickysession=ROUTEID	               
</Proxy>

               <VirtualHost *:80>
                    ServerName www.ydong.com
                    ProxyVia On
                    ProxyRequests Off
                    ProxyPreserveHost On
		    keepalive off
                    <Proxy *>
                         Require all granted
                    </Proxy>
                    ProxyPass / balancer://tcsrvs/
                    ProxyPassReverse / balancer://tcsrvs/
                    <Location />
                         Require all granted
                    </Location>
               </VirtualHost>    

First visit
Insert picture description here
second visit
Insert picture description here

When maintaining the session, the tomcat server.xml file needs to add a JVMRoute option. The function of this option is mainly

The principle of jvm_route (from author Weibin Yao):

  1. At the beginning, if the request comes, without session information, jvm_route will be sent to a tomcat according to the method of round robin.
  2. Tomcat adds the session information and returns it to the customer.
  3. When the user makes this request again, jvm_route sees the name of the back-end server in the session, and it forwards the request to the corresponding server.

For the time being, the jvm_route module does not support the default fair mode. The working mode of jvm_route conflicts with fair. For a specific user, when the tomcat that has been serving him goes down, by default it will retry the number of max_fails. If it still fails, it will re-enable the round
robin method. In this case, the user’s The session is lost.

In general, jvm_route uses session_cookie to achieve session stickiness, attaching a specific session to a specific tomcat, thereby solving the problem of session asynchronous synchronization, but it cannot solve the problem of session transfer after downtime.
Reposted from: http://blog.163.com/momoliu88@126/blog/static/139208463201231104120587/

It can be seen from httpd that the request message header has TomcatB. When the back-end server sees TomcatB, the cluster in tomcat will respond according to the corresponding JVMRoute.

tomcat session replication cluster

Tomcat has the function of session replication. In the tomcat cluster, the session of each node will be synchronized to the server of each node in the cluster. This function is implemented by DeltaManager (cluster incremental session manager) in tomcat.

Example The
following needs to be written in engine or host on server.xml. Engine means all hosts, host means designated host.

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  	
                 channelSendOptions="8">

          <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.192.131"   #此处是绑定地址,最高绑定在服务的ip地址上
                      port="4000"
                      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.MessageDispatchInterceptor"/>
          </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.ClusterSessionListener"/>
        </Cluster>

After the above is completed, you need to add <distributable/>elements in web.xml . And web.xml is copied to WEB-INF.

There is no difference between the previous anti-generation and the above, nginx and httpd, HAproxy can be.

test
Insert picture description here

Insert picture description here

As you can see, the content is different, but the conversation always exists

Guess you like

Origin blog.csdn.net/qq_44564366/article/details/107946536