同一個應用在運行多個tomcat實例的時候,經常需要共享Session。tomcat配置共享session有多種方式
1、利用tomcat自身集群特性進行配置;
2、利用Memcache第三方緩存進行配置;
3、利用Redis第三方緩存進行配置;
由於生產環境我們主使用了redis(在阿里雲中使用了redis服務)本文重點介紹了tomcat利用redis進配置session共享。
重點在於tomcat 8.x
因為tomcat7還有點不同,tomcat7的配置可以利用一個開源組件,直接配置即可以使用,比較簡單方便(直接參考這個文章即可:http://blog.csdn.net/qq584852076/article/details/46502185)。
tomcat8卻是有不同的,開源組件明確說是不支持的。
開源組件在這里: https://github.com/jcoleman/tomcat-redis-session-manager
打開網址之后,看Readme說明:
分步指南
1. 下載開源項目
@@ -713,9 +713,9 @@ private void initializeSerializer() throws ClassNotFoundException, IllegalAccess | |||
serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance(); | serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance(); | ||
Loader loader =null; | Loader loader =null; | ||
- | +Context context =this.getContext(); | ||
- if (getContainer()!=null) { | + if (context!=null) { | ||
- loader =getContainer().getLoader(); | + loader =context.getLoader(); | ||
} | } | ||
ClassLoader classLoader =null; | ClassLoader classLoader =null; |

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="r-wz97b7d28a98aa44.redis.rds.aliyuncs.com"
port="6379"
database="0"
password="xxxxxxx"
maxInactiveInterval="60" />

建議引用的幾個jar( commons-pool2-2.2.jar、jedis-2.5.2.jar)和新編譯的jar(tomcat-redis-session-manager-2.0.0.jar)放在tomcat的 lib下面。
同時如果應用下的WEB-INF/lib有相同的jar去掉
重啟tomcat服務即可
相關的文章