会话在创建后立即存储到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不变。