Tomcat6.x+memcached集群session管理(memcached-session-manager)

1.开源项目地址:

https://github.com/magro/memcached-session-manager

2.所需jar文件列表

Tomcat6.x+memcached集群session管理(memcached-session-manager)_第1张图片

3.配置

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>  

sticky模式(黏性):  tomcat session    session  memcached  为备  session

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

4.测试

将以下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>  

结果:我们可以看到通过nginx访问到两台tomcat上的页面,但是session确是相同的



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 





你可能感兴趣的:(Tomcat6.x+memcached集群session管理(memcached-session-manager))