首页 > 代码库 > 细说tomcat之集群session共享方案
细说tomcat之集群session共享方案
1. Tomcat Cluster
官网:http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
Tomcat原生支持的集群方案。
<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.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/> <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.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
注意事项:
(1)系统必须允许广播,Tomcat通过广播机制传递session复制信息。
在实践中遇到的错误:
严重: Unable to start cluster. org.apache.catalina.tribes.ChannelException: java.net.SocketException: 没有那个设备; No faulty members identified.
添加广播路由:route add -host 228.0.0.4 dev eth0(eth0为实际网卡名称)
[root@centosx64_tomcat1 ~]# route -en Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 228.0.0.4 0.0.0.0 255.255.255.255 UH 0 0 0 eth2 192.168.70.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth2
(2)解决报错:java.net.NoRouteToHostException: No route to host (Host unreachable)
检查Tomcat集群内的主机防火墙是否开启,如果防火墙打开,需要允许4000端口访问(tomcat cluster使用4000进行集群内通信)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4000 -j ACCEPT
(3)Tomcat官方推荐只在小规模集群时使用。详见:https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html。
2. Hazelcast IMDG Plugins 开源版本插件
https://github.com/hazelcast/hazelcast-tomcat-sessionmanager#tomcat-based-web-session-replication
在测试用中发现该插件P2P模式时不稳定,经常会出现session不能及时同步的问题。
3. Redis方案
https://github.com/jcoleman/tomcat-redis-session-manager
采用Redis作为session存储方案,实现多实例session共享。
4.总结
根据实际生产环境集群规模选择恰当的方案。
【参考】
http://mp.weixin.qq.com/s/NnnqVrC9-Jekwy3Opmvy_w session一致性架构设计实践
http://5880861.blog.51cto.com/5870861/1671622 Tomcat集群问题记录
http://blog.csdn.net/q_l_s/article/details/52015296 tomcat - 报错 No such device; No faulty members identified.
https://wiki.apache.org/tomcat/FAQ/Clustering The cluster doesn‘t work under Linux with two nodes on two boxes.
https://hazelcast.org/ Hazelcast IMDG
https://hazelcast.org/plugins/?type=web-clustering Hazelcast IMDG Plugins
http://blog.csdn.net/catoop/article/details/48603891 Tomcat7基于Redis的Session共享
http://www.cnblogs.com/lengfo/p/4260363.html 基于nginx tomcat redis分布式web应用的session共享配置
细说tomcat之集群session共享方案