nginx+tomcat+redis完成session共享


本文記錄nginx+redis+tomcat實現session共享的過程

nginx安裝:http://blog.csdn.net/grhlove123/article/details/47834673

redis安裝:http://blog.csdn.net/grhlove123/article/details/47783471

准備兩個tomcat,修改相應的端口

 

名稱 IP 端口 tomcat版本 JDK
tomcat1 10.10.49.23 8080 7.0.40 1.7.0_25
tomcat2 10.10.49.15 8081 7.0.40 1.7.0_25

 

修改nginx.conf加上:

[html]  view plain  copy
 
  1. upstream backend {  
  2.     server 10.10.49.23:8080 max_fails=fail_timeout=10s;  
  3.     server 10.10.49.15:8081 max_fails=fail_timeout=10s;  
  4. }  
修改nginx.conf的location成
[html]  view plain  copy
 
  1. location / {  
  2.     root   html;  
  3.     index  index.html index.htm;  
  4.     proxy_pass http://backend;  
  5.  }  

啟動nginx。

 

下載tomcat-redis-session-manager相應的jar包,主要有三個:

wget https://github.com/downloads/jcoleman/tomcat-redis-session-manager/tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
wget http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
wget http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar

下載完成后拷貝到$TOMCAT_HOME/lib中

修改兩tomcat的context.xml:

 

[html]  view plain  copy
 
  1. <Context>  
  2.   
  3.     <!-- Default set of monitored resources -->  
  4.     <WatchedResource>WEB-INF/web.xml</WatchedResource>  
  5.   
  6.     <!-- Uncomment this to disable session persistence across Tomcat restarts -->  
  7.     <!-- 
  8.     <Manager pathname="" /> 
  9.     -->  
  10.   
  11.     <!-- Uncomment this to enable Comet connection tacking (provides events  
  12.          on session expiration as well as webapp lifecycle) -->  
  13.     <!-- 
  14.     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> 
  15.     -->  
  16.   
  17.   <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
  18.   <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
  19.    host="10.10.49.20"  
  20.    port="6379"  
  21.    database="0"  
  22.    maxInactiveInterval="60" />  
  23. </Context>  

在tomcat/webapps/test放一個index.jsp

 

 

[html]  view plain  copy
 
  1. <%@ page language="java" %>  
  2. <html>  
  3.   <head><title>TomcatA</title></head>  
  4.   <body>  
  5.    
  6.     <table align="centre" border="1">  
  7.       <tr>  
  8.         <td>Session ID</td>  
  9.         <td><%= session.getId() %></td>  
  10.       </tr>  
  11.       <tr>  
  12.         <td>Created on</td>  
  13.         <td><%= session.getCreationTime() %></td>  
  14.      </tr>  
  15.     </table>  
  16.   </body>  
  17. </html>  
  18. sessionID:<%=session.getId()%>   
  19. <br>   
  20. SessionIP:<%=request.getServerName()%>   
  21. <br>   
  22. SessionPort:<%=request.getServerPort()%>   
  23. <%   
  24. //為了區分,第二個可以是222  
  25. out.println("This is Tomcat Server 1111");   
  26. %>    

 

啟動tomcat,發現有異常:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve 類找不到

分別打開三個jar包,確實沒有這個類,解決可以參考:

http://blog.csdn.net/qinxcb/article/details/42041023

 

通過訪問http://10.10.49.20/test/


刷新:

可以看到雖然Server從1111變為2222,但session的創建時間沒有變化,這就完成了session共享。

 
 http://blog.csdn.net/grhlove123/article/details/48047735

 

 

1,安裝redis並配置和啟動, tomcat也做相就的下載,其他地方都有,可以在其他地方查閱。
2,  獲取tomcat依賴包:
             Tomcat使用 從https://github.com/xetorthio/jedis/downloads下載jedis作為java的redis客戶端,
              從https://github.com/jcoleman/tomcat-redis-session-manager/downloads下載tomcat的redis session manager插件
          從http://commons.apache.org/proper/commons-pool/download_pool.cgi下載apache的common pool2包,2.2,將這幾個jar包拷貝到tomcat7的lib目錄下
         包有: redis2.8、jedis.jar、common-pool2.2.jar、tomcat-redis-session-manager-1.2-tomcat-7.jar
3 配置 tomcat
           在https://github.com/jcoleman/tomcat-redis-session-manager 這里面文章看到的配置為:
          <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
         <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost" <!-- optional: defaults to "localhost" -->
         port="6379" <!-- optional: defaults to "6379" -->
         database="0" <!-- optional: defaults to "0" -->
         maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
         sessionPersistPolicies="PERSIST_POLICY_1,PERSIST_POLICY_2,.." <!-- optional -->
         sentinelMaster="SentinelMasterName" <!-- optional -->
         sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
            而下載的包tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar或tomcat-redis-session-manager-1.2-tomcat-7.jar
 4 相關包的里面並沒有類:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve
     所以從https://github.com/jcoleman/tomcat-redis-session-manager直接下載源碼
     發現源碼里面存在相應的類,同時源碼(tomcat-redis-session-manager)依賴了tomcat其他的包:tomcat-juli.jar
     而tomcat默認是沒有這些包的,從http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.57/bin/extras/ 下載tomcat-juli-adapters.jar和tomcat-juli.jar兩個包,放在apache-tomcat-7.0.57\lib目錄下,同時將tomcat-juli.jar放在apache-tomcat-7.0.57\bin目錄下

     同時將編譯tomcat-redis-session-manager的源碼,通過相應的依賴包common-pool2.2,jedis以及tomcat-juli.jar編譯,

並打成自己的jar包,我已經上傳在:

    http://download.csdn.net/detail/qinxcb/8279761
   然后將這個依賴包放在apache-tomcat-7.0.57\lib目錄下,刪除網上下載的tomcat-redis-session-manager-1.2-tomcat-7.jar.

http://blog.csdn.net/qinxcb/article/details/42041023

 


免責聲明!

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



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