In the distributed multi-tomcat Web cluster environment, the first thing to solve is the session sharing problem, the general implementation ideas are:
(1) Session replication: Session synchronization between multiple Tomcat, Tomcat in the cluster stores the same session information
(2) Shared session storage: The session is stored centrally in the same place, such as redis/memcached, or even db. The session is not stored in Tomcat's own memory
(3) session Sticky: In fact, the session stickiness is not a session-sharing scheme, but a multi-tomcat cluster scheme, with Nginx/apache implementation, the same user request to the same Tomcat node, the multi-node does not share the Sesison. The advantage is that the implementation is simple, the disadvantage is the node failure, you will lose part of the session. It is generally used in the first two ways.
This document records the entire process of using the Memcached-session-manager component to build nginx+tomcat+memcached to achieve a shared storage environment for the cluster and session.
nginx:1.8.0
Tomcat Two: 6.0.44
memcached cluster: 1.4.4
Memcached-session-manager provides a variety of session data serialization strategies:
(1) Java Serialization: requires the information to be stored to implement the Java.io.Serializable interface;
(2) Serialization of Kryo
(3) Javolution
(4) XStream
(5) Flexjson
See also: http://code.google.com/p/memcached-session-manager/wiki/SerializationStrategies
The serialization policy used here is Kryo
(1) Place all of the following dependent jar packages in $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) Two Tomcat configuration conf/context.xml, respectively, added:
XML code
- <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 " />
The strategy used here is that the session is non-sticky and memcached uses two nodes of a cluster
Also, configure two Tomcat HTTP listener ports of 7181 and 7182, respectively
(3) Nginx configuration:
Java code
- upstream cluster_memcached {
- server localhost:7181;&NBSP;&NBSP;
- server localhost:7182;&NBSP;&NBSP;
- }
- server {
- listen 7180;&NBSP;&NBSP;
- 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; } }
Here the Nginx listener port is configured to be 7190, and all requests are reversed to proxy to the 7181 and 7182 tomcat clusters.
(4) Test code session.jsp:
JSP code
- <%@ page import="java.util.Enumeration"%>
- <%@ page contenttype="text/html;charset=utf-8" language="java"%>
- <title>session test</title>
- <%
- String key = Request.getparameter ("SessionKey");
- String value = Request.getparameter ("Sessionvalue");
- if (key! = NULL &&! "". Equalsignorecase (Key.trim ())) {
- Session.setattribute (key, value);
- }
- %>
- <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="Submission" >
- </form>
- "/")%>
- <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>
<%@ page import= "java.util.Enumeration"%><%@ page contenttype= "Text/html;charset=utf-8" language= "java"% >Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5
"Java Framework source code download"
"E-commerce" nginx+tomcat+memcached realize session sharing cluster