tomcat8的session共享实现

下载tomcat

版本:apache-tomcat-8.0.53.zip

实现步骤,只需要两步

  1. 两个tomcat的server.xml都增加一样cluster配置
	<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="" />
<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>
  1. 两个tomcat的应用的web.xml下都增加

比如在:

/Users/Library/Tomcat/apache-tomcat-8.0.53-8081/webapps/ROOT/WEB-INF/web.xml

增加:

	<distributable/>
  1. 然后重启两台tomcat即可

新建jsp测试

session.jsp

<html>
<head>
<title>test2</title>
</head>
<body>
SessionID is
<%=session.getId()%>
<BR> SessionIP is
<%=request.getServerName()%>
<BR> SessionPort is
<%=request.getServerPort()%>
<%
out.println("Response from tomcat2");
%>
</body>
</html>

比如放置在:

/Users/Library/Tomcat/apache-tomcat-8.0.53-8082/webapps/ROOT/session.jsp

另一台一样。

访问:

http://localhost:8081/session.jsp

http://localhost:8082/session.jsp

sessionid一致:

SessionID is 6E5D26E07FDE6FB5D01A59F457D64333
SessionIP is tomcat.chinaunicom.tech
SessionPort is 80 Response from tomcat1 SessionID is 6E5D26E07FDE6FB5D01A59F457D64333
SessionIP is tomcat.chinaunicom.tech
SessionPort is 80 Response from tomcat2

注意事项

  1. 需要使用tomcat8版本(上述测试在8.0.53上通过)。如果需要tomcat7,需要修改配置
  2. 不要漏了distributable的配置,漏了session也不能共享
  3. 这种方案只适用于并发量较少的应用,并发量大需使用redis等方案

参考官网:

http://localhost:8082/docs/cluster-howto.html

http://tomcat.apache.org/tomcat-8.0-doc/config/cluster.html

翻译:

http://wiki.jikexueyuan.com/project/tomcat/clustering.html

最新文章

  1. LOL 控制技能的解释
  2. Ios8代码关闭输入预测问题
  3. python核心编程笔记
  4. String与常量池
  5. linux 查看端口号命令
  6. java基础(十)面向对象(五)
  7. PHP的输出缓冲区
  8. 编写优秀jQuery插件的10个技巧
  9. 解决:“Ubuntu 10.04 LTS _Lucid Lynx_ - Release i38...
  10. Asp.Net Web Api 接口
  11. Python学习(一):编写购物车
  12. WCF(一):初识WCF
  13. [C++]2-4 子序列的和
  14. KNN分类算法补充
  15. w97常用功能代码
  16. (转)View Transform(视图变换)详解
  17. Python网络爬虫相关基础概念
  18. 【黑客免杀攻防】读书笔记10 - switch-case分支
  19. &lt;转&gt;python字典排序 关于sort()、reversed()、sorted()
  20. iOS开发-多线程之GCD(Grand Central Dispatch)

热门文章

  1. JPA中映射关系详细说明(一对多,多对一,一对一、多对多)、@JoinColumn、mappedBy说明
  2. Linux下汇编语言学习笔记50 ---
  3. Thinkphp5.0 的视图view的比较标签
  4. Web 后端编程的几个关键点(总结中...)
  5. 【Windows系统】-- 远程桌面时,WIN键被锁定
  6. linux以下安装dnw
  7. html实现类似excel的复杂表格,及导出到excel
  8. CentOS 7 防火墙开启了哪些服务和端口?
  9. HDU 1030 数学题
  10. 基于MFC的一个简单计算器