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
- <Valve classname="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
- <Manager classname="Com.radiadesign.catalina.session.RedisSessionManager"
- host="localhost"
- port="6379"
- database="0"
- 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
- Upstream Cluster_redis {
- Server localhost:6181;
- Server localhost:6182;
- }
- server {
- Listen 6180;
- server_name localhost;
- Location/{
- Proxy_pass http://cluster_memcached;
- }
- }
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
- <%@ 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"% > 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