"E-commerce" Nginx+redis+tomcat realize session sharing cluster

Source: Internet
Author: User

This document records the process of Nginx+redis+tomcat implementing session sharing.

Tomcat-redis-session-manager components:https://github.com/jcoleman/tomcat-redis-session-manager

Tomcat-redis-sessoin-manager Support for Tomcat:

(1) Tomcat6: Support, require JDK 1.6, need to introduce Tomcat-redis-session-manager-1.2-tomcat-6.jar, but now deal with the state of non-maintenance

(2) TOMCAT7: Support, JDK can be 1.6 or 1.7, need to introduce tomcat-redis-session-manager-1.2-tomcat-7.jar; If Tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar is introduced, it only supports JDK1.7

(3) TOMCAT8: Currently not supported, you can try Https://github.com/chexagon/redis-session-manager (not actually verified)

Component Downloads: https://github.com/jcoleman/tomcat-redis-session-manager/downloads

Working with component versions

nginx:1.8.0

Tomcat Two: 6.0.44

redis:2.4.10

Jdk:1.6.0_34

(1) Place all of the following dependent jar packages in $tomcat_home/lib:

Jedis-2.0.0.jar Tomcat-redis-session-manager-1.2-tomcat-6.jar Commons-pool-1.5.5.jar

(2) Two Tomcat configuration conf/context.xml, respectively, added:

XML code
  1. <Valve classname="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  2. <Manager classname="Com.radiadesign.catalina.session.RedisSessionManager"
  3. host="localhost"
  4. port="6379"
  5. database="0"
  6. maxinactiveinterval=" /> "
<valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/><manager classname= " Com.radiadesign.catalina.session.RedisSessionManager "         host=" localhost "          port=" 6379 "         database=" 0 "          maxinactiveinterval= "/>"

Also, configure two Tomcat HTTP listener ports of 6181 and 6182, respectively

(3) Nginx configuration:

Java code
  1. Upstream Cluster_redis {
  2. Server localhost:6181;
  3. Server localhost:6182;
  4. }
  5. server {
  6. Listen 6180;
  7. server_name localhost;
  8. Location/{
  9. Proxy_pass http://cluster_memcached;
  10. }
  11. }
Upstream Cluster_redis {          server localhost:6181;          Server localhost:6182;     }     server {         listen 6180;         server_name localhost;         Location/{           proxy_pass http://cluster_memcached;         }     }  

Here the Nginx listener port is configured to be 6180, and all requests are reversed to proxy to the 6181 and 6182 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"% > 

It is important to note that the Tomcat-redis-session-manager component does not provide support for Redis clusters. The author's explanation for the problem is to recommend using a REDIS proxy service such as Twitter's twemproxy or bowl pod Codis.

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+redis+tomcat 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.