tomcat session cluster的实现

发布时间 2023-10-08 09:40:50作者: 小糊涂90
#1.环境准备
	IP           主机名              服务
	10.0.0.150   proxy.tan.com      httpd
	10.0.0.160   tomcat1.tan.com    JDK8、tomcat8
	10.0.0.170   tomcat2.tan.com    JDK8、tomcat8

#2.在proxy主机配置httpd实现后端tomcat主机轮询
[root@proxy ~]#vim /etc/httpd/conf.d/tomcat.conf
<Proxy balancer://tomcat-server>
  BalancerMember http://tomcat1.tan.com:8080 loadfactor=1
  BalancerMember http://tomcat2.tan.com:8080 loadfactor=1
</Proxy>
<VirtualHost *:80>
  ServerName proxy.tan.com
  ProxyRequests Off
  ProxyVia On
  ProxyPreserveHost On
  ProxyPass / balancer://tomcat-server/
  ProxyPassReverse / balancer://tomcat-server/
</VirtualHost>

[root@proxy ~]#systemctl restart httpd

#或者使用nginx来实现后端轮询
	#在http语句块中添加upstream模块
    upstream tomcat-server{
        server tomcat1.tan.com:8080;
        server tomcat2.tan.com:8080;
        }
        
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  proxy.tan.com;  #修改此行
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://tomcat-server;#添加此行
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
[root@proxy ~]#systemctl start nginx

#编写hosts解析
[root@proxy ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.160 tomcat1.tan.com
10.0.0.170 tomcat2.tan.com

#3.在后端tomcat1主机上安装tomcat并修改conf/server.xml
#安装脚本请见上一此作业
[root@tomcat1 ~]#bash tomcat8.5.sh
[root@tomcat1 ~]#vim /usr/local/tomcat/conf/server.xml
...#略
 <Engine name="Catalina" defaultHost="tomcat1.tan.com">
...#略
      <Host name="tomcat1.tan.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
 <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="230.188.188.188"  #配置多播地址
 port="45564"
 frequency="500"
 dropTime="3000"/>
 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
 address="10.0.0.160"  #配置本机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>

      </Host>
...#略

[root@tomcat1 ~]#cat /usr/local/tomcat/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

[root@tomcat1 ~]#systemctl restart tomcat


#4.在后端tomcat2主机上安装tomcat并修改conf/server.xml
#安装脚本请见上一此作业
[root@tomcat2 ~]#bash tomcat8.5.sh
[root@tomcat2 ~]#vim /usr/local/tomcat/conf/server.xml
...#略
 <Engine name="Catalina" defaultHost="tomcat2.tan.com">
...#略
      <Host name="tomcat2.tan.com"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
 <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="230.188.188.188"
 port="45564"
 frequency="500"
 dropTime="3000"/>
 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
 address="10.0.0.170"
 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>

      </Host>
...#略
[root@tomcat2 ~]#cat /usr/local/tomcat/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

[root@tomcat2 ~]#systemctl restart tomcat


#5.修改应用的web.xml文件开启该应用程序的分布式
 #修改tomcat1主机的应用的web.xml文件
 [root@tomcat1 ~]#tail -n3 /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml
  </description>
<distributable/> #添加此行
</web-app>
[root@tomcat1 ~]#systemctl restart tomcat


#修改tomcat2主机的应用的web.xml文件
 [root@tomcat2 ~]#tail -n3 /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml
  </description>
<distributable/> #添加此行
</web-app>
[root@tomcat2 ~]#systemctl restart tomcat

#6.访问测试,此时主机轮询但是session不会变
[root@proxy ~]#curl -I proxy.tan.com/index.jsp                                            HTTP/1.1 200
Server: nginx/1.14.1
Date: Mon, 29 Aug 2022 05:05:28 GMT
Content-Type: text/html;charset=ISO-8859-1
Connection: keep-alive
Set-Cookie: JSESSIONID=06CEDA54AEFBBE67465BC4307874000D; Path=/; HttpOnly

[root@proxy ~]#curl -b "JSESSIONID=06CEDA54AEFBBE67465BC4307874000D" proxy.tan.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.170:8080</div>
<div>SessionID = <span style="color:blue">06CEDA54AEFBBE67465BC4307874000D</span></div>
Mon Aug 29 13:05:48 CST 2022
</body>
</html>
[root@proxy ~]#curl -b "JSESSIONID=06CEDA54AEFBBE67465BC4307874000D" proxy.tan.com/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tomcat test</title>
</head>
<body>
<div>On tomcat-server</div>
<div>10.0.0.160:8080</div>
<div>SessionID = <span style="color:blue">06CEDA54AEFBBE67465BC4307874000D</span></div>
Mon Aug 29 13:05:52 CST 2022
</body>
</html>