使用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文- p # 3件名可以随T ? : % @ v C意命名,只| T e / # Z q E要跟下面context.xml保持一致即可)具体配置内容如下(粗体字体部分为修改内容):

(1)单节点配置

{

"sinf Y U [ QgleServerConfid q zg":{

"idleConnectionTimeout":10000,

"pingTimeout":1000,

"connectTimeout":10000,

"timeout":3000,

"retryAttempts":3,

"retryInterva, ; ) s dl":1500,

"password":"##########",

"subscriptionsPerConnection [ *":5,

"clientName":null,

"address": "redis://127x B 5 E 7 a !.0.0.1:6379",

"subscriptionConnectionMinimumIdleSize":1,

"subscriptionConnectionPoolSizeM X $ V w u ? u p":50,

"connectionMinimumIdleSize":32,

"conF N C c T w y 9nectionPoolSize":200,

"database":0,

"dnsMonitort k u c ;ingInterval":5000

},

"threads":i l [ 50,

"nettyThreads":0,

"codec"Q s f W c M b 8 %:{

"class":"org.rE p 8 @ & m }edisson.codec.JsonJa& : f M R LcksonCodec"

},

"transportMode":"NIO"

}

注释:

address(节点地址):可以通过host* ` % n:port的格式来指定节点地址。

database(数据库编号):默认值:0,尝试连接的数据库编号。

password(密7 x C 4 ]码):默认值:null,用于节点Z ( Q 6身份验证的密码。

(2)主从配置

{

": & DmasterSlaveServersConf$ $ -ig":{

"idleConnectionTimeout":10000,

"pingTimeout":1000,

"connectTimeout":10000,

"time] = - : Aout":3000,

"retryAttempts":3,

"retryInterval":1500,

"rec8 I 0 ! QonnectionTimeout":3000,

"failedAttempts":3,

"password":null,

"sg o QubscriptionsPerConnection":5,

"cli1 3 L ! 8 {entName":null,

V u 6 o ! E h $ $"loadBalancer":{

"class":"orgt . 7 C p W P e.redisson.connection.balancer.RoundRobe p pinLoadN X { M } .Balancer" },

"slaveSubscriptionConnectionMinimt ~ K AumIdleSize":1,

"slaveSubH y : H !scriptionConnectionPoolSize":r S j u ` l J / 750,

"slaveConnectionMinimumIdleSize":32,

"s` M l o ] AlaveConnecti[ N ) x 9onPoolSize":64,

"masterConnectionMinimumIdleSizez k H l U j =":32,

"mad 5 % B } W m -sterConnectionPoolSize":64,

"readMode":"SLAVE",

"slaveAddresses":[

"redis://127.0.0.1:6381",

"redis://127.0.0.1v Q Y:6380"

],

"masterAddresso J y": "redis://127.0.0.1:6379",

"database":0

},

"threads":0,

"nettyThreads":0,

"codec":{

"class":"B e b C borg.redisson.codec.JsonJacksonCodec" },

"transportMoa _ e % Ade":A = { !"NIO"}

注释:

masterAddress(主节点地址):可以通过host:port的格式来指定主节点地址。

addSlaveAddress(添加从主节点地址):可以通过host:p; ! _ort的格式来指定从节点的地址。多个节点可以一次性批量添加。

database(数据库编号G Z 1 $ x b 4 j):H { X + e ^ d T N默认值:0,尝试连接的数据库编号。

pd k aassword(密码):默认值:{ Y 7 Hnull,用于节点身份验证的m K d Y t R y x密码。

4.修i v L { C a F q改tomcat context.xml文件添加内容如k n D L O %下:

<ManaQ & | t Eger className="org.redisson.tomcad 8 8 @ = Kt.RedissonSessionManager"

configPath=f * R A I"${catalina.base}/conf/redissoz [ e ? 5 G G A Ln.conf"

readMode="REDIS" updateMode="DEM C h ^ kFAULT" broadcastSessionEvents="false"/>

使用redis实现tomcat8的session共享