zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Tomcat Session集群

Tomcat集群 session
2023-06-13 09:15:42 时间
Tomcat Session集群(问题)

官方文档:https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

 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= 230.100.100.8 

 port= 45564 #45564/UDP

 frequency= 500 

 dropTime= 3000 / 

 Receiver className= org.apache.catalina.tribes.transport.nio.NioReceiver 

 address= auto #此项建议修改为当前主机的IP

 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 

配置说明

Cluster 集群配置 Manager 会话管理器配置 Channel 信道配置 Membership 成员判定。使用什么多播地址、端口多少、间隔时长ms、超时时长ms。同一个多播地址和端口认为同属一个组。使用时修改这个多播地址,以防冲突 Receiver 接收器,多线程接收多个其他节点的心跳、会话信息。默认会从4000到4100依次尝试可用端口 address= auto ,auto可能绑定到127.0.0.1上,所以一定要改为可以用的IP上去 Sender 多线程发送器,内部使用了tcp连接池。 Interceptor 拦截器
ReplicationValve 检测哪些请求需要检测Session,Session数据是否有了变化,需要启动复制过程

使用 Cluster className= org.apache.catalina.ha.tcp.SimpleTcpCluster /

添加到 Engine 所有虚拟主机都可以启用Session复制

添加到 Host ,该虚拟主机可以启用Session复制

最后,在应用程序内部启用了才可以使用

前提:

时间同步,确保NTP或Chrony服务正常运行。 防火墙规则。

本次把多播复制的配置放到缺省虚拟主机里面, 即Host之下。

特别注意修改Receiver的address属性为一个本机可对外的IP地址。

t2的server.xml中,如下

 Host name= t1.magedu.com appBase= /data/webapps autoDeploy= true 

 其他略去

 Receiver className= org.apache.catalina.tribes.transport.nio.NioReceiver 

 address= 10.0.0.101 

 port= 4000 

 autoBind= 100 

 selectorTimeout= 5000 

 maxThreads= 6 / 

t3的server.xml中,如下

 Host name= t2.magedu.com appBase= /data/webapps autoDeploy= true 

 其他略去

 Receiver className= org.apache.catalina.tribes.transport.nio.NioReceiver 

 address= 10.0.0.102 

 port= 4000 

 autoBind= 100 

 selectorTimeout= 5000 

 maxThreads= 6 / 

Tomcat重启后,ss命令能看到tomcat监听在4000端口上

尝试使用刚才配置过得负载均衡(移除Session黏性),测试发现Session还是变来变去。

准备web.xml

在应用中增加WEB-INF,从全局复制一个web.xml过来

# cp /usr/local/tomcat/conf/web.xml /data/webapps/ROOT/WEB-INF/

为web.xml的 web-app 标签增加子标签 distributable/ 来开启该应用程序的分布式。

重启全部Tomcat,通过负载均衡调度到不同节点,返回的SessionID不变了。

本文链接:http://www.yunweipai.com/35185.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52747.html

apachenginxTomcat