1. 程式人生 > >細說tomcat之集群session共享方案

細說tomcat之集群session共享方案

eth0 -h 4.0 kernel mage etc 插件 root b-

技術分享

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為實際網卡名稱)

[[email protected]_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共享方案