nginx負載均衡tomcat集群memcache共享session_第1頁
nginx負載均衡tomcat集群memcache共享session_第2頁
nginx負載均衡tomcat集群memcache共享session_第3頁
nginx負載均衡tomcat集群memcache共享session_第4頁
nginx負載均衡tomcat集群memcache共享session_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、nginx 負載均衡 tomcat集群 memcache共享 session分類:nginx 2011-03-05 20:521319人閱讀評論(1)收藏舉報要集群tomcat主要是解決 SESSION共享的問題,因此我利用memcached來保存session,多臺TOMCAT服務(wù)器即可共享 SESSION 了。你可以自己寫tomcat的擴展來保存 SESSION到memcached。這里推薦使用 memcached-sessi on-man ager這個開源項目url /url,下面簡稱 msm。女M可安裝 nginx、memcached、tomcat這些就不多說了。先說明一下測試環(huán)境:t

2、omcat1、nginx、memcached 安裝在 1tomcat2 安裝在 01下面分步實現(xiàn)基于nginx的tomcat負載均衡和集群配置一,tomcat 集群1,先下載msm及其依賴包urlhttp:/memcached-sessi on-man on-man ager-1.3.0.jar /urlurlhttp:/memcached-sessi on-man on-serializer-jodatime-1.3.0.jar/urlurlhttp:/memcached-sessi on-man on-serializer-cglib-1.3.0

3、.jar /urlurl /urlurl http:/memcached-sessi on-man n-.jar /url2, 將這5個包放到$TOMCAT_HOME/lib 目錄下3, 修改 $TOMCAT_HOME/conf/server.xml1 <Context docBase= "E:/java_codes/TestSession/WebContent" path=""reloadable= "true" >2 2 .<Manager className= "de.javakaffe

4、e.web.msm.MemcachedBackupSessionManager"3 3 . memcachedNodes= "n1:localhost:11211"4 4 . requestUrilgnorePattern= ".*/.(png|gif|jpg|css|js)$"5 . sessionBackupAsync= "false"6 . sessionBackupTimeout= "100"6 . transcoderFactoryClass="de.javakaffee.web.ms

5、m.serializer.javolutio n.J avolutionTranscoderFactory"7 8 . copyCollectionsForSerialization= "false"8 9 . />9 10 .</Context>11J2 <Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" >13 vManager className="

6、de.javakaffee.web.msm.MemcachedBackupSessionManager"14 memcachedNodes="n1:localhost:11211"15 requestUrilgnorePattern=".*/.(png|gif|jpg|css|js)$"16 sessionBackupAsync="false"17 sessionBackupTimeout="100"IStranscoderFactoryClass="de.javakaffee.web.msm.

7、serializer.javolution.JavolutionTranscoderFactory"19 copyCollectionsForSerialization="false" />21 </Context>22這里的 memcachedNodes是填寫 memcached節(jié)點,多個節(jié)點時可以以空隔分開,如 :n1:localhost:11211 n2:localhost:11212 sessionBackupTimeout 的單位為分鐘E:/java_codes/TestSession/WebContent 替換成你的 WEB 目錄修改

8、后重啟兩個TOMCAT即可,這個時候已經(jīng)解決 SESSION的共享問題 二,配置nginx實現(xiàn)負載均衡以我的nginx.conf為例 Xml 代碼 1.#user nobody; 2.worker_processes 1;error_log logs/error.log;3.5.6. eve nts 7. worker_c onnections 1024;1. http 12.13. iin elude default_typemime.types;applicati on/o ctet-stream;14.15.sen dfile16.on;keepalive_timeout

9、 65;17.18.19.#gzip on; upstream20.21. server 1:8080;server 01:8080;22.23.24. server liste n25.80;name ;2.33.servercharset utf-8; locatio n / root html; in dex in dex.html in dex.htm;proxy_pass;proxy_set_header X-Real-IP $remote_addr; clie nt_max_body_size100m

10、;7.38.location T(WEB-INF)/ deny all;2.43.error_page 500 502 503 504 /50x.html; locatio n = /50x.html root html;44.45.46. #userno body;worker_processes 1;error_log logs/error.log; events worker_c onnections1024; http in cludemime.types;default_type applicati on/o ctet-stream;sen d

11、fileon;keepalive_timeout 65;#gzip on;upstream server 1:8080;server 01:8080;server liste n80;server_name ;charset utf-8;locati on / root html;in dex in dex.html in dex.htm;proxy_pass;proxy_set_header X-Real-IP $remote_addr; clie nt_max_body_size 100m;location 9(WEB-INF)/ deny al

12、l;error_page 500 502 503 504 /50x.html;locati on = /50x.html root html;將替換成你的域名1和01替換成你服務(wù)器的IPOK,已經(jīng)完成。啟動 nginx即可。以下是wiki上面的原話:SetupA ndCon figuratio nThis page shows what's n ecessary to get the memcached-sessi on-man ager up and running.In troductio n?For the most simple in

13、 tegrati on you just n eed to have a tomcat (6 or 7) and a memcached in stalled(or s.th. support ing the memcached protocol). In your product ion en viro nment you probably will have several tomcats and you should also have several memcached no des available, on differe nt pieces of hardware. You ca

14、n use sticky sessi ons ornon-stickysessi ons,memcached-sessi on-man ager supports both operati on modes.The follow ing descripti on shows an example for a setup with sticky sessi ons, with two in sta nces of tomcat and two in sta nces of memcached in stalled.Tomcat-1 (t1) will primarily store it'

15、;s sessi ons in memcached-2 (m2) which is running on ano ther machine (m2 is a regular node for t1). Only if m2 is not available, t1 will store it's sessions in memcached-1 (ml, ml is the failoverNode for t1). With this con figurati on, sessi ons won't be lost whe n machi ne 1 (servi ng t1 a

16、nd ml) crashes. The follow ing really nice ASCII art shows this setup. <t1><t2>. / / . X . / / . <m1> <m2>Details?So what n eeds to be done for this?Decide which serializati on strategy to use?Starting with release 1.1 there are several session serialization strategies availa

17、ble, as they are described on Serializati on Strategies. The default strategy uses java serializati on and is already provided by the memcached-sessi on-man ager jar. Other strategies are provided by separate jars, in the secti on below you'll see which jars are required for which strategy.Con f

18、igure tomcat?The configuration of tomcat requires two things: you need to drop some jars in your $CATALINA_HOME/lib/ directory and you have to con figure the memcached sessi on man ager in the server.xml.Add jars to $CATALINA_HOME/lib/?you always n eed thetomcat6,orand the memcached-2.5.jarIn depe n

19、dentof thechose nserializati onstrategymemcached-sessi on-man ager-1.4.0-RC1.jar(formemcached-sessi on-man ager-tc7-1.4.0-RC1.jar for tomcat7) (spymemcached). Just down load and put them in $CA TALINA_HOME/lib/.Add jars to WEB-INF/lib/?If you want to use java's built i n serializati on nothing m

20、ore has to be don e. If you want to use a custom serializati on strategy (e.g. because of better performa nee) you n eed to drop some jars into your projects WEB-INF/lib/ direct ory. In the followi ng the jars are listed by serializati on strategy.For kryo based serializati on (recomme nded) these j

21、ars are required additi on ally (in your WEB-INF/lib): kryo-1.03.jarmin log-1.2.jarreflectasm-0.9.jarasm-3.2.jarkryo-serializers-0.8.jarmsm-kryo-serializer-1.3.6.jarFor javoluti on based serializati on these jars are required additi on ally: javolution-.jar (a patched version of the latest ja

22、volution release, the patch is required to support serializati on of jdk proxies) msm-javoluti on-serializer-1.3.6.jarFor xstream based serializati on these jars are required additi on ally: xstream-1.3.1.jarxpp3_ min-.O.jar (xml pull parser used by xstream) msm-xstream-serializer-1.3.6.jarUp

23、date server.xml?Con figure the appropriate con text in your $CA TALINA_HOME/c on f/server.xml so that it contains the Manager configuration for the memcached-session-manager, like this example for sticky sessi ons:<Co ntextpath=""docBase="ROOT"><Ma nagerclassName="d

24、e.javakaffee.web.msm.MemcachedBackupSessi onMan ager" memcachedNodes=" n1:localhost:11211n2:localhost:11212"failoverNodes=" n1"requestUriIg no rePatter n=".*/.(p ng|gif|jpg|css|js)$"tran scoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTra nscoder

25、Factory"/></C on text>For non-sticky sessi ons the con figuratio n would look like this:<Co ntextpath=""docBase="ROOT"><Ma nagerclassName="de.javakaffee.web.msm.MemcachedBackupSessi onMan ager"memcachedNodes=" n1:localhost:11211n2:localhost:1

26、1212"sticky="false"locki ngMode="uriPattern:/path1|/path2"requestUriIg norePattern=".*/.(p ng|gif|jpg|css|js)$"tran scoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTra nscoderFactory"/></C on text>These example con figuratio ns assu

27、me, that you have running two memcached no des on localhost, one on port 11211 and ano ther one on port 11212, and that you want to use kryo based serializati on. More details to con figurati on attributes are provided in the sect ion below.Now you have fini shed the con figuratio n of your first to

28、mcat. For the sec ond tomcat you just n eed to cha nge the failover node whe n you're using sticky sessi ons.After this is done, you can just start your applicati on and sessi ons will be stored in the con figured memcached no des as con figured. Now you should do some tests with a simulated tom

29、cat failure, a restart of a memcached node etc. - have fun!:-)MemcachedBackupSessi onMan ager con figurati on attributes?className (required)This should be set to de.javakaffee.web.msm.MemcachedBackupSessi onMan ager to get sessi ons stored in memcached. However, since version 1.3.6 there's also a de.javakaffee.web.msm.Du

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論