码迷,mamicode.com
首页 > 其他好文 > 详细

N个tomcat实现Session共享

时间:2017-07-12 12:11:27      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:pac   transport   local   options   问题   文档   目的   jvm   parallel   

集群之后比如我们有N个Tomcat,用户在访问我们的网站时有可能第一次请求分发到tomcat1下,而第二次请求又分发到了tomcat2下,有过web开发经验的朋友都知道这时session不一致会导致怎样的后果,所以我们需要解决一下多个tomcat之间session共享的问题。

 

修改index.jsp

SessionID:<%=session.getId()%>  

 SessionIP:<%=request.getServerName()%>  

SessionPort:<%=request.getServerPort()%>  

out.println("This is Tomcat Server 11111");  

 

修改server.xml文件

<Engine name="Catalina" defaultHost="localhost">

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

  1. address="auto"  
  2. port="4000"  
  3. autoBind="100"  
  4. selectorTimeout="5000"  
  5. maxThreads="6"/>  
  6. <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  7. <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  8. </Sender>  
  9. <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  10. <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  11. </Channel>  
  12. <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  13. filter=""/>  
  14. <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  15. <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  16. tempDir="/tmp/war-temp/"  
  17. deployDir="/tmp/war-deploy/"  
  18. watchDir="/tmp/war-listen/"  
  19. watchEnabled="false"/>  
  20. <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  21. <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  22. </Cluster>  

</Engine>

 

这个就是tomcat自带的集群配置了,我们可以在tomcat官方文档中的cluster-howto.html中看到相关注意事项,其中有一条需要注意一下:

 

Make sure your web.xml has the <distributable/> element

很明显是说我们的web项目的web.xml文件中需要有<distributable/>这个元素,所以在我们刚才引入的web项目中做如上的修改。

以上配置就可以实现session共享,此共享是基于apache自带的。

 

N个tomcat实现Session共享

标签:pac   transport   local   options   问题   文档   目的   jvm   parallel   

原文地址:http://www.cnblogs.com/Lichuntao/p/7154395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!