redis + Tomcat 8 的session共享解決

若是英文不錯的看,建議直接看官網吧,官網寫的挺清楚。下面的內容是轉載的一篇文章,本身補充了一些,供你們參考,也歡迎你們一塊兒討論前端

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

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

修改的源代碼:RedisSessionManager.javagit

    @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);
    }github

修改後的內容web

    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

前提:你已經部署了Redis,還沒有學會的略過tomcat

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

1> 單點Reids配置session

<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做爲前端服務器

五:網站運行效果圖:

相關文章
相關標籤/搜索