使用redis实现tomcat8的session共享
1.下载jar包redisson-all-3.9.1.jar、redisson-tomcat-8-3.9.1.jar
可以从阿里云maven仓里面下载:https://maven.aliyun.com/mvn/search
2.将redisson-all-3.9.1.jar、redisson-tomcat-8-3.9.1.jar导入tomcat lib目录下
3.创建redisson.conf文件,并将文件放置tomcat conf目录下(redisson.conf文件名可以随意命名,只要跟下面context.xml保持一致即可)具体配置内容如下(粗体字体部分为修改内容):
(1)单节点配置
{
"singleServerConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"password":"##########",
"subscriptionsPerConnection":5,
"clientName":null,
"address": "redis://127.0.0.1:6379",
"subscriptionConnectionMinimumIdleSize":1,
"subscriptionConnectionPoolSize":50,
"connectionMinimumIdleSize":32,
"connectionPoolSize":200,
"database":0,
"dnsMonitoringInterval":5000
},
"threads":0,
"nettyThreads":0,
"codec":{
"class":"org.redisson.codec.JsonJacksonCodec"
},
"transportMode":"NIO"
}
注释:
address(节点地址):可以通过host:port的格式来指定节点地址。
database(数据库编号):默认值:0,尝试连接的数据库编号。
password(密码):默认值:null,用于节点身份验证的密码。
(2)主从配置
{
"masterSlaveServersConfig":{
"idleConnectionTimeout":10000,
"pingTimeout":1000,
"connectTimeout":10000,
"timeout":3000,
"retryAttempts":3,
"retryInterval":1500,
"reconnectionTimeout":3000,
"failedAttempts":3,
"password":null,
"subscriptionsPerConnection":5,
"clientName":null,
"loadBalancer":{
"class":"org.redisson.connection.balancer.RoundRobinLoadBalancer" },
"slaveSubscriptionConnectionMinimumIdleSize":1,
"slaveSubscriptionConnectionPoolSize":50,
"slaveConnectionMinimumIdleSize":32,
"slaveConnectionPoolSize":64,
"masterConnectionMinimumIdleSize":32,
"masterConnectionPoolSize":64,
"readMode":"SLAVE",
"slaveAddresses":[
"redis://127.0.0.1:6381",
"redis://127.0.0.1:6380"
],
"masterAddress": "redis://127.0.0.1:6379",
"database":0
},
"threads":0,
"nettyThreads":0,
"codec":{
"class":"org.redisson.codec.JsonJacksonCodec" },
"transportMode":"NIO"}
注释:
masterAddress(主节点地址):可以通过host:port的格式来指定主节点地址。
addSlaveAddress(添加从主节点地址):可以通过host:port的格式来指定从节点的地址。多个节点可以一次性批量添加。
database(数据库编号):默认值:0,尝试连接的数据库编号。
password(密码):默认值:null,用于节点身份验证的密码。
4.修改tomcat context.xml文件添加内容如下:
<Manager className="org.redisson.tomcat.RedissonSessionManager"
configPath="${catalina.base}/conf/redisson.conf"
readMode="REDIS" updateMode="DEFAULT" broadcastSessionEvents="false"/>