K 的博客

记录精彩的程序人生 开始使用

Nginx + Tomcat + Redis 集群 Session 管理



  1. upstream tomcat  {  
  2.         server localhost:8080;  
  3.         server localhost:8081;  
  4.     }  
  5. location / {  
  6.     proxy_pass  http://tomcat;  
  7.   }  

我们需要配置 Tomcat,让 Tomcat 把会话 Session 保存到 Redis 数据库。

我们要使用 tomcat-redis-session-manager 这样的第三方库,主页见:


要注意此库并非开箱即用的,使用时需要做一些调整。你需要下载源码,并在更新了依赖库的版本后,重建项目。比如我使用了 commons-pool2-2.2.jar 和 jedis-2.6.1.jar 依赖库。要记住把这些 jar 文件复制到每一个 Tomcat 实例的 lib 子目录下。

在更新了 commons-pool、jedis 和 tomcat 版本这些库后,你可以使用 build.gradle 来构建整个项目。构建完毕后,复制新生成的 tomcat-redis-session-manager-1.2.jar 到每一个 Tomcat 实例的 lib 子目录下。并在修改每一个 Tomcat 实例的 context.xml 配置文件:

Add the following into your Tomcat context.xml (or the context block of the server.xml if applicable.)

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost" <!-- optional: defaults to "localhost" -->
         port="6379" <!-- optional: defaults to "6379" -->
         database="0" <!-- optional: defaults to "0" -->
         maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
         sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional -->
         sentinelMaster="SentinelMasterName" <!-- optional -->
         sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />

The Valve must be declared before the Manager.

Copy the following files into the TOMCAT_BASE/lib directory:

  • tomcat-redis-session-manager-VERSION.jar
  • jedis-2.5.2.jar
  • commons-pool2-2.2.jar

Reboot the server, and sessions should now be stored in Redis.

配置完成之后 依次启动redis、tomcat、nginx 》》OK!

redis安装  http://xxk.b3log.org/articles/2015/08/21/1440141912836.html

MongoDB 也可以 做 session 管理


Mongo Tomcat Session Manager


This is a tomcat session manager that saves sessions in MongoDB. It is made up of MongoManager, that provides the save/load functions, and MongoSessionTrackerValve that controls the timing of the save.

For those interested, PersistentManager/ an implementation of a SessionStore is non deterministic in when it saves to the DB, so isn't suitable as a distributed session replicator. This is why I've used a valve to control the save timing at the end of each request.


Add the following into your tomcat server.xml, or context.xml

<Valve className="com.dawsonsystems.session.MongoSessionTrackerValve" />
<Manager className="com.dawsonsystems.session.MongoManager" host="dbHost1,dbHost2" port="27017" database="sessions" maxInactiveInterval="84"/>

The Valve must be before the Manager.

The following parameters are available on the Manager :-

maxInactiveInterval The initial maximum time interval, in seconds, between client requests before a session is invalidated. A negative value will result in sessions never timing out. If the attribute is not provided, a default of 60 seconds is used.
processExpiresFrequency Frequency of the session expiration, and related manager operations. Manager operations will be done once for the specified amount of backgrondProcess calls (i.e., the lower the amount, the more often the checks will occur). The minimum value is 1, and the default value is 6.
host The database hostnames(s). Multiple hosts can be entered, seperated by a comma
port The database port to connect to. The same port will be used for each database host. The default is 27017
database The database used to store sessions in, the default is 'sessions'

Put the mongo-store jar and the mongo java driver (I've tested with 2.3) into the tomcat lib directory and you're good to go.