Redis+Tomcat+Nginx集羣實現Session共享,Tomcat Session共享

Redis+Tomcat+Nginx集羣實現Session共享,Tomcat Session共享html

 

=============================java

蕃薯耀 2017年11月27日nginx

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

 

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

 

1、Session共享使用tomcat-cluster-redis-session-manager插件實現web

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

該插件支持Tomcat七、Tomcat八、Tomcat9express

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

 

這裏有是一個只支持Tomcat7的,不支持tomcat8,暫時不見新的維護:windows

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

 

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

一、解壓後的文件以下:



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

 

Java代碼   收藏代碼
  1. #-- Redis data-cache configuration  
  2.   
  3. #- redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....  
  4. redis.hosts=127.0.0.1:6379  
  5.   
  6. #- redis password (for stand-alone mode)  
  7. #redis.password=  
  8.   
  9. #- set true to enable redis cluster mode  
  10. redis.cluster.enabled=false  
  11.   
  12. #- redis database (default 0)  
  13. #redis.database=0  
  14.   
  15. #- redis connection timeout (default 2000)  
  16. #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

 

3、使用方法:

壓縮文件中有使用方法,見readMe.txt 文件:

第一步:

 

Java代碼   收藏代碼
  1. 1. Move the downloaded jars to tomcat/lib directory  
  2.         * tomcat/lib/  

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

 

(通常來講tomcat是集羣,至少有2個tomcat,因此先配置好一個tomcat,複製完文件後,再將tomcat文件從新複製一份,這樣省事,但須要修改tomcat相應的端口)

 

第二步:

 

Java代碼   收藏代碼
  1. 2. Add tomcat system property "catalina.base"  
  2.         * catalina.base="TOMCAT_LOCATION"  

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

 

以下:



 



 

 

第三步:

 

Java代碼   收藏代碼
  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/目錄下

 

 

第四步:

 

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

 

 

第五步:

 

Java代碼   收藏代碼
  1. 5. Verify the session expiration time (minutes) in tomcat/conf/web.xml  
  2.         <session-config>  
  3.             <session-timeout>60<session-timeout>  
  4.         <session-config>  

 修改session的過時時間,默認是30分鐘,能夠不須要此步驟。

 

 

session集羣的配置至此結束。

 

4、Nginx集羣

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

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

 

二、下載後解壓:D:\soft\nginx-1.12.2 (以前使用的是1.12.2的版本,如今最新版是1.13.7,但都同樣,附件中有1.12.2版本提供下載)



 

三、修改Nginx配置文件nginx.conf

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

 

3.1在http{……}里加上upstream,以下:

 

Java代碼   收藏代碼
  1. upstream myTomcatCluster{# tomcatCluster和proxy_pass保持同樣    
  2.         #解決session的問題    
  3.         #ip_hash;#加上這個,解決Session每次訪問頁面都不同,加上就同樣了。    
  4.             
  5.         #這裏是tomcat的地址,weight越大,訪問機率越大。    
  6.         server 127.0.0.1:9300 weight=1 fail_timeout=5s max_fails=1;    
  7.         server 127.0.0.1:9400 weight=1 fail_timeout=5s max_fails=1;    
  8.     }   

 server:配置tomcat服務器請求的地址,2臺Tomcat服務就配置2個server,分別對應9300,9400端口

 

weight 表示權重,權重越大,訪問到的機率越大。

 

3.二、修改location / {……}

默認是這個的:

Xml代碼   收藏代碼
  1. location / {  
  2.             root   html;  
  3.             index  index.html index.htm;  
  4.         }  

 

修改爲這樣:

Xml代碼   收藏代碼
  1. location / {  
  2.             #root   html;  
  3.         proxy_pass http://myTomcatCluster;  
  4.             #index  index.html index.htm;  
  5.         proxy_set_header Host $host;  
  6.             proxy_set_header X-Real-IP $remote_addr;  
  7.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  8.             proxy_connect_timeout       1;  
  9.             proxy_read_timeout          1;  
  10.             proxy_send_timeout          1;   
  11.         }  

 

最簡單的配置就是:

Xml代碼   收藏代碼
  1. location / {  
  2.      proxy_pass http://myTomcatCluster;  
  3. }  

 myTomcatCluster 對應upstream後的命名。

 

下面的配置能夠解決2個Tomcat服務器集羣,當一臺服務器掛掉(宕機)後,請求變得很慢的問題。

(Tomcat集羣一臺服務器掛掉後請求變慢解決方案)

Xml代碼   收藏代碼
  1. proxy_connect_timeout       1;  
  2. proxy_read_timeout          1;  
  3. proxy_send_timeout          1;   

3.三、啓動Nginx服務器

使用Windows命令行啓動

(1)進入D盤:d:

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

Java代碼   收藏代碼
  1. cd D:\soft\nginx-1.12.2  

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

Java代碼   收藏代碼
  1. start nginx  

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

從新加載的命令:nginx -s reload,修改配置文件後,可使用該命令直接加載,不須要重啓。

 

 

5、測試集羣:

一、tomcat準備

將已經配置好的一個tomcat複製一份,修改端口,而後再修改一下tomcat的配置文件(server.xml)

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

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

 

修改:

Xml代碼   收藏代碼
  1. <Engine defaultHost="localhost" name="Catalina">  

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

Xml代碼   收藏代碼
  1. <Engine defaultHost="localhost" jvmRoute="jvm9300" name="Catalina">  

 tomcat 9400端口的修改以下:

Java代碼   收藏代碼
  1. <Engine defaultHost="localhost" jvmRoute="jvm9400" name="Catalina">  

 

二、項目準備:

新創建一個web項目,而後新創建一個index.jsp的文件,以下:

Html代碼   收藏代碼
  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的信息:

Html代碼   收藏代碼
  1. out.println("<br> SESSION ID:" + session.getId()+"<br>");  

 

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

而後打開瀏覽器經過地址訪問項目:http://localhost/redis-session/ (使用Nginx集羣分發,不須要端口號訪問),顯示以下:

Java代碼   收藏代碼
  1. tomcat 集羣測試  
  2.   
  3. SESSION ID:B837ECA85B47081EAA2FEFCD7E579CD2.jvm9400  

 不管怎麼刷新訪問(打開新的標籤頁也是(非新窗口))的都是jvm9400,也就是端口號爲9400的tomcat

後綴.jvm9400就是前面配置的:

Java代碼   收藏代碼
  1. <Engine defaultHost="localhost" jvmRoute="jvm9400" name="Catalina">  

 

打開新的隱身窗口訪問:

Java代碼   收藏代碼
  1. tomcat 集羣測試  
  2.   
  3. 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重啓或掛機。

 

 

結束了。

 

 

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

蕃薯耀 2017年11月27日

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

相關文章
相關標籤/搜索