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

posted @ 2017-06-27 16:08  豆苗稀  阅读(4173)  评论(1编辑  收藏  举报