生活随笔
收集整理的這篇文章主要介紹了
多台tomcat服务的session共享 memcached与redis
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由于tomcat的并發(fā)數(shù)瓶頸問題,可以說使用tomcat的web應(yīng)用,幾乎都存在session不同步問題。
借鑒網(wǎng)上的資料,我也找時(shí)間實(shí)驗(yàn)一把。
文中涉及的軟件下載和安裝,一一略過,想必大家也沒必要看。
注:本文不對(duì)memcached和redis做任何口水討論,望各個(gè)網(wǎng)友自行問谷歌和度娘。
(個(gè)人愚見,它們作為一個(gè)軟件,能獲得各自眾多支持者,想必它們自然有各自的優(yōu)點(diǎn),重點(diǎn)還是從實(shí)際需要出發(fā),選擇合適自己的東東。)
一、nginx+tomcat+memcached? (依賴包下載)
1.memcached配置:(v1.4.13)
節(jié)點(diǎn)1(192.168.159.131:11444)
節(jié)點(diǎn)2(192.168.159.131:11333)
2.tomcat配置
tomcat1(192.168.159.128:8081)
tomcat2(192.168.159.128:8082)
3.nginx安裝在192.168.159.131。
?????? 首先,是配置tomcat,使其將session保存到memcached上。有兩種方法:
方法一:在server.xml中配置。
找到host節(jié)點(diǎn),加入
[html]?view plaincopy
<Context?docBase="/var/www/html"?path="">???????<Manager?className="de.javakaffee.web.msm.MemcachedBackupSessionManager"???????????????memcachedNodes="n1:192.168.159.131:11444?n2:192.168.159.131:11333"???????????????requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"???????????????sessionBackupAsync="false"?sessionBackupTimeout="3000"???????????????transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"???????????????copyCollectionsForSerialization="false"?/>??</Context>??
方法二:在context.xml中配置。
找到Context節(jié)點(diǎn),加入
[html]?view plaincopy
<Manager?className="de.javakaffee.web.msm.MemcachedBackupSessionManager"???????????memcachedNodes="n1:192.168.159.131:11444"???????????requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"???????????sessionBackupAsync="false"?sessionBackupTimeout="3000"???????????transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"???????????copyCollectionsForSerialization="false"?/>??
?????? 其次,配置nginx,用于測(cè)試session保持共享。
[html]?view plaincopy
upstream??xxy.com??{????????server???192.168.159.128:8081?;????????server???192.168.159.128:8082?;??}????log_format??www_xy_com??'$remote_addr?-?$remote_user?[$time_local]?$request?'?????????????????'"$status"?$body_bytes_sent?"$http_referer"'??????????????????'"$http_user_agent"?"$http_x_forwarded_for"';????server??{????????listen??80;????????server_name??xxy.com;??????????location?/?{?????????????????proxy_pass????????http://xxy.com;?????????????????proxy_set_header???Host?????????????$host;?????????????????proxy_set_header???X-Real-IP????????$remote_addr;?????????????????proxy_set_header???X-Forwarded-For??$proxy_add_x_forwarded_for;????????}??????????access_log??/data/base_files/logs/www.xy.log??www_xy_com;??}??最后,將你的應(yīng)用放到兩個(gè)tomcat中,并依次啟動(dòng)memcached、tomcat、nginx。訪問你的nginx,可以發(fā)現(xiàn)兩個(gè)tomcat中的session可以保持共享了。
二、nginx+tomcat+redis?? (依賴包下載)
1.redis配置(192.168.159.131:16300)(v2.8.3)
2.tomcat配置
tomcat1(192.168.159.130:8081)
tomcat2(192.168.159.130:8082)
3.nginx安裝在192.168.159.131。
?????? 首先,是配置tomcat,使其將session保存到redis上。有兩種方法,也是在server.xml或context.xml中配置,不同的是memcached只需要添加一個(gè)manager標(biāo)簽,而redis需要增加的內(nèi)容如下:(注意:valve標(biāo)簽一定要在manager前面。)
[html]?view plaincopy
<Valve?className="com.radiadesign.catalina.session.RedisSessionHandlerValve"?/>??<Manager?className="com.radiadesign.catalina.session.RedisSessionManager"???????????host="192.168.159.131"???????????port="16300"????????????database="0"????????????maxInactiveInterval="60"/>??其次,配置nginx,用于測(cè)試session保持共享。
[html]?view plaincopy
upstream??redis.xxy.com??{????????server???192.168.159.130:8081;????????server???192.168.159.130:8082;??}????log_format??www_xy_com??'$remote_addr?-?$remote_user?[$time_local]?$request?'?????????????????'"$status"?$body_bytes_sent?"$http_referer"'??????????????????'"$http_user_agent"?"$http_x_forwarded_for"';????server??{????????listen??80;????????server_name?redis.xxy.com;???????????location?/?{?????????????????proxy_pass????????http://redis.xxy.com;?????????????????proxy_set_header???Host?????????????$host;?????????????????proxy_set_header???X-Real-IP????????$remote_addr;?????????????????proxy_set_header???X-Forwarded-For??$proxy_add_x_forwarded_for;????????}??????????access_log??/data/base_files/logs/redis.xxy.log??www_xy_com;??}??最后,將你的應(yīng)用放到兩個(gè)tomcat中,并依次啟動(dòng)redis、tomcat、nginx。訪問你的nginx,可以發(fā)現(xiàn)兩個(gè)tomcat中的session可以保持共享了。
上面文章中,有一點(diǎn)需要說明的是:
如果tomcat配置中,將manager放在server.xml中,那么使用maven做熱部署時(shí),會(huì)發(fā)生失敗。所以,本人推薦放在context.xml中。
總結(jié)
以上是生活随笔為你收集整理的多台tomcat服务的session共享 memcached与redis的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。