不同tomcat服务器,使用公用的redis来存储session信息,实现session共享。
1.修改tomcat下 tomcat/conf/context.xml 文件,在配置中增加:
<Manager className="org.redisson.tomcat.RedissonSessionManager"
configPath="${catalina.base}/conf/redisson.conf"
readMode="REDIS" updateMode="DEFAULT" broadcastSessionEvents="false"/>
readMode
- read Session attributes mode. Two modes are available:
MEMORY
- stores attributes into local Tomcat Session and Redis. Further Session updates propagated to local Tomcat Session using Redis-based events.REDIS
- stores attributes into Redis only. Default mode.
broadcastSessionEvents
- if true
then sessionCreated
and sessionDestroyed
events are broadcasted across all Tomcat instances and cause all registered HttpSessionListeners to be triggered. Default is false
.
updateMode
- Session attributes update mode. Two modes are available:
DEFAULT
- session attributes are stored into Redis only through setAttribute method. Default mode.AFTER_REQUEST
- all session attributes are stored into Redis after each request.
configPath
- path to Redisson JSON or YAML config.
2.在 tomcat/conf/ 目录下创建 redisson.conf 配置文件,内容如下:
{
"singleServerConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"password":"redisPass",
"subscriptionsPerConnection":5,
"clientName":null,
"address": "redis://IP:PORT",
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"connectionMinimumIdleSize":32,
"connectionPoolSize":64,
"database":0,
"dnsMonitoringInterval":5000
},
"threads":0,
"nettyThreads":0,
"codec":{
"class":"org.redisson.codec.JsonJacksonCodec"
},
"transportMode":"NIO"
}
更换其中的redis的服务器IP及端口PORT以及redis的密码password即可。
参考地址:
https://github.com/redisson/redisson/wiki/2.-Configuration
3.下载redisson-tomcat插件对应的jar文件,放入tomcat的lib目录下:
插件地址:
https://github.com/redisson/redisson/tree/master/redisson-tomcat
结尾处可下载jar文件
4.redis的配置和启动参考:
https://blog.csdn.net/ajian132/article/details/103505701
5.对所有的tomcat都进行如上配置,启动即可实现session共享。