Tomcat Clustering Redis Session Manager
The Redis session manager is pluggable one. It stores session into Redis for easy distribution of HTTP Requests across a cluster of Tomcat servers.
Here the Sessions are implemented as non-sticky (means, each request can able to go to any server in the cluster, unlike the Apache provided Tomcat clustering setup.)
Request Sessions will be stored into Redis immediately (Session attributes must be Serializable), for the use of other servers. When tomcat receives a request from the client, Sessions are loaded directly from Redis.
Supports Redis default, sentinel and cluster mode, based on the configuration.
Going forward, we no need to enable sticky session (JSESSIONID) in Load Balancer.
Supports:
- Apache Tomcat 7
- Apache Tomcat 8
- Apache Tomcat 9
- Apache Tomcat 10
Downloads:
Maven configuration
<repository>
<id>repsy</id>
<name>tomcat-cluster-redis-session-manager-repo</name>
<url>https://repo.repsy.io/mvn/ranmanic/tomcat-session-manager</url>
</repository>
<dependency>
<groupId>tomcat-session-manager</groupId>
<artifactId>redis</artifactId>
<version>4.0</version>
</dependency>
Pre-requisite:
- jedis.jar
- commons-pool2.jar
- commons-logging.jar
more details.. https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki
Steps to be done,
- Copy the downloaded jars to your tomcat/lib directory.
tomcat/lib/
- Add tomcat system property "catalina.base".
catalina.base="TOMCAT_LOCATION"
example: env "catalina.base=/opt/tomcat" bash
- Copy the redis-data-cache.properties file to your tomcat/conf directory and update your Redis server details.
tomcat/conf/redis-data-cache.properties
- Add the below two lines in your tomcat/conf/context.xml file.
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />
- Verify the session expiration time in tomcat/conf/web.xml file.
<session-config>
<session-timeout>60</session-timeout>
</session-config>
Note:
- All your session attribute values must implement java.io.Serializable.
Configuration Properties:
Property | Description |
---|---|
redis.hosts | Redis server running instance IP address and port number - ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, .. - default: 127.0.0.1:6379 |
redis.password | Redis protected password |
redis.database | Redis database selection. (Numeric value) - default: 0 |
redis.timeout | Redis connection timeout - default: 2000 ms |
redis.cluster.enabled | To enable redis cluster mode - default: false - supported values: true/false |
redis.sentinel.enabled | To enable redis sentinel mode - default: false - supported values: true/false |
redis.sentinel.master | Redis sentinel master name - default: mymaster |
lb.sticky-session.enabled | To enable redis and standard session mode If enabled,
|
session.persistent.policies | session persistent policies. - policies - DEFAULT, SAVE_ON_CHANGE, ALWAYS_SAVE_AFTER_REQUEST
|
redis.sso.timeout | single-sign-on session timeout. - default: 0 ms (-no expiry) |