天天看點

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

=============================

蕃薯耀 2017年11月27日

http://www.cnblogs.com/fanshuyao/

代碼美化版或附件下載下傳見:http://fanshuyao.iteye.com/blog/2400975

一、Session共享使用tomcat-cluster-redis-session-manager插件實作

插件位址見:https://github.com/ran-jit/tomcat-cluster-redis-session-manager

該插件支援Tomcat7、Tomcat8、Tomcat9

或者直接在附件中下載下傳(版本為2.0.2,2017-11-27日前最新版本)

這裡有是一個隻支援Tomcat7的,不支援tomcat8,暫時不見新的維護:

https://github.com/jcoleman/tomcat-redis-session-manager

二、tomcat-cluster-redis-session-manager詳解

1、解壓後的檔案如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

 conf目錄下有一個redis-data-cache.properties :Redis的配置檔案

Java代碼  

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

  1. #-- Redis data-cache configuration  
  2. #- redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....  
  3. redis.hosts=127.0.0.1:6379  
  4. #- redis password (for stand-alone mode)  
  5. #redis.password=  
  6. #- set true to enable redis cluster mode  
  7. redis.cluster.enabled=false  
  8. #- redis database (default 0)  
  9. #redis.database=0  
  10. #- redis connection timeout (default 2000)  
  11. #redis.timeout=2000  

lib目錄下有4個jar包,如下:

commons-logging-1.2.jar

commons-pool2-2.4.2.jar

jedis-2.9.0.jar

tomcat-cluster-redis-session-manager-2.0.1.jar

三、使用方法:

壓縮檔案中有使用方法,見readMe.txt 檔案:

第一步:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. 1. Move the downloaded jars to tomcat/lib directory  
  2.         * tomcat/lib/  

 就是把lib目錄下的Jar包全複制到tomcat/lib目錄下

(一般來說tomcat是叢集,至少有2個tomcat,是以先配置好一個tomcat,複制完檔案後,再将tomcat檔案重新複制一份,這樣省事,但需要修改tomcat相應的端口)

第二步:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. 2. Add tomcat system property "catalina.base"  
  2.         * catalina.base="TOMCAT_LOCATION"  

 就是配置一個環境變量,和Jdk配置的環境變量一樣,需要配置一個catalina.base的環境變量,值為TOMCAT_LOCATION

如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

第三步:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. 3. Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory  
  2.         * tomcat/conf/redis-data-cache.properties  

 把conf目錄下的配置檔案redis-data-cache.properties複制到tomcat/conf/目錄下

第四步:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. 4. Add the below two lines in tomcat/conf/context.xml  
  2.         <Valve className="tomcat.request.session.redis.SessionHandlerValve" />  
  3.         <Manager className="tomcat.request.session.redis.SessionManager" />  

 在tomcat/conf/目錄下的context.xml檔案,加上相應的配置,如下:

Xml代碼  

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.   Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.       http://www.apache.org/licenses/LICENSE-2.0  
  10.   Unless required by applicable law or agreed to in writing, software  
  11.   distributed under the License is distributed on an "AS IS" BASIS,  
  12.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.   See the License for the specific language governing permissions and  
  14.   limitations under the License.  
  15. --><!-- The contents of this file will be loaded for each web application --><Context>  
  16.     <!-- Default set of monitored resources. If one of these changes, the    -->  
  17.     <!-- web application will be reloaded.                                   -->  
  18.     <WatchedResource>WEB-INF/web.xml</WatchedResource>  
  19.     <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>  
  20.     <!-- Uncomment this to disable session persistence across Tomcat restarts -->  
  21.     <!-- 
  22.     <Manager pathname="" /> 
  23.     -->  
  24.     <!-- Uncomment this to enable Comet connection tacking (provides events  
  25.          on session expiration as well as webapp lifecycle) -->  
  26.     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> 
  27.     <Valve className="tomcat.request.session.redis.SessionHandlerValve"/>  
  28.     <Manager className="tomcat.request.session.redis.SessionManager"/>  
  29. </Context>  

第五步:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. 5. Verify the session expiration time (minutes) in tomcat/conf/web.xml  
  2.         <session-config>  
  3.             <session-timeout>60<session-timeout>  

 修改session的過期時間,預設是30分鐘,可以不需要此步驟。

session叢集的配置至此結束。

四、Nginx叢集

1、下載下傳Nignx:http://nginx.org/en/download.html

本人練習時使用windows,是以下載下傳的windows版本:http://nginx.org/download/nginx-1.13.7.zip

2、下載下傳後解壓:D:\soft\nginx-1.12.2 (之前使用的是1.12.2的版本,現在最新版是1.13.7,但都一樣,附件中有1.12.2版本提供下載下傳)

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

3、修改Nginx配置檔案nginx.conf

進入conf目錄(D:\soft\nginx-1.12.2\conf),找到nginx.conf配置檔案,打開編輯:

3.1在http{……}裡加上upstream,如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. upstream myTomcatCluster{# tomcatCluster和proxy_pass保持一樣    
  2.         #解決session的問題    
  3.         #ip_hash;#加上這個,解決Session每次通路頁面都不一樣,加上就一樣了。    
  4.         #這裡是tomcat的位址,weight越大,通路機率越大。    
  5.         server 127.0.0.1:9300 weight=1 fail_timeout=5s max_fails=1;    
  6.         server 127.0.0.1:9400 weight=1 fail_timeout=5s max_fails=1;    
  7.     }   

 server:配置tomcat伺服器請求的位址,2台Tomcat服務就配置2個server,分别對應9300,9400端口

weight 表示權重,權重越大,通路到的機率越大。

3.2、修改location / {……}

預設是這個的:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. location / {  
  2.             root   html;  
  3.             index  index.html index.htm;  
  4.         }  

修改成這樣:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1.             #root   html;  
  2.         proxy_pass http://myTomcatCluster;  
  3.             #index  index.html index.htm;  
  4.         proxy_set_header Host $host;  
  5.             proxy_set_header X-Real-IP $remote_addr;  
  6.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  7.             proxy_connect_timeout       1;  
  8.             proxy_read_timeout          1;  
  9.             proxy_send_timeout          1;   

最簡單的配置就是:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1.      proxy_pass http://myTomcatCluster;  
  2. }  

 myTomcatCluster 對應upstream後的命名。

下面的配置可以解決2個Tomcat伺服器叢集,當一台伺服器挂掉(當機)後,請求變得很慢的問題。

(Tomcat叢集一台伺服器挂掉後請求變慢解決方案)

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. proxy_connect_timeout       1;  
  2. proxy_read_timeout          1;  
  3. proxy_send_timeout          1;   

3.3、啟動Nginx伺服器

使用Windows指令行啟動

(1)進入D盤:d:

(2)進入D:\soft\nginx-1.12.2目錄:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. cd D:\soft\nginx-1.12.2  

(3)啟動服務: (啟動一閃而過,但打開程序管理器能看到是已經啟動的)

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. start nginx  

關閉服務的指令:nginx -s stop

重新加載的指令:nginx -s reload,修改配置檔案後,可以使用該指令直接加載,不需要重新開機。

五、測試叢集:

1、tomcat準備

将已經配置好的一個tomcat複制一份,修改端口,然後再修改一下tomcat的配置檔案(server.xml)

我的一個tomcat在:D:\soft\apache-tomcat-8.0.45-9300\conf

另一個是:D:\soft\apache-tomcat-8.0.45-9400\conf

修改:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. <Engine defaultHost="localhost" name="Catalina">  

 其中tomcat 9300端口的修改如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. <Engine defaultHost="localhost" jvmRoute="jvm9300" name="Catalina">  

 tomcat 9400端口的修改如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. <Engine defaultHost="localhost" jvmRoute="jvm9400" name="Catalina">  

2、項目準備:

建立立一個web項目,然後建立立一個index.jsp的檔案,如下:

Html代碼  

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>首頁redis-session</title>  
  8. </head>  
  9. <body>  
  10.     <div>tomcat 叢集測試</div>  
  11.     <div>  
  12.         <%     
  13.           //HttpSession session = request.getSession(true);     
  14.           System.out.println(session.getId());     
  15.           out.println("<br> SESSION ID:" + session.getId()+"<br>");     
  16.         %>  
  17.     </div>  
  18. </body>  
  19. </html>  

 主要是在列印頁面輸出sessionId的資訊:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. out.println("<br> SESSION ID:" + session.getId()+"<br>");  

然後把這個項目分别部署到9300、9400端口的2個tomcat中,分别啟動,記得也啟動Nginx和redis哦

然後打開浏覽器通過位址通路項目:http://localhost/redis-session/ (使用Nginx叢集分發,不需要端口号通路),顯示如下:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. tomcat 叢集測試  
  2. SESSION ID:B837ECA85B47081EAA2FEFCD7E579CD2.jvm9400  

 無論怎麼重新整理通路(打開新的标簽頁也是(非新視窗))的都是jvm9400,也就是端口号為9400的tomcat

字尾.jvm9400就是前面配置的:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

打開新的隐身視窗通路:

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享
  1. SESSION ID:83BBA58F4EB7B2EFF90AE05D4A0629FD.jvm9300  

 這時通路的是端口号為9300的tomcat,通過字尾.jvm9300判斷知道。

新視窗每次通路的是都是tomcat9300,session也不會變。

在通路字尾為.jvm9400時,把端口9400的tomcat關掉,再次重新整理通路,sessionId一樣不變,由此可見,2個tomcat的sessionId是共享的。

使用Redis實作session共享的好處就是,把session管理放在redis中,如果伺服器重新開機或挂機,sessionId儲存在redis中,下次重新開機後一樣生效,避免sessionId失效,同樣redis最好也做叢集,避免redis重新開機或挂機。

(如果你覺得文章對你有幫助,歡迎捐贈,^_^,謝謝!) 

Redis+Tomcat+Nginx叢集實作Session共享,Tomcat Session共享

今天越懶,明天要做的事越多。

繼續閱讀