搭建Tomcat(集羣)session共享,用Redis保存session

環境: Win7,Tomcat7
1. 下載

     redis : https://github.com/MSOpenTech/redis/releases
    tomcat-redis-session-manager :  https://github.com/jcoleman/tomcat-redis-session-manager
2. Tomcat添加Jar包
    1. tomcat-redis-session-manager.jar : 下載源碼編譯
    2. jedis,commons-pool2,tomcat-juli    : tomcat-redis-session-manager.jar依賴包(tomcat/lib裏沒有)
    3. 添加到tomcat/lib目錄
    ## 可以使用http://download.csdn.net/download/u010379996/9792528
3. 配置Tomcat:context.xml    
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
             host="127.0.0.1"
             port="6379"
             database="0"
             maxInactiveInterval="1800" />
4. 測試
    I. 創建session.jsp:
        <%@page import="java.io.PrintWriter"%>
        <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>session</title>
        </head>
        <body>
        <%
            Object data = request.getSession().getAttribute("data");
            PrintWriter writer = response.getWriter();
            if(data==null){
                request.getSession().setAttribute("data", "redis!");
                writer.write("not session!");
            } else {
                writer.write(data.toString());
            }
            writer.flush();
            writer.close();
        %>
        </body>
        </html>
    II.
        a. 啓動redis,tomcat,訪問session.jsp
        b. 停止tomcat,再訪問session.jsp
        c. 查看redis裏的數據,看session是否存在
   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章