一、简单说明:

Tomcat会话管理器(Session Manager)分类:

Ø  StandardManager:标准会话管理器,提供一个专门管理某个Web应用所有会话的容器,并且会在Web应用启动停止时进行会话重加载和持久化

Ø  PersistentManager:持久会话管理器,将会话数据保存至文件存储(FileStore)或JDBC存储(JDBCStore)中,并且能在服务器意外中止后重新启动、重新加载这些会话信息

Ø  DeltaManager:集群增量会话管理器,Tomcat默认的集群会话管理器,将某节点的会话改变同步到集群内其它成员节点上,属于全节点复制模式,所谓全节点复制是指集群中某个节点的状态变化后需要同步到集群中剩余的所有节点,非全节点方式可能只是同步到其中某个或若干个节点,全节点复制的网络流量随节点数量增加呈平方趋势增长,也正是因为这个因素导致无法构建较大规模的集群

Ø  BackupManager:集群备份会话管理器,每个会话只会有一个备份,它使会话备份的网络流量随节点数量的增加呈线性趋势增长,大大减少了网络流量和逻辑操作,可构建较大的集群

 

二、演示环境:

IP

操作系统

部署程序

192.168.1.143

CentOS 7.6

Nginx-1.12.2

192.168.1.144

CentOS 7.6

Tomcat-8.5.37

192.168.1.145

CentOS 7.6

Tomcat-8.5.37

1、三台服务器时间同步NTP

2、配置192.168.1.144节点的主机名:

# vim /etc/hosts --> 192.168.1.144 TomcatA.qiuyue.com TomcatA

# vim /etc/hostname --> TomcatA

# hostnamectl set-hostname TomcatA

# hostname TomcatA

# logout

Ctrl + Shift + r

# hostname

3、配置192.168.1.145节点的主机名:

# vim /etc/hosts --> 192.168.1.145 TomcatB.qiuyue.com TomcatB

# vim /etc/hostname --> TomcatB

# hostnamectl set-hostname TomcatB

# hostname TomcatB

# logout

Ctrl + Shift + r

# hostname

4、192.168.1.144节点创建如下测试页:

# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="red">TomcatA.qiuyue.com</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

5、192.168.1.145节点创建如下测试页:

# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatB</title></head>

<body>

<h1><font color="blue">TomcatB.qiuyue.com</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

6、分别启动192.168.1.144节点和192.168.1.145节点上的Tomcat

# catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

7、配置192.168.1.143节点域名解析:# vim /etc/hosts

192.168.1.144 TomcatA.qiuyue.com TomcatA

192.168.1.145 TomcatB.qiuyue.com TomcatB

8、192.168.1.143节点中使用curl命令访问:

# curl http://TomcatA.qiuyue.com:8080/test.jsp         //能正常显示文本内容

# curl http://TomcatB.qiuyue.com:8080/test.jsp         //能正常显示文本内容

9、192.168.1.143节点安装Nginx实现反代和动静分离:

# yum -y install epel-release

# yum -y install nginx

# cd /etc/nginx

# cp nginx.conf nginx.conf.bak

# vim nginx.conf

(1)http配置段中、server配置段外新增如下upstream

upstream tcsrvs {

server TomcatA.qiuyue.com:8080;

server TomcatB.qiuyue.com:8080;

}

(2)server配置段中新增如下location

location ~* \.(jsp|do)$ {

proxy_pass http://tcsrvs;

}

备注:将.jsp.do结尾的请求反向代理至http://tcsrvs

# nginx -t  # systemctl start nginx.service  # ss -tunlp | grep -w :80

10、本地浏览器访问测试:

192.168.1.143

image.png

192.168.1.143/index.jsp

image.png

192.168.1.143/test.jsp

image.png

image.png

刷新页面,轮询显示,且Session ID一直在变:

image.png

image.png

备注:如果使用了Tomcat自带的Session ClusterNginx无需配置session-sticky

11、192.168.1.144节点配置使用DeltaManager

# catalina.sh stop  # ss -tunlp | grep -w :8080

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml,在<Engine></Engine>配置段中新增如下代码:

<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.1.144"

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>

备注:

Ø  上述代码如果定义在Engine容器中,则表示对所有主机均启用集群功能;如果定义在某Host中,则表示仅对此主机启用集群功能

Ø  address="228.0.0.4",表示多播地址,且所有Tomcat节点必须为同一个多播地址

Ø  参考文档http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

# catalina.sh configtest

12、192.168.1.145节点配置使用DeltaManager

# catalina.sh stop  # ss -tunlp | grep -w :8080

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml,在<Engine></Engine>配置段中新增如下代码:

<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.1.145"

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>

# catalina.sh configtest

13、192.168.1.144节点修改配置文件web.xml

# cd /usr/local/tomcat/webapps/ROOT/WEB-INF

# cp web.xml web.xml.bak

# vim web.xml在末尾</web-app>上方新增代码:<distributable/>

image.png

备注:所有启用集群功能的webapps,其web.xml中都必须添加<distributable/>才能实现集群功能。如果某webapps没有自己的web.xml,也可以通过复制默认的/usr/local/tomcat/conf/web.xml至其WEB-INF目录中实现。

# catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

14、192.168.1.145节点修改配置文件web.xml

# cd /usr/local/tomcat/webapps/ROOT/WEB-INF

# cp web.xml web.xml.bak

# vim web.xml,在末尾</web-app>上方新增代码:<distributable/>

image.png

# catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8080

15、192.168.1.144节点查看日志:# tail /usr/local/tomcat/logs/catalina.out

image.png

16、192.168.1.145节点查看日志:# tail -100 /usr/local/tomcat/logs/catalina.out

image.png

17、本地浏览器访问测试:

192.168.1.143/test.jsp

image.png

image.png

刷新页面,轮询显示,且Session ID一直不变