参考文档:Apache Tomcat 8 (8.5.82) - Clustering/Session Replication How-To
实验环境为本机,操作系统win10,tomcat版本8.5.55,配置两个tomcat实例,端口分别配置为8080,9080。
官方建议4个节点以下。
测试中用的demo:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>demo</display-name>
<distributable/>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<h2>
session id:<% out.println(session.getId() + "/tomcat 8080"); %><br>
remote host :<%out.println(request.getRemoteHost()); %><br>
remote port :<%out.println(request.getRemotePort()); %>
</h2>
</body>
</html>
如下:
具体配置步骤如下:
1、修改tomcat配置文件server.xml端口号一个是8080,另一个是9080
2、在tomcat配置文件server.xml文件中增加如下配置(其中8080中端口号为4000,9080中端口号为4001,这里可以是4000到4100的任意值,因为是同一台机器,需要保证两台中唯一即可,其他都一样):
<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="auto"
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"/>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/><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>
3、在自己的项目中的web.xml中加入<distributable/>配置,如下:
4、在测试项目demo的jsp页面中加入测试代码,为了区分,一个信息写tomcat 8080,另一个写tomcat 9080:
5、启动tomcat8080,tomcat9080
6、访问测试,看session 信息是否一致,可以看到session id一致,均为37031974369C8F7847BB99217258F5CB :
7、可以关闭其中一台实例,再启动,发现session id不变。