Tomcat8.5的session復制技術


Tomcat集群中,不同Tomcat的session是孤立的,在訪問不同Tomcat時,用戶的登錄信息無法保持一致性。這時,可以使用Tomcat原生的session復制技術。

  • 修改Tomcat的server.xml,去掉下面代碼的注釋

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

這是默認使用了如下配置

   <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.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>

如果Tomcat在同一台主機上,需要保證Receiver.port的唯一性。如果使用Tomcat的默認配置,Tomcat會自動保證Receiver.port的唯一性,范圍在4000-4100。

228.0.0.4是組播地址,這是一個保留地址,一般使用默認即可。同一個集群的Tomcat,Membership 的地址和端口號要相同。

  • 開啟Membership 和 Receiver 的端口

/sbin/iptables -I INPUT -p udp --dport 45564 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 4000 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 4001 -j ACCEPT
/etc/rc.d/init.d/iptables save
  • 修改項目web.xml,加入<distributable/>

<distributable/>位置在<display-name>下面即可。

        <display-name></display-name>
        <distributable/>

參考鏈接

http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

http://blog.csdn.net/fengwind1/article/details/52163096


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM