標籤:
本文記錄nginx+redis+tomcat實現session共用的過程
tomcat-redis-session-manager組件:https://github.com/jcoleman/tomcat-redis-session-manager
tomcat-redis-sessoin-manager對tomcat的支援:
(1) tomcat6:支援,要求jdk為1.6, 需要引入tomcat-redis-session-manager-1.2-tomcat-6.jar,不過現在已經處理不維護的狀態
(2) tomcat7:支援,jdk可以是1.6或1.7,需要引入tomcat-redis-session-manager-1.2-tomcat-7.jar; 如果引入的是tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar則僅支援JDK1.7
(3) tomcat8:目前不支援, 可以嘗試一下https://github.com/chexagon/redis-session-manager(未實際驗證)
組件下載:https://github.com/jcoleman/tomcat-redis-session-manager/downloads
使用組件版本
nginx:1.8.0
tomcat兩個:6.0.44
redis:2.4.10
JDK:1.6.0_34
(1) 將下面所有依賴的這些jar包放在$TOMCAT_HOME/lib中:
jedis-2.0.0.jar tomcat-redis-session-manager-1.2-tomcat-6.jar commons-pool-1.5.5.jar
(2) 兩個tomcat分別配置conf/context.xml,增加:
Xml代碼
- <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
- <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
- host="localhost"
- port="6379"
- database="0"
- maxInactiveInterval="60" />
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /><Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
另外,分別配置兩個tomcat的HTTP監聽連接埠為6181和6182
(3) nginx配置:
Java代碼
- 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; } }
這裡配置nginx監聽連接埠為6180,並請所有的請求反向 Proxy到6181和6182的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>
需要注意的是,tomcat-redis-session-manager組件並不提供redis叢集的支援。對該問題,作者的解釋是推薦使用twitter的twemproxy或者碗豆莢的codis之類的redis代理服務。
java企業級通用許可權安全架構源碼 SpringMVC mybatis or hibernate+ehcache shiro druid bootstrap HTML5
【java架構源碼下載】
【電商】nginx+redis+tomcat實現session共用叢集