以下设置和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.so2、然后在此配置文件末端加入以下代码(该处配置与前文有些差异):
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 -->
其中的port为前面<Proxy>中设定的端口,还要配置其route,代码如下:
<!--Define the top level container in our container hierarchy-->
jvmRoute也须同前面的设置一样。 【Linux公社 http://www.linuxidc.com 】
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>

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的访问。

