Tomcat8利用Redis配置Session共享


同一個應用在運行多個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. 下載開源項目

        
2. 創建maven項目(或者直接用gradle編譯也可以)
        創建項目並把src/main/java/com/orangefunction/tomcat/redissessions/復制到項目
3. 支持tomcat8需修改代碼
        RedisSessionManager.java        
  @@ -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;
 
 
 
4. 打包部署
    將實現包和依賴包 commons-pool2-2.2.jar、jedis-2.5.2.jar、tomcat-redis-session-manager-master-2.0.0.jar拷貝到tomcat的lib下
    新增tomcat context.xml配置
   

<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服務即可

相關的文章

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM