多个tomcat配置 cluster 使用组播方式共享session,会出现session丢失,为什么?
最近成功使用了 Nginx + 2 个tomcat7 在一台Linux机器上实现了负载均衡。 tomcat之间通过配置cluster + web.xml 配置 <distributable /> 实现 session复制功能,但是当在web系统中点击url访问功能,会时不时出现session丢失,要求重新登录。 通过chrome或firebug看拦截每个URL时的头部 cookie,可以看到 JSESSIONID = xxxxx.jvm1 和 JSESSIONID = xxxxx.jvm2 进行切换,这说明了 nginx 负载均衡有效,将http请求分发给2个tomcat进行处理,但是当访问了几次http(或 jvm1, jvm2切换几次)后,就出现了 session 丢失,JSESSIONID 值变了,不知道为什么了? 下面的2个tomcat的 cluster 配置: tomcat:8080
<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="10.0.125.15"
port="4001"
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=""/>
<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>
tomcat:9090
<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="10.0.125.15"
port="4002"
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=""/>
<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. linux 的组播服务是正常的:route add -net 224.0.0.0 netmask 240.0.0.0 dev eth1 2. 2个tomcat之间的tcp通信也是正常的,启动2个tomcat后,会在日志中看到类似的: Manager [localhost#/crm], requesting session state from org...Memberlmpl[tcp://{10,0,125,15}:4002] .... 3. 当我停掉一个tomcat后,令一个tomcat的日志中就会显示: Received member disappeared:org....MemberImlp[tcp://{10,0,125,15}:4001], ...., UDP Port=-1,.. id={-3 67....}...., command={66 65 66 89......}, domain={} 以上都说明了2个tomcat之间的集群通信都是OK的,但是为什么2个tomcat之间的session共享有时正常,有时不正常呢?时不时的 session 就丢失了。 请大家帮我看看啊,我的CRM项目中使用大量的iframe,有没有可能是 iframe 造成了2个tomcat之间session共享丢失呢?