手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Apache+Tomcat负载均衡两种session共享方式的设置

复制会话

[日期:2012-08-30] 来源:Linux社区  作者:shuzui1985 [字体:大 中 小]

以下设置和Apache+Tomcat负载均衡两种session共享方式的设置之一粘性会话 一文中的粘性会话设置基本一样http://www.linuxidc.com/Linux/2012-08/69311.htm

1、修改apache http server配置文件http.conf,首先load三个model,代码如下:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

2、然后在此配置文件末端加入以下代码(该处配置与前文有些差异):

ProxyPass / balancer://tomcatcluster/

ProxyPassReverse / balancer://tomcatcluster/   
  
<Proxy balancer://tomcatcluster>  
BalancerMember ajp://localhost:8009 route=a  
BalancerMember ajp://localhost:9009 route=b
</Proxy>

3、接下来修改Tomcat的server.xml文件,如下:

<!--Define an AJP 1.3 Connector on port 8009 -->
    <Connectorport="8009"
                enableLookups
="false" redirectPort="8443" protocol="AJP/1.3" />

其中的port为前面<Proxy>中设定的端口,还要配置其route,代码如下:

<!--Define the top level container in our container hierarchy-->
    <Enginename="Catalina"defaultHost="localhost"jvmRoute="a">

jvmRoute也须同前面的设置一样。 【Linux公社 http://www.linuxidc.com 】

4、另外,还需要在tomcat中将server.xml加入一下集群session复制配置(该处于参考的文档有些出入,主要是因为tomcat版本不一样,此处也有时效性,一些以你的tomcat 的docs文档中的配置实例为准):如果你不想这么麻烦的话直接将<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>一句放开注释即可,但是可能会遇到一些问题,尤其当你用虚拟机的时候。

  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
  2.            channelSendOptions="8">  
  3.   
  4.     <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  5.              expireSessionsOnShutdown="false"  
  6.              notifyListenersOnReplication="true"/>  
  7.   
  8.     <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  9.       <Membership className="org.apache.catalina.tribes.membership.McastService"  
  10.                   address="228.0.0.4"  
  11.                   port="45564"  
  12.                   frequency="500"  
  13.                   dropTime="3000"/>  
  14.       <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  15.                 address="auto"  
  16.                 port="4000"  
  17.                 autoBind="100"  
  18.                 selectorTimeout="5000"  
  19.                 maxThreads="6"/>  
  20.   
  21.       <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  22.         <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  23.       </Sender>  
  24.       <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  25.       <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  26.     </Channel>  
  27.   
  28.     <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  29.            filter=""/>  
  30.     <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  31.   
  32.     <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  33.               tempDir="/tmp/war-temp/"  
  34.               deployDir="/tmp/war-deploy/"  
  35.               watchDir="/tmp/war-listen/"  
  36.               watchEnabled="false"/>  
  37.   
  38.     <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  39.     <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  40.   </Cluster>      
5、参数配置以及原理解释参考请见:


6、最后,要在要将web.xml文件<web-app>元素的最后加上: <distributable/>。一定要加上,原理待会再说。
将apache服务器以及两个tomcat服务器全部启动.

7、编写测试代码index.jsp放入webapps/balance目录

  1. <%@ page contentType="text/html; charset=GBK" %>    
  2.     <%@ page import="Java.util.*" %>    
  3.     <html><head><title>Cluster Test</title></head>    
  4.     <body>    
  5.     <%    
  6.       //HttpSession session = request.getSession(true);    
  7.       System.out.println(session.getId());    
  8.       out.println("<br> SESSION ID:" + session.getId()+"<br>");      
  9.       // 如果有新的请求,则添加session属性    
  10.       String name = request.getParameter("name");    
  11.       if (name != null && name.length() > 0) {    
  12.          String value = request.getParameter("value");    
  13.          session.setAttribute(name, value);    
  14.       }      
  15.         out.print("<b>Session List:</b>");      
  16.         Enumeration<String> names = session.getAttributeNames();    
  17.         while (names.hasMoreElements()) {    
  18.             String sname = names.nextElement();     
  19.             String value = session.getAttribute(sname).toString();    
  20.             out.println( sname + " = " + value+"<br>");    
  21.             System.out.println( sname + " = " + value);    
  22.        }    
  23.     %>    
  24.       <form action="index.jsp" method="post">    
  25.         名称:<input type=text size=20 name="name">    
  26.          <br>    
  27.         值:<input type=text size=20 name="value">    
  28.          <br>    
  29.         <input type=submit value="提交">    
  30.        </form>    
  31.     </body>    
  32.     </html>    

8、开始测试输入localhost/balance

本案用的是chrome浏览器(经测试ff也效果一样)

在界面中的表格元素中填入元素并提交,虽然sessionid 的 .a后缀表明该session来自服务器a,但你提交的写入session中的属性值仍然可以读出。

说明session已经成功发生了拷贝。


紧接着关闭服务器a 的服务,我们刷新页面,发现 a服务器的模拟宕机并不影响该session的访问。



 

linux
【内容导航】
第1页:粘性会话 第2页:复制会话
相关资讯       负载均衡 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2017/12/15 10:44:51
原理没看到