Tomcat基於DeltaManager實現Session Cluster

一、簡單說明:

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一直不變


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章