Tomcat Session Replication Cluster:
一、 编辑server.xml,配置启用集群,将下列配置放置于 <engine> 或 <host> 中。具体信息参考https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html
注意:每台tomcat主机都要添加;Receiver的地址记得修改,绑定的地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址,因此,确保/etc/hosts中的地址与名称正确
<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.239.128"
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>
二、 配置webapps
编辑WEB-INF/web.xml,添加元素;如果指定目录下没有web.xml文件可以从/usr/local/tomcat/conf/复制过去
注意:每个tomcat主机都有添加
[root@centos7 WEB-INF]# cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF
[root@centos7 WEB-INF]#vim web.xml
<web-app>
....
<distributable/>
</web-app>
三、配置测试页面
tomcatA主机
[root@centos7 webapps]# pwd
/usr/local/tomcat/webapps
[root@centos7 webapps]# tree test/
test/
├── classes
├── index.jsp
├── lib
└── WEB-INF
3 directories, 1 file
[root@centos7 webapps]# cat test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.CWJ.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("CWJ.com","CWJ.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
tomcatB主机
[root@centos7 test]# pwd
/usr/local/tomcat/webapps/test
[root@centos7 test]# tree .
.
├── classes
├── index.jsp
├── lib
└── WEB-INF
3 directories, 1 file
[root@centos7 test]# cat index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
四、重启服务
tomcatA主机
[root@centos7 WEB-INF]# su - tomcat -c "catalina.sh stop"
[root@centos7 WEB-INF]# su - tomcat -c "catalina.sh start"
查看日志信息
[root@centos7 logs]# tail -f catalina.out
09-Jan-2020 22:46:27.973 信息 [testhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/webapps/classes] has finished in [10] ms
09-Jan-2020 22:46:27.974 信息 [testhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/data/webapps/lib]
09-Jan-2020 22:46:27.982 信息 [testhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/webapps/lib] has finished in [9] ms
09-Jan-2020 22:46:27.982 信息 [testhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/data/webapps/ROOT]
09-Jan-2020 22:46:27.993 信息 [testhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/webapps/ROOT] has finished in [11] ms
09-Jan-2020 22:46:27.997 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
09-Jan-2020 22:46:28.004 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["ajp-nio-8009"]
09-Jan-2020 22:46:28.008 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 2840 ms
09-Jan-2020 22:47:21.241 信息 [Membership-MemberDisappeared.] org.apache.catalina.ha.tcp.SimpleTcpCluster.memberDisappeared 收到成员消失:[org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 239, 130}:4000,{192, 168, 239, 130},4000, alive=6135856, securePort=-1, UDP Port=-1, id={10 -31 -60 86 55 54 64 -55 -72 -38 -43 11 51 -120 -35 61 }, payload={}, command={66 65 66 89 45 65 76 69 88 ...(9)}, domain={}]]
09-Jan-2020 22:47:28.237 信息 [Membership-MemberAdded.] org.apache.catalina.ha.tcp.SimpleTcpCluster.memberAdded Replication member added:[org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 239, 130}:4000,{192, 168, 239, 130},4000, alive=1019, securePort=-1, UDP Port=-1, id={23 -122 44 40 -80 -48 65 -93 -91 -60 12 -111 30 -121 -103 -28 }, payload={}, command={}, domain={}]]
tomcatB主机
[root@centos7 WEB-INF]# su - tomcat -c "catalina.sh stop"
[root@centos7 WEB-INF]# su - tomcat -c "catalina.sh start"
查看日志信息
09-Jan-2020 22:47:29.567 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/ROOT] has finished in [273] ms
09-Jan-2020 22:47:29.567 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/usr/local/apache-tomcat-8.5.49/webapps/docs]
09-Jan-2020 22:47:29.584 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/docs] has finished in [17] ms
09-Jan-2020 22:47:29.584 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/usr/local/apache-tomcat-8.5.49/webapps/examples]
09-Jan-2020 22:47:29.794 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/examples] has finished in [210] ms
09-Jan-2020 22:47:29.794 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/usr/local/apache-tomcat-8.5.49/webapps/host-manager]
09-Jan-2020 22:47:29.817 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/host-manager] has finished in [23] ms
09-Jan-2020 22:47:29.818 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/usr/local/apache-tomcat-8.5.49/webapps/manager]
09-Jan-2020 22:47:29.841 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/manager] has finished in [23] ms
09-Jan-2020 22:47:29.842 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [/usr/local/apache-tomcat-8.5.49/webapps/test]
09-Jan-2020 22:47:29.868 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-8.5.49/webapps/test] has finished in [26] ms
09-Jan-2020 22:47:29.874 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["http-nio-8080"]
09-Jan-2020 22:47:29.882 信息 [main] org.apache.coyote.AbstractProtocol.start 开始协议处理句柄["ajp-nio-8009"]
09-Jan-2020 22:47:29.884 信息 [main] org.apache.catalina.startup.Catalina.start Server startup in 2749 ms
可以看到服务之间已经收到了多播信息,可以正常复制session了。