"E-commerce" nginx+tomcat+memcached realize session sharing cluster

Source: Internet
Author: User
Tags serialization

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
  1. <Manager classname="De.javakaffee.web.msm.MemcachedBackupSessionManager"
  2. memcachednodes="n1:localhost:11211,n2:localhost:11212"
  3. sticky="false"
  4. sessionbackupasync="false"
  5. lockingmode="Uripattern:/path1|/path2"
  6. requesturiignorepattern=". *\. ( ICO|PNG|GIF|JPG|CSS|JS) $ "
  7. transcoderfactoryclass="De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  8. />
<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
    1. upstream cluster_memcached {  
    2.          server localhost:7181;&NBSP;&NBSP;
    3.          server localhost:7182;&NBSP;&NBSP;
    4.    }  
    5.    server {  
    6.         listen 7180;&NBSP;&NBSP;
    7.         server_name localhost;  
    8.         location / {  
    9.           Proxy_pass http://cluster_memcached;  
    10.         }  
    11.    }  
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
  1. <%@ page import="java.util.Enumeration"%>
  2. <%@ page contenttype="text/html;charset=utf-8" language="java"%>
  3. <title>session test</title>
  4. <%
  5. String key = Request.getparameter ("SessionKey");
  6. String value = Request.getparameter ("Sessionvalue");
  7. if (key! = NULL &&! "". Equalsignorecase (Key.trim ())) {
  8. Session.setattribute (key, value);
  9. }
  10. %>
  11. <body>
  12. <form action="session.jsp" method="POST" >
  13. <input type="text" name="SessionKey" value="Key1" ><input type="text" Name=" Sessionvalue "value="value1 ">
  14. <input type="Submit" value="Submission" >
  15. </form>
  16. "/")%>
  17. <table>
  18. <tr>
  19. <th>key</th>
  20. <th>value</th>
  21. </tr>
  22. <%
  23. enumeration<string> keys = Session.getattributenames ();
  24. while (Keys.hasmoreelements ()) {
  25. String k = Keys.nextelement ();
  26. String v = (string) session.getattribute (k);
  27. %>
  28. <tr>
  29. <td><%=k%></td>
  30. <td><%=v%></td>
  31. </tr>
  32. <%
  33. }
  34. %>
  35. </table>
  36. </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.