[Switch] redis realizes session sharing of tomcat cluster

1. Deploy two tomcat nodes and use nginx to implement the cluster (see http://www.cnblogs.com/zhangzhi0556/articles/nginx.html );

 
 
3. Build a simple web project, package it and publish it to each tomcat, to test whether the session is really shared (how to build a test project by yourself)
 
4. Download the open source component tomcat-redis-session-manager
Download method 1: https://github.com/jcoleman/tomcat-redis-session-manager/downloads   (this is early and does not support redis3.0 clusters)
 
Download method 2 (recommended): https://github.com/ran-jit/TomcatClusterRedisSessionManager  (supports redis3 cluster and tomat8)
It is best to download from the following address:
https://github.com/ran-jit/TomcatClusterRedisSessionManager/wiki/Tomcat-Cluster-Redis-Session-Manager
You can download the TomcatRedisSessionManager-1.0.zip package and decompress it to include the required jar package and configuration files. As follows:
commons-logging-1.2.jar
commons-pool2-2.4.1.jar
jedis-2.8.0.jar
commons-pool2-2.4.1.jar
commons-logging-1.2.jar
RedisDataCache.properties
 
5. Copy the above jar package to the lib directory of each tomcat, and copy RedisDataCache.properties to the conf directory of tomcat.
 
6. Configuration file modification
(1) Modify the RedisDataCache.properties configuration file as follows:
# redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....
redis.hosts=192.168.187.131:6379 (redis server IP and port number)
# Redis Password
redis.password= redis (password set in REDIS configuration file)
# set true to enable redis cluster mode
redis.cluster.enabled=false
 
(2) Add the following two lines to the tomcat/conf/context.xml file:
<Valve className="com.r.tomcat.session.management.RequestSessionHandlerValve"/>
 <Manager className="com.r.tomcat.session.management.RequestSessionManager"/>
 
(3) Set the session validity period in tomcat/conf/web.xml
    <session-config>
         <session-timeout>60<session-timeout>
    <session-config>
If the session validity period is also configured in the project, the project will prevail.
 
7. Start redis first, then start tomcat, and access the test project for testing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326769672&siteId=291194637