Tomcat集群中实现session共享,会话保持
Tomcat自带的session cluster方式可以实现会话共享。
tomcat自带的cluster是基于多播传递以及时间同步的方式,使用session cluster,可以将本地的session复制传递给集群中的其它服务器,多个tomcat间自动实时复制session信息,从而实现session保持。
一、编辑测试页面,返回会话id
Tomcat LB集群搭建参考《Tomcat LB 负载均衡集群实现》
链接:https://blog.csdn.net/field_yang/article/details/80698498
[root@test conf]# cd /data/webapps/ROOT/
[root@test ROOT]# vim index.jsp
[root@test ROOT]# more index.jsp
<%@ page language="java" %>
Session ID | <%= session.getId()%> |
Created on | <%= session.getCreationTime() %> |
[root@www ROOT]# cd /data/webapps/ROOT/
[root@www ROOT]# vim index.jsp
[root@www ROOT]# more index.jsp
<%@ page language="java" %>
Session ID | <%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
访问:http://192.168.88.132/index.jsp,刷新,每次返回的session都不一样,如图为在Google四次刷新。
web1.field.com/Tomcat2
Session ID B5303B4675D513BB409F0824AFEF4121
Created on 1528978355101
test.field.com/Tomcat1
Session ID 4FF2A9DF3CEE3E70AE8464573E9A5C1A
Created on 1528978640933
test.field.com/Tomcat1
Session ID C1D5F6FA4B7DD09C78FE07A7F7C60806
Created on 1528978657213
web1.field.com/Tomcat2
Session ID 3CDB193E556D9766D0F5A5F01B1A598C
Created on 1528978669299
二、添加session共享
添加session共享只需两步
1、编辑server.xml
在
行下面添加
此段代码及用法可以从Tomcat官方文档获取《https://tomcat.apache.org/tomcat-8.0-doc/cluster-howto.html》
此处只需修改:
多播地址:address="228.0.1.8"
服务器地址:address="auto"为address="192.168.88.130"
1)、Tomcat1修改
[root@test conf]# vim server.xml
channelSendOptions="8"> expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> address="228.0.1.8" port="45564" frequency="500" dropTime="3000"/> address="192.168.88.130" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> filter=""/> tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> 2)、Tomcat2修改 [root@www conf]# vim server.xml channelSendOptions="8"> expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> address="228.0.1.8" port="45564" frequency="500" dropTime="3000"/> address="192.168.88.131" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> filter=""/> tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> 3)、两台机上检查Tomcat语法:catalina.shconfigtest [root@www conf]#catalina.sh configtest Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR:/usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server version: Apache Tomcat/8.0.47 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server built: Sep29 2017 13:46:41 UTC 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Server number: 8.0.47.0 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Name: Linux 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: OS Version: 2.6.32-279.el6.x86_64 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Architecture: amd64 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Java Home: /usr/java/jdk1.8.0_151/jre 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Version: 1.8.0_151-b12 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: JVM Vendor: OracleCorporation 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_BASE: /usr/local/apache-tomcat-8.0.47 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: CATALINA_HOME: /usr/local/apache-tomcat-8.0.47 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument:-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument:-Djava.protocol.handler.pkgs=org.apache.catalina.webresources 六月14, 2018 8:31:37 下午org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument:-Djava.endorsed.dirs=/usr/local/tomcat/endorsed 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.base=/usr/local/tomcat 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Dcatalina.home=/usr/local/tomcat 六月 14, 2018 8:31:37 下午 org.apache.catalina.startup.VersionLoggerListener log 信息: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 六月 14, 2018 8:31:37 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The APR based Apache Tomcat Native library which allows optimalperformance in production environments was not found on the java.library.path:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 六月 14, 2018 8:31:37 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-nio-8080"] 六月 14, 2018 8:31:38 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 信息: Using a shared selector for servlet write/read 六月 14, 2018 8:31:38 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-nio-8009"] 六月 14, 2018 8:31:38 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 信息: Using a shared selector for servlet write/read 六月 14, 2018 8:31:38 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1072 ms [root@test conf]# catalina.sh configtest 略 2、确保web.xml有 [root@test conf]# cp web.xml /data/webapps/ROOT/WEB-INF/ [root@test conf]# vim /data/webapps/ROOT/WEB-INF/web.xml [root@test conf]# scp /data/webapps/ROOT/WEB-INF/web.xml 192.168.88.131:/data/webapps/ROOT/WEB-INF/ The authenticity of host '192.168.88.131(192.168.88.131)' can't be established. RSA key fingerprint isda:52:ca:c5:e6:c5:3a:de:5d:bc:07:ed:86:e6:ac:44. Are you sure you want to continueconnecting (yes/no)? yes Warning: Permanently added '192.168.88.131'(RSA) to the list of known hosts. [email protected]'s password: web.xml 100% 166KB 165.7KB/s 00:00 [root@test conf]# tail /data/webapps/ROOT/WEB-INF/web.xml 3、启动服务器,观察日志 1)、Tomcat1 [root@test conf.d]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR:/usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@test conf]# tail/usr/local/tomcat/logs/catalina.out 14-Jun-2018 20:38:27.369 信息 [test.field.com-startStop-1] org.apache.catalina.ha.session.DeltaManager.getAllClusterSessionsManager [], requesting session state fromorg.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 88,131}:4000,{192, 168, 88, 131},4000, alive=9026, securePort=-1, UDP Port=-1,id={91 -111 2 57 -68 -75 73 -115 -111 9 7 -65 -4 118 -5 -49 }, payload={},command={}, domain={}, ]. This operation will timeout if no session state hasbeen received within 60 seconds. 14-Jun-2018 20:38:27.573 信息[test.field.com-startStop-1] org.apache.catalina.ha.session.DeltaManager.waitForSendAllSessionsManager []; session state send at 6/14/18 8:38 PM received in 212 ms. 14-Jun-2018 20:38:27.609 信息[Catalina-startStop-1]org.apache.catalina.ha.session.JvmRouteBinderValve.startInternalJvmRouteBinderValve started 14-Jun-2018 20:38:27.611 信息[test.field.com-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectory Deploying webapplication directory /data/webapps/classes 14-Jun-2018 20:38:27.647 信息[test.field.com-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectoryDeployment of web application directory /data/webapps/classes has finished in37 ms 14-Jun-2018 20:38:27.647 信息[test.field.com-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectory Deploying webapplication directory /data/webapps/lib 14-Jun-2018 20:38:27.663 信息[test.field.com-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectory Deployment of webapplication directory /data/webapps/lib has finished in 16 ms 14-Jun-2018 20:38:27.682 信息 [main]org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler["http-nio-8080"] 14-Jun-2018 20:38:27.699 信息 [main]org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler["ajp-nio-8009"] 14-Jun-2018 20:38:27.709 信息 [main]org.apache.catalina.startup.Catalina.start Server startup in 3453 ms 1)、Tomcat2 [root@www conf]# catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR:/usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@www ROOT]# tail /usr/local/tomcat/logs/catalina.out 14-Jun-2018 20:38:20.395 信息[web1.field.com-startStop-1] org.apache.catalina.ha.session.DeltaManager.startInternalStarting clustering manager at 14-Jun-2018 20:38:20.395 信息[web1.field.com-startStop-1]org.apache.catalina.ha.session.DeltaManager.getAllClusterSessions Manager []:skipping state transfer. No members active in cluster group. 14-Jun-2018 20:38:20.397 信息[Catalina-startStop-1]org.apache.catalina.ha.session.JvmRouteBinderValve.startInternalJvmRouteBinderValve started 14-Jun-2018 20:38:20.398 信息[web1.field.com-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectory Deploying webapplication directory /data/webapps/shopxx 14-Jun-2018 20:38:20.442 信息[web1.field.com-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectory Deployment of webapplication directory /data/webapps/shopxx has finished in 44 ms 14-Jun-2018 20:38:20.456 信息 [main]org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler["http-nio-8080"] 14-Jun-2018 20:38:20.503 信息 [main]org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler["ajp-nio-8009"] 14-Jun-2018 20:38:20.515 信息 [main]org.apache.catalina.startup.Catalina.start Server startup in 4204 ms 14-Jun-2018 20:38:25.941 信息[Tribes-Task-Receiver[web1.field.com-Channel]-1]org.apache.catalina.tribes.io.BufferPool.getBufferPool Created a buffer poolwith max size:104857600 bytes of type:org.apache.catalina.tribes.io.BufferPool15Impl 14-Jun-2018 20:38:26.319 信息 [Membership-MemberAdded.]org.apache.catalina.ha.tcp.SimpleTcpCluster.memberAdded Replication memberadded:org.apache.catalina.tribes.membership.MemberImpl[tcp://{192, 168, 88,130}:4000,{192, 168, 88, 130},4000, alive=1036, securePort=-1, UDP Port=-1,id={-45 -58 90 97 -125 119 71 77 -116 -45 98 31 -63 48 31 -101 }, payload={},command={}, domain={}, ] [root@www ROOT]# 由[Membership-MemberAdded.]可以看到已经成功开启会话共享成功 三、测试会话共享 访问http://192.168.88.132/index.jsp,刷新,可以发现,session保持不变 如图为分别在Firefox和Google浏览器实现会话保持 test.field.com/Tomcat1 Session ID ACDD83C17831B6204D74E39859D2F3FB Created on 1528980076240 web1.field.com/Tomcat2 Session ID ACDD83C17831B6204D74E39859D2F3FB Created on 1528980076240