Apache+Tomcat负载均衡两种session共享方式的设置之二复制会话

本文的测试环境为windows xp

参考文档:

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

基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置

Tomcat集群Cluster实现原理剖析

以下设置和Apache+Tomcat负载均衡两种session共享方式的设置之一粘性会话 一文中的粘性会话设置基本一样

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 -->
        < Connector port ="8009"
                enableLookups
="false" redirectPort ="8443" protocol ="AJP/1.3" />
其中的port为前面<Proxy>中设定的端口,还要配置其route,代码如下:
<!-- Define the top level container in our container hierarchy -->
        < Engine name ="Catalina" defaultHost ="localhost" jvmRoute ="a" >

jvmRoute也须同前面的设置一样。

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

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

5、参数配置以及原理解释参考请见:


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

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

<%@ page contentType="text/html; charset=GBK" %>  
    <%@ page import="java.util.*" %>  
    <html><head><title>Cluster Test</title></head>  
    <body>  
    <%  
      //HttpSession session = request.getSession(true);  
      System.out.println(session.getId());  
      out.println("<br> SESSION ID:" + session.getId()+"<br>");    
      // 如果有新的请求,则添加session属性  
      String name = request.getParameter("name");  
      if (name != null && name.length() > 0) {  
         String value = request.getParameter("value");  
         session.setAttribute(name, value);  
      }    
        out.print("<b>Session List:</b>");    
        Enumeration<String> names = session.getAttributeNames();  
        while (names.hasMoreElements()) {  
            String sname = names.nextElement();   
            String value = session.getAttribute(sname).toString();  
            out.println( sname + " = " + value+"<br>");  
            System.out.println( sname + " = " + value);  
       }  
    %>  
      <form action="index.jsp" method="post">  
        名称:<input type=text size=20 name="name">  
         <br>  
        值:<input type=text size=20 name="value">  
         <br>  
        <input type=submit value="提交">  
       </form>  
    </body>  
    </html>  

8、开始测试输入localhost/balance

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

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

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


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
















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值