https://github.com/magro/memcached-session-manager
3.1将以上文件复制到tomcat的lib目录下
配置tomcat context.xml
在每台tomcat 都配置context.xml文件并在<Context>节点下增加如下配置:(两种模式二选一,推荐非黏性模式)
non-sticky模式(非黏性): tomcat session 为 中转session, memcached1 为主 sessionmemcached 2 为备session
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" <span style="white-space:pre"> </span>memcachedNodes="n1:192.168.150.135:11211,n2:192.168.150.136:11211" <span style="white-space:pre"> </span>requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$" <span style="white-space:pre"> </span>sessionBackupAsync="false" <span style="white-space:pre"> </span>sessionBackupTimeout="100" <span style="white-space:pre"> </span>transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" <span style="white-space:pre"> </span>copyCollectionsForSerialization="false"/> <Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="<span style="font-family: Arial, Helvetica, sans-serif;">n1:192.168.150.135:11211,n2:192.168.150.136:11211</span><span style="font-family: Arial, Helvetica, sans-serif;">"</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"> </span> failoverNodes="n2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
将以下jsp页面放置到每台tomcat容器下,使用nginx做负载访问
<%@ page contentType="text/html; charset=utf-8" %> <%@ page import="java.util.*" %> <html><head><title>Cluster App Test</title></head> <body> Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%> <% out.println("<br> ID " + session.getId()+"<br>"); // 如果有新的 Session 属性设置 String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print("<b>Session 列表</b>"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+"<br>"); System.out.println( name + " = " + value); } %> <form action="test.jsp" method="POST"> 名称:<input type=text size=20 name="dataName"> <br> 值:<input type=text size=20 name="dataValue"> <br> <input type=submit> </form> </body> </html>
ps:nginx做负载请参考我的文章:
http://blog.csdn.net/caicongyang/article/details/46388845
http://blog.csdn.net/caicongyang/article/details/46444007
我的个人网站:http://www.caicongyang.com
我的CSDN博客地址: http://blog.csdn.net/caicongyang