會話在創建後立即存儲到Redis中以供其他服務器使用。 會話直接從Redis加載(但是在同一請求上下文期間對會話的後續請求將返回一個ThreadLocal緩存,而不是多次擊中Redis。)爲了儘可能地防止衝突(和丟失的寫入),會話數據 僅在會話已修改的情況下在Redis中更新。
管理器依賴於Redis的本地過期能力來使自動會話期滿的密鑰過期,以避免不斷地搜索會話的過期會話的整個列表的開銷。存儲在會話中的數據必須是可序列化的。
該版本tomcat-redis-session-manager-2.0.0用於jdk1.7及以上,如果需要低版本的jdk6的包,可以去github下載源碼包自行編譯。
tomcat-redis-session-manager-2.0.0
service | ip | jdk | version |
---|---|---|---|
tomcat1 | 172.18.3.183 | 1.8.0_73 | 7.0.72 |
tomcat2 | 172.18.3.187 | 1.8.0.73 | 7.0.72 |
nginx | 172.18.3.181 | 1.6.3 | |
redis | 172.18.3.186 | 3.0.0 |
nginx.conf配置:
upstream backend {
server 172.18.3.183:8080 max_fails=1 fail_timeout=10s;
server 172.18.3.187:8080 max_fails=1 fail_timeout=10s;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://backend;
}
tomcat配置:context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="172.18.3.186"
port="6379"
database="0"
password="xinwei"
maxInactiveInterval="60" />
</Context>
在tomcat/webapps/test放一個index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
sessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<%
//爲了區分,第二個可以是222
out.println("This is Tomcat Server 1111");
%>
redis配置 (只有加密時用到,不加密可以直接使用redis)
在redis.conf 配置增加
requirepass xinwei 密碼要和tomcat中配置的相同、
至此,就大功告成了。通過nginx訪問test工程,刷新後,會看到tomcat輪詢出現(顯示1111和2222),但是sessionID不變。