tomcat+redis实现会话保持
需求描述
项目组反馈原nignx负载采用ip hash,实现了会话保持但无法做到负载均衡,导致某台应用服务器负载过高。
现需取消ip hash的负载方式,改用redis实现会话保持
改造方案
-
采用三节点redis哨兵模式
-
改造tomcat
1
2
3
4
|
commons-pool2-2.2.jar
jedis-2.8.2.jar
tomcat-redis-session-manager.jar
放入tomcat/lib中
|
单机redis:
1
2
3
4
5
6
7
8
9
|
# vim tomcat/conf/context.xml
<Valve className="com.seejoke.tomcat.redissessions.RedisSessionHandlerValve"/>
<Manager className="com.seejoke.tomcat.redissessions.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
password="123456"
maxInactiveInterval="1800" />
|
集群redis:
1
2
3
4
5
6
7
|
<Valve className="com.seejoke.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.seejoke.tomcat.redissessions.RedisSessionManager"
sentinelMaster="mymaster"
sentinels="172.19.80.3:26379,172.19.80.4:26379,172.19.80.5:26379"
database="2"
password="123456"
maxInactiveInterval="3600" />
|
重启tomcat后即可实现session在tomcat之间的共享
参考链接:https://gitee.com/diaodiaofly/tomcat-redis-session-manager/tree/master