Tomcat 8 的session共享解決(redis)


如果英文不錯的看,建議直接看官網吧,官網寫的挺清楚。下面的內容是轉載的一篇文章,自己補充了一些,供大家參考,也歡迎大家一起討論

官方截止到2015-10-12前是不支持Tomcat8的,詳情見官網:https://github.com/jcoleman/tomcat-redis-session-manager

銳洋智能修改的支持Tomcat8的 reyo.redis.session.manager.tomcat8

修改的源代碼:RedisSessionManager.java

    @SuppressWarnings("deprecation")
    private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        log.info("Attempting to use serializer :" + serializationStrategyClass);
        serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();

        Loader loader = null;

        if (getContainer() != null) {
            loader = getContainer().getLoader();
        }

        ClassLoader classLoader = null;

        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        serializer.setClassLoader(classLoader);
    }

修改后的內容

    private void initializeSerializer() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        log.info("Attempting to use serializer :" + serializationStrategyClass);
        serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();

        Loader loader = null;
        Context context = this.getContext();
        if (context != null) {
            loader = context.getLoader();
        }

        ClassLoader classLoader = null;

        if (loader != null) {
            classLoader = loader.getClassLoader();
        }
        serializer.setClassLoader(classLoader);
    }

前提:你已經部署了Redis,尚未學會的略過

其實很簡單,就幾個步驟: 
1.配置Tomcat的conf目錄下的context.xml文件:

1> 單點Reids配置

<Valve className="reyo.redis.session.manager.tomcat8.RedisSessionHandlerValve" />        
<Manager className="reyo.redis.session.manager.tomcat8.RedisSessionManager"
    host="localhost"
    port="6379"
    database="0"
    password="reyo"
    maxInactiveInterval="60"/>

2> Sentinel集群配置:

<!-- Sentinel 配置 -->

<Valve className="reyo.redis.session.manager.tomcat8.RedisSessionHandlerValve" />        
<Manager className="reyo.redis.session.manager.tomcat8.RedisSessionManager"

    maxInactiveInterval="60"

    sentinelMaster="mymaster"

    sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />

2.添加jar

3.測試

1> 
存儲Session:

protected  void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException {
         System.out.println( "hello" );            //取得Session對象
         HttpSession session=request.getSession(); 
         //設置Session屬性
         for (int i= 0 ;i< 100000 ;i++){
             session.setAttribute( "name" +i,  "Magci_" +i); 
         }
     }

2>重啟Tomcat:假如Session保存在tomcat下,重啟后Session不存在;如果保存在Redis下,Tomcat重啟對Session無影響

3>取出Session:

  protected  void  doPost(HttpServletRequest request, HttpServletResponse response)
      throws  ServletException, IOException {
         System.out.println( "hello" );           
         HttpSession session=request.getSession(); 
         //取出Session屬性
         for ( int  i= 0 ;i< 100000 ;i++){
             System.out.println(session.getAttribute( "name" +i));
         }
     }

注意事項:從Tomcat6開始默認開啟了Session持久化設置,測試時可以關閉本地Session持久化,其實也很簡單,在Tomcat的conf目錄下的context.xml文件中,取消注釋下面那段配置即可:

  <!-- Uncomment this to disable session persistence across Tomcat restarts -->
     <!--
     <Manager pathname="" />
     -->

 

需要注意的是:

web.xml中的配置是有效的,即使是context.xml總配置maxInactiveInterval默認60秒,只要web.xml中的sessionConfig配置30分鍾,則session的失效時間還是30分鍾。

 

運行效果圖:

一:redis主從服務器

二:redis Sentinel集群(三台)

三:tomcat8.x 集群(兩台)

Sentinel集群下的tomcat...

四:nginx作為前端服務器

五:網站運行效果圖:

 

 實例測試地址:http://sms.reyo.cn 

用戶名:aa 密碼:123456

 


免責聲明!

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



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