Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享
轉(zhuǎn)載自??Redis+Tomcat+Nginx集群實(shí)現(xiàn)Session共享,Tomcat Session共享
一、Session共享使用tomcat-cluster-redis-session-manager插件實(shí)現(xiàn)
插件地址見(jiàn):
https://github.com/ran-jit/tomcat-cluster-redis-session-manager
該插件支持Tomcat7、Tomcat8、Tomcat9
或者直接在附件中下載(版本為2.0.2,2017-11-27日前最新版本)
http://dl.iteye.com/topics/download/d9fffd9d-84dd-385b-b10e-6376eaf0c815
這里有是一個(gè)只支持Tomcat7的,不支持tomcat8,暫時(shí)不見(jiàn)新的維護(hù):
https://github.com/jcoleman/tomcat-redis-session-manager
?
二、tomcat-cluster-redis-session-manager詳解
1、解壓后的文件如下:
?
conf目錄下有一個(gè)redis-data-cache.properties :
Redis的配置文件
#--?Redis?data-cache?configuration??#-?redis?hosts?ex:?127.0.0.1:6379,?127.0.0.2:6379,?127.0.0.2:6380,?....?? redis.hosts=127.0.0.1:6379??#-?redis?password?(for?stand-alone?mode)?? #redis.password=??#-?set?true?to?enable?redis?cluster?mode?? redis.cluster.enabled=false??#-?redis?database?(default?0)?? #redis.database=0??#-?redis?connection?timeout?(default?2000)?? #redis.timeout=2000??ib目錄下有4個(gè)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
?
三、使用方法:
壓縮文件中有使用方法,見(jiàn)readMe.txt 文件:
第一步:
1.?Move?the?downloaded?jars?to?tomcat/lib?directory??*?tomcat/lib/??就是把lib目錄下的Jar包全復(fù)制到tomcat/lib目錄下
(一般來(lái)說(shuō)tomcat是集群,至少有2個(gè)tomcat,所以先配置好一個(gè)tomcat,復(fù)制完文件后,再將tomcat文件重新復(fù)制一份,這樣省事,但需要修改tomcat相應(yīng)的端口)
第二步:
2.?Add?tomcat?system?property?"catalina.base"??*?catalina.base="TOMCAT_LOCATION"??就是配置一個(gè)環(huán)境變量,和Jdk配置的環(huán)境變量一樣,需要配置一個(gè)catalina.base的環(huán)境變量,值為TOMCAT_LOCATION
如下:
第三步:
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??*?tomcat/conf/redis-data-cache.properties??把conf目錄下的配置文件redis-data-cache.properties復(fù)制到tomcat/conf/目錄下
第四步:
4.?Add?the?below?two?lines?in?tomcat/conf/context.xml??<Valve?className="tomcat.request.session.redis.SessionHandlerValve"?/>??<Manager?className="tomcat.request.session.redis.SessionManager"?/>??在tomcat/conf/目錄下的context.xml文件,加上相應(yīng)的配置,如下:
<?xml?version="1.0"?encoding="UTF-8"?>??<!--??Licensed?to?the?Apache?Software?Foundation?(ASF)?under?one?or?more??contributor?license?agreements.??See?the?NOTICE?file?distributed?with??this?work?for?additional?information?regarding?copyright?ownership.??The?ASF?licenses?this?file?to?You?under?the?Apache?License,?Version?2.0??(the?"License");?you?may?not?use?this?file?except?in?compliance?with??the?License.??You?may?obtain?a?copy?of?the?License?at??http://www.apache.org/licenses/LICENSE-2.0??Unless?required?by?applicable?law?or?agreed?to?in?writing,?software??distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS,??WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied.??See?the?License?for?the?specific?language?governing?permissions?and??limitations?under?the?License.?? --><!--?The?contents?of?this?file?will?be?loaded?for?each?web?application?--><Context>??<!--?Default?set?of?monitored?resources.?If?one?of?these?changes,?the????-->??<!--?web?application?will?be?reloaded.???????????????????????????????????-->??<WatchedResource>WEB-INF/web.xml</WatchedResource>??<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>??<!--?Uncomment?this?to?disable?session?persistence?across?Tomcat?restarts?-->??<!--?<Manager?pathname=""?/>?-->??<!--?Uncomment?this?to?enable?Comet?connection?tacking?(provides?events??on?session?expiration?as?well?as?webapp?lifecycle)?-->??<!--?<Valve?className="org.apache.catalina.valves.CometConnectionManagerValve"?/>?-->??<Valve?className="tomcat.request.session.redis.SessionHandlerValve"/>??<Manager?className="tomcat.request.session.redis.SessionManager"/>??</Context>??第五步:
5.?Verify?the?session?expiration?time?(minutes)?in?tomcat/conf/web.xml??<session-config>??<session-timeout>60<session-timeout>??<session-config>??修改session的過(guò)期時(shí)間,默認(rèn)是30分鐘,可以不需要此步驟。
session集群的配置至此結(jié)束。
?
四、Nginx集群
1、下載Nignx:
http://nginx.org/en/download.html
本人練習(xí)時(shí)使用windows,所以下載的windows版本:
http://nginx.org/download/nginx-1.13.7.zip
2、下載后解壓:D:\soft\nginx-1.12.2
(之前使用的是1.12.2的版本,現(xiàn)在最新版是1.13.7,但都一樣,附件中有1.12.2版本提供下載)
?
3、修改Nginx配置文件nginx.conf
進(jìn)入conf目錄(D:\soft\nginx-1.12.2\conf),找到nginx.conf配置文件,打開(kāi)編輯:
3.1在http{……}里加上upstream,如下:
upstream?myTomcatCluster{#?tomcatCluster和proxy_pass保持一樣????#解決session的問(wèn)題????#ip_hash;#加上這個(gè),解決Session每次訪問(wèn)頁(yè)面都不一樣,加上就一樣了。????#這里是tomcat的地址,weight越大,訪問(wèn)機(jī)率越大。????server?127.0.0.1:9300?weight=1?fail_timeout=5s?max_fails=1;????server?127.0.0.1:9400?weight=1?fail_timeout=5s?max_fails=1;????}???server:配置tomcat服務(wù)器請(qǐng)求的地址,2臺(tái)Tomcat服務(wù)就配置2個(gè)server,分別對(duì)應(yīng)9300,9400端口
weight 表示權(quán)重,權(quán)重越大,訪問(wèn)到的機(jī)率越大。
3.2、修改location / {……}
默認(rèn)是這個(gè)的:
location?/?{??root???html;??index??index.html?index.htm;??}??修改成這樣:
location?/?{??#root???html;??proxy_pass?http://myTomcatCluster;??#index??index.html?index.htm;??proxy_set_header?Host?$host;??proxy_set_header?X-Real-IP?$remote_addr;??proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;??proxy_connect_timeout???????1;??proxy_read_timeout??????????1;??proxy_send_timeout??????????1;???}??最簡(jiǎn)單的配置就是:
location?/?{??proxy_pass?http://myTomcatCluster;?? }??myTomcatCluster 對(duì)應(yīng)upstream后的命名。
下面的配置可以解決2個(gè)Tomcat服務(wù)器集群,當(dāng)一臺(tái)服務(wù)器掛掉(宕機(jī))后,請(qǐng)求變得很慢的問(wèn)題。
(Tomcat集群一臺(tái)服務(wù)器掛掉后請(qǐng)求變慢解決方案)
proxy_connect_timeout???????1;?? proxy_read_timeout??????????1;?? proxy_send_timeout??????????1;???3.3、啟動(dòng)Nginx服務(wù)器
使用Windows命令行啟動(dòng)
(1)進(jìn)入D盤:d:
(2)進(jìn)入D:\soft\nginx-1.12.2目錄:
cd?D:\soft\nginx-1.12.2??(3)啟動(dòng)服務(wù): (啟動(dòng)一閃而過(guò),但打開(kāi)進(jìn)程管理器能看到是已經(jīng)啟動(dòng)的)
start?nginx??關(guān)閉服務(wù)的命令:nginx -s stop
重新加載的命令:nginx -s reload,修改配置文件后,可以使用該命令直接加載,不需要重啟。
?
五、測(cè)試集群:
1、tomcat準(zhǔn)備
將已經(jīng)配置好的一個(gè)tomcat復(fù)制一份,修改端口,然后再修改一下tomcat的配置文件(server.xml)
我的一個(gè)tomcat在:
D:\soft\apache-tomcat-8.0.45-9300\conf
另一個(gè)是:
D:\soft\apache-tomcat-8.0.45-9400\conf
修改:
<Engine?defaultHost="localhost"?name="Catalina">??其中tomcat 9300端口的修改如下:
<Engine?defaultHost="localhost"?jvmRoute="jvm9300"?name="Catalina">??tomcat 9400端口的修改如下:
<Engine?defaultHost="localhost"?jvmRoute="jvm9400"?name="Catalina">??2、項(xiàng)目準(zhǔn)備:
新建立一個(gè)web項(xiàng)目,然后新建立一個(gè)index.jsp的文件,如下:
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"??pageEncoding="UTF-8"%>?? <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">?? <html>?? <head>?? <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">?? <title>首頁(yè)redis-session</title>?? </head>?? <body>??<div>tomcat?集群測(cè)試</div>??<div>??<%?????//HttpSession?session?=?request.getSession(true);?????System.out.println(session.getId());?????out.println("<br>?SESSION?ID:"?+?session.getId()+"<br>");?????%>??</div>?? </body>?? </html>??主要是在打印頁(yè)面輸出sessionId的信息:
out.println("<br>?SESSION?ID:"?+?session.getId()+"<br>");??然后把這個(gè)項(xiàng)目分別部署到9300、9400端口的2個(gè)tomcat中,分別啟動(dòng),記得也啟動(dòng)Nginx和redis哦
然后打開(kāi)瀏覽器通過(guò)地址訪問(wèn)項(xiàng)目:http://localhost/redis-session/ (使用Nginx集群分發(fā),不需要端口號(hào)訪問(wèn)),顯示如下:
tomcat?集群測(cè)試??SESSION?ID:B837ECA85B47081EAA2FEFCD7E579CD2.jvm9400??無(wú)論怎么刷新訪問(wèn)(打開(kāi)新的標(biāo)簽頁(yè)也是(非新窗口))的都是jvm9400,也就是端口號(hào)為9400的tomcat
后綴.jvm9400就是前面配置的:
<Engine?defaultHost="localhost"?jvmRoute="jvm9400"?name="Catalina">??打開(kāi)新的隱身窗口訪問(wèn):
tomcat?集群測(cè)試??SESSION?ID:83BBA58F4EB7B2EFF90AE05D4A0629FD.jvm9300??這時(shí)訪問(wèn)的是端口號(hào)為9300的tomcat,通過(guò)后綴.jvm9300判斷知道。
新窗口每次訪問(wèn)的是都是tomcat9300,session也不會(huì)變。
在訪問(wèn)后綴為.jvm9400時(shí),把端口9400的tomcat關(guān)掉,再次刷新訪問(wèn),sessionId一樣不變,由此可見(jiàn),2個(gè)tomcat的sessionId是共享的。
使用Redis實(shí)現(xiàn)session共享的好處就是,把session管理放在redis中,如果服務(wù)器重啟或掛機(jī),sessionId保存在redis中,下次重啟后一樣生效,避免sessionId失效,同樣redis最好也做集群,避免redis重啟或掛機(jī)。
結(jié)束了。
總結(jié)
以上是生活随笔為你收集整理的Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 闪迪发布新款闪存盘:USB-A / C
- 下一篇: 这几道 Redis 面试题都不懂,怎么拿