背景
当客户端访问Nginx服务器时,Nginx负载均衡会自动将请求转发到TomcatA或TomcatB服务器,以减轻Tomcat压力,从而达到Tomcat集群化部署,为了使各Tomcat之间共享同一个Session,将采用Redis缓存服务来集中管理Session存储。但是网上的方法基本都只支持tomcat7,于是记录下tomcat8的实现方法
环境
nginx: http://192.168.1.100:8080 tomcatA: http://192.168.1.100:9090 tomcatB: http://192.168.1.100:9091
步骤
- 下载3个需要的jar
commons-pool2-2.4.2.jar
jedis-2.9.0.jar
tomcat-redis-session-manager-8.5-master-2.0.0-8.5.40.jar
- 把下载的文件放到tomcat8/lib下面
- 修改tomcat8/conf/context.xml
vi tomcat8/context.xml # redis是哨兵模式 <Manager pathname=\"\" /> <Valve className=\"com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve\" /> <Manager className=\"com.orangefunction.tomcat.redissessions.RedisSessionManager\" maxInactiveInterval=\"60\" sentinelMaster=\"mymaster\" database=\"4\" password=\"redis-password\" sessionPersistPolicies=\"SAVE_ON_CHANGE\" sentinels=\"192.168.1.100:26379,192.168.1.101:26379,192.168.1.102:26379\"/> # redis是单点模式 <Manager pathname=\"\" /> <Valve className=\"com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve\" /> <Manager className=\"com.orangefunction.tomcat.redissessions.RedisSessionManager\" host=\"127.0.0.1\" port=\"6379\" database=\"0\" password=\"redis-password\" maxInactiveInterval=\"1200\"/> # 备注 host: redis服务器地址 port: redis服务器的端口号 database: 要使用的redis数据库索引 maxInactiveInterval: session最大空闲超时时间,如果不填则使用tomcat的超时时长,一般tomcat默认为1800 即半个小时 sessionPersistPolicies: session保存策略,除了默认的策略还可以选择的策略有: [SAVE_ON_CHANGE]:每次 session.setAttribute() 、 session.removeAttribute() 触发都会保存. 注意:此功能无法检测已经存在redis的特定属性的变化, 权衡:这种策略会略微降低会话的性能,任何改变都会保存到redis中. [ALWAYS_SAVE_AFTER_REQUEST]: 每一个request请求后都强制保存,无论是否检测到变化. 注意:对于更改一个已经存储在redis中的会话属性,该选项特别有用. 权衡:如果不是所有的request请求都要求改变会话属性的话不推荐使用,因为会增加并发竞争的情况。 sentinelMaster: redis集群主节点名称(Redis集群是以分片(Sharding)加主从的方式搭建,满足可扩展性的要求) sentinels: redis集群列表配置(类似zookeeper,通过多个Sentinel来提高系统的可用性)
- 新增测试页面
在tomcat8/webapps/ROOT/下面新增一个index.jsp
<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; %> <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"> <html> <head> <base href=\"<%=basePath%>\"> <title>My JSP \'index.jsp\' starting page</title> <meta http-equiv=\"pragma\" content=\"no-cache\"> <meta http-equiv=\"cache-control\" content=\"no-cache\"> <meta http-equiv=\"expires\" content=\"0\"> <meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\"> <meta http-equiv=\"description\" content=\"This is my page\"> <!-- <link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\"> --> </head> <body> SessionID:<%=session.getId()%> <BR> SessionIP:<%=request.getServerName()%> <BR> SessionPort:<%=request.getServerPort()%> <% out.println(\"This is Tomcat Server 22222\"); %> </body> </html>
- 重启tomcat
- 配置nginx
修改nginx的代理配置 upstream test { server 192.168.1.100:9090; server 192.168.1.100:9091; }
- 验证
访问http://192.168.1.100:8080,查看页面返回的session值
1.停掉tomcatA,再刷新http://192.168.1.100:8080查看session是否改变
2.启动tomcatA,再刷新http://192.168.1.100:8080查看session是否改变
3.停掉tomcatB,再刷新http://192.168.1.100:8080查看session是否改变