在分布式多tomcat web集群环境下,首先要解决的是session的共享问题,一般的实现思路有:
(1) session复制:多tomcat之间进行session的同步,集群中的tomcat存储相同的session信息
(2) 共享session存储:将session集中存储在同一个地方,如redis/memcached,甚至DB。tomcat本身的内存中并不存储session
(3) session粘性:其实session粘性并不是session共享的方案,而是多tomcat的集群方案,配合nginx/apache实现,将同一用户请求转向到相同的tomcat结点,多结点之间并不共享sesison。优点是实现简单,缺点就是结点故障时,会丢失部分session。一般会配合前两种方式使用。
本文记录使用memcached-session-manager组件搭建nginx+tomcat+memcached实现集群和session共享存储环境的全部过程。
nginx:1.8.0
tomcat两个:6.0.44
memcached集群:1.4.4
memcached-session-manager提供了多种session数据序列化策略:
(1) java序列化:要求待存储的信息实现java.io.Serializable接口;
(2) kryo序列化
(3) javolution
(4) xstream
(5) flexjson
参见:http://code.google.com/p/memcached-session-manager/wiki/SerializationStrategies
这里的使用的序列化策略为kryo
(1) 将下面所有依赖的这些jar包放在$TOMCAT_HOME/lib中:
asm-3.2.jar kryo-1.04.jar kryo-serializers-0.11.jar memcached-session-manager-1.8.3.jar memcached-session-manager-tc6-1.8.3.jar minlog-1.2.jar msm-kryo-serializer-1.8.3.jar reflectasm-1.01.jar spymemcached-2.11.1.jar
(2) 两个tomcat分别配置conf/context.xml,增加:
Xml代码
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:localhost:11211,n2:localhost:11212"
- sticky="false"
- sessionBackupAsync="false"
- lockingMode="uriPattern:/path1|/path2"
- requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
- />
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211,n2:localhost:11212" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
这里使用的策略是session非粘性,memcached使用两个结点的集群
另外,分别配置两个tomcat的HTTP监听端口为7181和7182
(3) nginx配置:
Java代码
- upstream cluster_memcached {
- server localhost:7181;
- server localhost:7182;
- }
- server {
- listen 7180;
- server_name localhost;
- location / {
- proxy_pass http://cluster_memcached;
- }
- }
upstream cluster_memcached { server localhost:7181; server localhost:7182; } server { listen 7180; server_name localhost; location / { proxy_pass http://cluster_memcached; } }
这里配置nginx监听端口为7190,并请所有的请求反向代理到7181和7182的tomcat集群.
(4) 测试代码session.jsp:
Jsp代码
- <%@ page import="java.util.Enumeration" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>session test</title>
- <%
- String key = request.getParameter("sessionKey");
- String value = request.getParameter("sessionValue");
- if(key != null && !"".equalsIgnoreCase(key.trim())) {
- session.setAttribute(key, value);
- }
- %>
- </head>
- <body>
- <form action="session.jsp" method="post">
- <input type="text" name="sessionKey" value="key1"><input type="text" name="sessionValue" value="value1">
- <input type="submit" value="提交">
- </form>
- <h4>已经存储的session--<%=session.getServletContext().getRealPath("/")%></h4>
- <table>
- <tr>
- <th>key</th>
- <th>value</th>
- </tr>
- <%
- Enumeration<String> keys = session.getAttributeNames();
- while(keys.hasMoreElements()){
- String k = keys.nextElement();
- String v = (String)session.getAttribute(k);
- %>
- <tr>
- <td><%=k%></td>
- <td><%=v%></td>
- </tr>
- <%
- }
- %>
- </table>
- </body>
- </html>
<%@ page import="java.util.Enumeration" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>session test</title> <% String key = request.getParameter("sessionKey"); String value = request.getParameter("sessionValue"); if(key != null && !"".equalsIgnoreCase(key.trim())) { session.setAttribute(key, value); } %> </head> <body> <form action="session.jsp" method="post"> <input type="text" name="sessionKey" value="key1"><input type="text" name="sessionValue" value="value1"> <input type="submit" value="提交"> </form> <h4>已经存储的session--<%=session.getServletContext().getRealPath("/")%></h4> <table> <tr> <th>key</th> <th>value</th> </tr> <% Enumeration<String> keys = session.getAttributeNames(); while(keys.hasMoreElements()){ String k = keys.nextElement(); String v = (String)session.getAttribute(k); %> <tr> <td><%=k%></td> <td><%=v%></td> </tr> <% } %> </table> </body> </html>
java企业级通用权限安全框架源码 SpringMVC mybatis or hibernate+ehcache shiro druid bootstrap HTML5