JAVA—咖啡館

          ——?dú)g迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請與我聯(lián)系。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          本文記錄nginx+redis+tomcat實(shí)現(xiàn)session共享的過程

          nginx安裝:http://www.aygfsteel.com/fanyingjie/archive/2016/04/22/430204.html

          redis安裝:http://www.aygfsteel.com/fanyingjie/archive/2016/04/22/430203.html

          準(zhǔn)備兩個tomcat,修改相應(yīng)的端口


          名稱 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=1 fail_timeout=10s;  
          3.     server 10.10.49.15:8081 max_fails=1 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相應(yīng)的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. //為了區(qū)分,第二個可以是222  
          25. out.println("This is Tomcat Server 1111");   
          26. %>    

          啟動tomcat,發(fā)現(xiàn)有異常:com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve 類找不到

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

          http://www.aygfsteel.com/fanyingjie/archive/2016/04/22/430205.html


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


          刷新:

          可以看到雖然Server從1111變?yōu)?222,但session的創(chuàng)建時間沒有變化,這就完成了session共享。

          posted on 2016-04-22 17:15 rogerfan 閱讀(424) 評論(0)  編輯  收藏 所屬分類: 【開源技術(shù)】
          主站蜘蛛池模板: 瑞金市| 基隆市| 天门市| 丹凤县| 阿图什市| 海门市| 崇信县| 尚志市| 绩溪县| 芒康县| 德令哈市| 墨脱县| 任丘市| 道真| 高陵县| 衡水市| 陵川县| 恭城| 四子王旗| 谷城县| 磐安县| 利津县| 中超| 通道| 柳江县| 奈曼旗| 漾濞| 怀远县| 淳化县| 巴彦县| 宝鸡市| 东丰县| 卓尼县| 南和县| 楚雄市| 尼木县| 镇安县| 阿拉善右旗| 文山县| 同德县| 陕西省|