[转帖]多个tomcat之间的session复制_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3476 | 回复: 0   主题: [转帖]多个tomcat之间的session复制        下一篇 
FDY1
注册用户
等级:网站编辑
经验:436455203
发帖:23
精华:2
注册:2013-3-6
状态:离线
发送短消息息给FDY1 加好友    发送短消息息给FDY1 发消息
发表于: IP:您无权察看 2013-3-11 14:30:14 | [全部帖] [楼主帖] 楼主

用tomcat做负载集群时, 经常会用到session复制(Session Replication), 很多例子会告诉我们要配置apache或者其他的Web Server. 而事实上, 单纯从session复制的角度讲, 是不需要Web Server的.

tomcat的session复制分为两种, 一种是全局试的(all-to-all), 这意味着一个node(tomcat实例)的session发生变化之后, 它会将这些变更复制到其他所有集群组的成员;另一种是局部试的, 它会用到BackupManager, BackupManager能实现只复制给一个Buckup Node, 并且这个Node会部署相同的Web应用, 但是这种方式并没用经过很多的测试北京联动北方科技有限公司(来自官方说明..).

tomcat的session复制是基于IP组播(multicast)来完成的, 详细的IP组播介绍可以参考这里.

简单的说就是需要进行集群的tomcat通过配置统一的组播IP和端口来确定一个集群组, 当一个node的session发生变更的时候, 它会向IP组播发送变更的数据, IP组播会将数据分发给所有组里的其他成员(node).

配置如下(这里所有的tomcat都在不同的物理主机, 如果在同一台主机上需要改tomcat的tcpListenPort)

Xml代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
  2. channelSendOptions="8">
  3. <ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
  4. expireSessionsOnShutdown="false"
  5. notifyListenersOnReplication="true"/>
  6. <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
  7. <MembershipclassName="org.apache.catalina.tribes.membership.McastService"
  8. address="228.0.0.4"
  9. port="45564"
  10. frequency="500"
  11. dropTime="3000"/>
  12. <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
  13. address="auto"
  14. port="4000"
  15. autoBind="100"
  16. selectorTimeout="5000"
  17. maxThreads="6"/>
  18. <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
  19. <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
  20. </Sender>
  21. <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
  22. <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  23. </Channel>
  24. <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
  25. filter=""/>
  26. <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  27. <DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
  28. tempDir="/tmp/war-temp/"
  29. deployDir="/tmp/war-deploy/"
  30. watchDir="/tmp/war-listen/"
  31. watchEnabled="false"/>
  32. <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  33. <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
  34. </Cluster>

[xml]view plaincopyprint?

  1. <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"
  2. channelSendOptions="8">
  3. <ManagerclassName="org.apache.catalina.ha.session.DeltaManager"
  4. expireSessionsOnShutdown="false"
  5. notifyListenersOnReplication="true"/>
  6. <ChannelclassName="org.apache.catalina.tribes.group.GroupChannel">
  7. <MembershipclassName="org.apache.catalina.tribes.membership.McastService"
  8. address="228.0.0.4"
  9. port="45564"
  10. frequency="500"
  11. dropTime="3000"/>
  12. <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"
  13. address="auto"
  14. port="4000"
  15. autoBind="100"
  16. selectorTimeout="5000"
  17. maxThreads="6"/>
  18. <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">
  19. <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
  20. </Sender>
  21. <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
  22. <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
  23. </Channel>
  24. <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"
  25. filter=""/>
  26. <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
  27. <DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"
  28. tempDir="/tmp/war-temp/"
  29. deployDir="/tmp/war-deploy/"
  30. watchDir="/tmp/war-listen/"
  31. watchEnabled="false"/>
  32. <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
  33. <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>
  34. </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.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>


然后新建一个web应用, 我们这里叫TomcatClusterDemo, web context与名称一致.

新建一个jsp, 这个jsp复制向session里创建/更新属性, 并将session里的所有属性显示在网页上.

当然, 为了验证我们的session是同步的, 我们还将session ID显示了出来, 代码如下:

Jsp代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1.  <%@ page contentType="text/html; charset=UTF-8" %> 
  2. <%@ page import="java.util.*" %> 
  3. <html><head><title>Tomcat Cluster Demo</title></head> 
  4. <body> 
  5. Server Info: 
  6. <% 
  7. out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%> 
  8. <% 
  9.  out.println("<br> ID " + session.getId()+"<br>"); 
  10.  String dataName = request.getParameter("dataName"); 
  11.  if (dataName != null && dataName.length() > 0) { 
  12.        String dataValue = request.getParameter("dataValue"); 
  13.        session.setAttribute(dataName, dataValue); 
  14.        System.out.println("application:" + application.getAttribute(dataName)); 
  15.        application.setAttribute(dataName, dataValue); 
  16.  } 
  17.  out.print("<b>Session List</b>"); 
  18.  Enumeration<String> e = session.getAttributeNames(); 
  19.  while (e.hasMoreElements()) { 
  20.        String name = e.nextElement(); 
  21.        String value = session.getAttribute(name).toString(); 
  22.        out.println( name + " = " + value+"<br>"); 
  23.        System.out.println( name + " = " + value); 
  24.  } 
  25. %> 
  26.  <form action="test.jsp" method="POST"> 
  27.  Name:<input type=text size=20 name="dataName"> 
  28.  <br> 
  29.  Value:<input type=text size=20 name="dataValue"> 
  30.  <br> 
  31.  <input type=submit> 
  32.  </form> 
  33. </body> 
  34. </html> 


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Tomcat Cluster Demo</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");

String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
       String dataValue = request.getParameter("dataValue");
       session.setAttribute(dataName, dataValue);
       System.out.println("application:" + application.getAttribute(dataName));
       application.setAttribute(dataName, dataValue);
}
out.print("<b>Session List</b>");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
       String name = e.nextElement();
       String value = session.getAttribute(name).toString();
       out.println( name + " = " + value+"<br>");
       System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
Name:<input type=text size=20 name="dataName">
<br>
Value:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>


同时, 在web.xml里增加<distributable/>描述

Xml代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appid="WebApp_ID"version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  3. <display-name>TomcatClusterDemo</display-name>
  4. <distributable/>
  5. <welcome-file-list>
  6. <welcome-file>index.html</welcome-file>
  7. <welcome-file>index.htm</welcome-file>
  8. <welcome-file>index.jsp</welcome-file>
  9. <welcome-file>default.html</welcome-file>
  10. <welcome-file>default.htm</welcome-file>
  11. <welcome-file>test.jsp</welcome-file>
  12. </welcome-file-list>
  13. </web-app>



[xml]view plaincopyprint?

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appid="WebApp_ID"version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  3. <display-name>TomcatClusterDemo</display-name>
  4. <distributable/>
  5. <welcome-file-list>
  6. <welcome-file>index.html</welcome-file>
  7. <welcome-file>index.htm</welcome-file>
  8. <welcome-file>index.jsp</welcome-file>
  9. <welcome-file>default.html</welcome-file>
  10. <welcome-file>default.htm</welcome-file>
  11. <welcome-file>test.jsp</welcome-file>
  12. </welcome-file-list>
  13. </web-app>


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>TomcatClusterDemo</display-name>
<distributable/>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
</web-app>

现在将TomcatClusterDemo部署到两个tomcat上(直接将war包或者部署文件拷贝到webapps下, 或者通过Tomcat Web Application Manager部署), 依次启动两个tomcat.

先访问第一台tomcat(下面的9.119.84.68)的test.jsp, 并添加几个session.

然后访问第二台tomcat(下面的9.119.84.88)的test.jsp, 为了告诉tomcat我们要用那个session访问, 我们需要在URL后面加上 ;jsessionid=SESSION_ID

SESSION_ID可以从第一个test.jsp页面上获得.

看看效果, 两个在不同server上的tomcat实现了session复制!

北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论