Skip to content

毛俊的博客

Tomcat集群session共享

tomcat1 min read

tomcat session复制

取消注释

1<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

这一行配置等同于下面的配置文件,开启一个即可,下面的配置可以修改配置信息

1<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
2 channelSendOptions="8">
1<Manager className="org.apache.catalina.ha.session.DeltaManager"
2 expireSessionsOnShutdown="false"
3 notifyListenersOnReplication="true"/>
4
5 <Channel className="org.apache.catalina.tribes.group.GroupChannel">
6 <Membership className="org.apache.catalina.tribes.membership.McastService"
7 address="228.0.0.4"
8 port="45564"
9 frequency="500"
10 dropTime="3000"/>
11 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
12 address="auto"
13 port="4000"
14 autoBind="100"
15 selectorTimeout="5000"
16 maxThreads="6"/>
17
18 <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
19 <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
20 </Sender>
21 <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
22 <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
23 </Channel>
24
25 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
26 filter=""/>
27 <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
28
29 <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
30 tempDir="/tmp/war-temp/"
31 deployDir="/tmp/war-deploy/"
32 watchDir="/tmp/war-listen/"
33 watchEnabled="false"/>
34
35 <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener">
36 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener">
37 </Cluster>
1修改上面的`address="auto"`为对应的集群地址。
2## tomcat redis存放session
3### tomcat-redis-session-manager
4刚开始使用的是这种方法,这个只支持tomcat7并且作者已经很久没有更新了。
5```xml
6<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
7<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
8 host="localhost" <!-- optional: defaults to "localhost" -->
9 port="6379" <!-- optional: defaults to "6379" -->
10 database="0" <!-- optional: defaults to "0" -->
11 maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
12 sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional -->
13 sentinelMaster="SentinelMasterName" <!-- optional -->
14 sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />

按上面的配置然后下载下面的jar包运行tomcat报class not found的问题,原因是jar包的路径名称已经修改了。

复制下面三个jar包到tomcat的lib目录下:

tomcat-redis-session-manager-VERSION.jar 下面这两个去maven仓库搜索下载jar jedis-2.5.2.jar commons-pool2-2.2.jar

redisson-tomcat

tomcat中启用Redisson前提:在lib下添加两个jar包 这个支持tomcat现有的版本(6,7,8,9)。

$catalina_home/conf/context.xml当中指定redisson:

1<!-- 单Redis Session共享管理 -->
2<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/conf/redission-single.json" readMode="REDIS" updateMode="DEFAULT"/>
3<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/conf/redission-single.json" readMode="MEMORY" updateMode="AFTER_REQUEST"/>
4<!-- 多Redis集群 Session共享管理 -->
5<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/conf/redission-cluster.json" readMode="REDIS" updateMode="DEFAULT"/>

创建Redisson配置信息 redission-single.json内容:单redis情况

1{
2 "singleServerConfig": {
3 "idleConnectionTimeout": 10000,
4 "pingTimeout": 1000,
5 "connectTimeout": 10000,
6 "timeout": 3000,
7 "retryAttempts": 3,
8 "retryInterval": 1500,
9 "password": null,
10 "subscriptionsPerConnection": 5,
11 "clientName": null,
12 "address": "redis://redis:6379",
13 "subscriptionConnectionMinimumIdleSize": 1,
14 "subscriptionConnectionPoolSize": 50,
15 "connectionMinimumIdleSize": 32,
16 "connectionPoolSize": 64,
17 "database": 0,
18 "dnsMonitoringInterval": 5000
19 },
20 "threads": 0,
21 "nettyThreads": 0,
22 "codec": {
23 "class": "org.redisson.codec.SerializationCodec"
24 },
25 "transportMode": "NIO"
26}

redission-cluster.json内容:多redis情况,只需配置从redis地址

1{
2 "clusterServersConfig": {
3 "idleConnectionTimeout": 10000,
4 "pingTimeout": 1000,
5 "connectTimeout": 10000,
6 "timeout": 3000,
7 "retryAttempts": 3,
8 "retryInterval": 1500,
9 "failedSlaveReconnectionInterval": 3000,
10 "failedSlaveCheckInterval": 60000,
11 "password": null,
12 "subscriptionsPerConnection": 5,
13 "clientName": null,
14 "loadBalancer": {
15 "class": "org.redisson.connection.balancer.RoundRobinLoadBalancer"
16 },
17 "subscriptionConnectionMinimumIdleSize": 1,
18 "subscriptionConnectionPoolSize": 50,
19 "slaveConnectionMinimumIdleSize": 32,
20 "slaveConnectionPoolSize": 64,
21 "masterConnectionMinimumIdleSize": 32,
22 "masterConnectionPoolSize": 64,
23 "readMode": "SLAVE",
24 "subscriptionMode": "SLAVE",
25 "nodeAddresses": [
26 "redis://127.0.0.1:7004",
27 "redis://127.0.0.1:7001",
28 "redis://127.0.0.1:7000"
29 ],
30 "scanInterval": 1000,
31 "pingConnectionInterval": 0,
32 "keepAlive": false,
33 "tcpNoDelay": false
34 },
35 "threads": 0,
36 "nettyThreads": 0,
37 "codec": {
38 "class": "org.redisson.codec.SerializationCodec"
39 },
40 "transportMode": "NIO"
41}

实际存储的类型是hash,key类似redisson:tomcat_session:1CF4CF29EADCB3695FDB8E183B69A523

(完)

© 2020 by 毛俊的博客. All rights reserved.
Theme by LekoArts