




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、Apache + Tomcat 負(fù)載均衡及Session綁定的實(shí)現(xiàn)關(guān)鍵字: apache tomcat 負(fù)載均衡 群集 session綁定 集群背景介紹1.1 術(shù)語定義 服務(wù)軟體是b/s或c/s結(jié)構(gòu)的s部分,是為b或c提供服務(wù)的服務(wù)性軟件系統(tǒng)。 服務(wù)硬體指提供計算服務(wù)的硬件、比如pc機(jī)、pc服務(wù)器。 服務(wù)實(shí)體通指服務(wù)軟體和服務(wù)硬體。 客戶端指接受服務(wù)實(shí)體服務(wù)的軟件或硬件。 1.2 兩大關(guān)鍵特性 集群是一組協(xié)同工作的服務(wù)實(shí)體,用以提供比單一服務(wù)實(shí)體更具擴(kuò)展性與可用性的服務(wù)平臺。在客戶端看來,一個集
2、群就象是一個服務(wù)實(shí)體,但事實(shí)上集群由一組服務(wù)實(shí)體組成。與單一服務(wù)實(shí)體相比較,集群提供了以下兩個關(guān)鍵特性: · 可擴(kuò)展性集群的性能不限于單一的服務(wù)實(shí)體,新的服務(wù)實(shí)體可以動態(tài)地加入到集群,從而增強(qiáng)集群的性能。 · 高可用性集群通過服務(wù)實(shí)體冗余使客戶端免于輕易遇到out of service的警告。在集群中,同樣的服務(wù)可以由多個服務(wù)實(shí)體提供。如果一個服務(wù)實(shí)體失敗了,另一個服務(wù)實(shí)體會接管失敗的服務(wù)實(shí)體。集群提供的從一個出錯的服務(wù)實(shí)體恢復(fù)到另一個服務(wù)實(shí)體的功能增強(qiáng)了應(yīng)用的可用性。1.3 兩大能力 為了具有可擴(kuò)展性和高可用性特點(diǎn),集群的必須具備以下兩大能力: · 負(fù)
3、載均衡負(fù)載均衡能把任務(wù)比較均衡地分布到集群環(huán)境下的計算和網(wǎng)絡(luò)資源。 · 錯誤恢復(fù)由于某種原因,執(zhí)行某個任務(wù)的資源出現(xiàn)故障,另一服務(wù)實(shí)體中執(zhí)行同一任務(wù)的資源接著完成任務(wù)。這種由于一個實(shí)體中的資源不能工作,另一個實(shí)體中的資源透明的繼續(xù)完成任務(wù)的過程叫錯誤恢復(fù)。 負(fù)載均衡和錯誤恢復(fù)都要求各服務(wù)實(shí)體中有執(zhí)行同一任務(wù)的資源存在,而且對于同一任務(wù)的各個資源來說,執(zhí)行任務(wù)所需的信息視圖(信息上下文)必須是一樣的。 1.4 兩大技術(shù) 實(shí)現(xiàn)集群務(wù)必要有以下兩大技術(shù): · 集群地址集群由多個服務(wù)實(shí)體組成,集群客戶端通過訪問集群的集群地址獲取集群內(nèi)部各服務(wù)實(shí)體的功能。具有單一
4、集群地址(也叫單一影像)是集群的一個基本特征。維護(hù)集群地址的設(shè)置被稱為負(fù)載均衡器。負(fù)載均衡器內(nèi)部負(fù)責(zé)管理各個服務(wù)實(shí)體的加入和退出,外部負(fù)責(zé)集群地址向內(nèi)部服務(wù)實(shí)體地址的轉(zhuǎn)換。有的負(fù)載均衡器實(shí)現(xiàn)真正的負(fù)載均衡算法,有的只支持任務(wù)的轉(zhuǎn)換。只實(shí)現(xiàn)任務(wù)轉(zhuǎn)換的負(fù)載均衡器適用于支持ACTIVE-STANDBY的集群環(huán)境,在那里,集群中只有一個服務(wù)實(shí)體工作,當(dāng)正在工作的服務(wù)實(shí)體發(fā)生故障時,負(fù)載均衡器把后來的任務(wù)轉(zhuǎn)向另外一個服務(wù)實(shí)體。 · 內(nèi)部通信為了能協(xié)同工作、實(shí)現(xiàn)負(fù)載均衡和錯誤恢復(fù),集群各實(shí)體間必須時常通信,比如負(fù)載均衡器對服務(wù)實(shí)體心跳測試信息、服務(wù)實(shí)體間任務(wù)執(zhí)行上下文信息的通信。 具有同一個集群
5、地址使得客戶端能訪問集群提供的計算服務(wù),一個集群地址下隱藏了各個服務(wù)實(shí)體的內(nèi)部地址,使得客戶要求的計算服務(wù)能在各個服務(wù)實(shí)體之間分布。內(nèi)部通信是集群能正常運(yùn)轉(zhuǎn)的基礎(chǔ),它使得集群具有均衡負(fù)載和錯誤恢復(fù)的能力。 集群配置 從上圖可知,由服務(wù)實(shí)體1、服務(wù)實(shí)體2和負(fù)載均衡器組成了一個集群。服務(wù)實(shí)體1和服務(wù)實(shí)體2參與對客戶端的服務(wù)支持工作,均衡負(fù)載器為客戶端維護(hù)集群的單一影像。集群實(shí)體間通過內(nèi)部的通信網(wǎng)交流信息,這種交流機(jī)制一般采用組播協(xié)議。負(fù)載均衡器通過內(nèi)部通信網(wǎng)探測各服務(wù)實(shí)體的心跳信息,服務(wù)實(shí)體間通過內(nèi)部通信
6、網(wǎng)完成任務(wù)資源的傳播??梢钥闯?,配置集群主要由配置服務(wù)實(shí)體和配置負(fù)載均衡器兩部分組成。本文使用tomcat 5.5.20、apache 2.2.11配置集群環(huán)境。 2.1 準(zhǔn)備軟件· tomcat是開源服務(wù)器,下載地點(diǎn) /download-55.cgi;(本文使用的apache-tomcat-5.5.20.zip)· apache是開源的www服務(wù)器,下載地點(diǎn) /download.cgi;(本文使用的apache_2.2.11-win32-x86-no_ssl.msi)
7、83; jk2模塊,jk 是mod_jserv的替代者,它是Tomcat-Apache插件,處理Tomcat和Apache之間的通信,在集群配置中充當(dāng)負(fù)載均衡器的作用。JK2是符合apache 2.x系列的新品,下載地址 /tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/。(本文使用的mod_jk-1.2.28-httpd-2.2.3.so)2.2 配置負(fù)載均衡器在apache下配置負(fù)載均衡器分為三步,注意每次修改httpd.conf和perties時不要忘了重新啟動a
8、pache。 · 第一步,安裝和調(diào)試apache 負(fù)載均衡器jk2模塊是apache www 服務(wù)的插件,所以配置負(fù)載均衡器就得先安裝apache。本文下載的是windows版本 2.2.11,執(zhí)行msi并回答一些簡單問題就可完成apache的任務(wù)。值得注意的是,安裝并啟動apache后如果apache對http:/localhost/ 地址沒反應(yīng),你得修改apache安裝路徑下htdocs目錄下的index.html.xx文件,比如把index.html.en改成index.html。 · 第二步,安裝jk2把下載的 mod_jk-1.2.28-ht
9、tpd-2.2.3.so 改名為 mod_jk2.so 放到apache的modules目錄下,修改apache的conf目錄下的httpd.conf,在文件最后加入一行include confmod_jk2.conf,在conf目錄下創(chuàng)建mod_jk2.conf文件,內(nèi)容如下: Conf代碼 1. # Load mod_jk2 module 2. LoadModule jk_module modules/mod_jk2.so 3. 4. 5. #&
10、#160;Where to find perties 6. JkWorkersFile conf/perties 7. 8. 9. # Where to put jk logs 10. JkLogFile logs/mod_jk2.log 11. 12. 13.
11、# Set the jk log level debug/error/info 14. JkLogLevel info 15. 16. 17. # Select the log format 18. JkLogStampFormat "%a %b %d %H:%M:%S %Y "
12、; 19. 20. 21. # JkOptions indicate to send SSL KEY SIZE, 22. JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories 23. 24. 25. # JkRequestLogFormat set
13、160;the request format 26. JkRequestLogFormat "%w %V %T" 27. 28. # Send JSPs for context / to worker named loadBalancer 29. JkMount /*.jsp loadBalancer
14、# Load mod_jk2 moduleLoadModule jk_module modules/mod_jk2.so# Where to find pertiesJkWorkersFile conf/perties# Where to put jk logsJkLogFile logs/mod_jk2.log# Set the jk log level debug/error/infoJkLogLevel info# Select the log formatJkLogStampFormat "%a %b %d %H:%M:%S %Y
15、"# JkOptions indicate to send SSL KEY SIZE,JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories# JkRequestLogFormat set the request formatJkRequestLogFormat "%w %V %T"# Send JSPs for context / to worker named loadBalancerJkMount /*.jsp loadBalancer · 第三步,配置jk2jk2的
16、配置全在一個配置文件中,文件名為perties,和apache 的httpd.conf放在同一個目錄下。以下是這個文件的內(nèi)容: Properties代碼 1. # 2. # perties 3. # 4. 5. # list the workers by name 6. 7. worker.list=tomcat1, tomcat2,
17、60;loadBalancer 8. 9. # - 10. # First tomcat server 11. # - 12. worker.tomcat1.port=8009 13. worker.tomcat1.host=07 14. worker.tomcat1.type=ajp13 15. 16. #
18、60;Specify the size of the open connection cache. 17. #worker.tomcat1.cachesize 18. 19. # 20. # Specifies the load balance factor when used with 21. # a lo
19、ad balancing worker. 22. # Note: 23. # -> lbfactor must be > 0 24. # -> Low lbfactor means less work done by the worker. 25. worker.tomcat1.lbfactor=
20、100 26. 27. 28. # - 29. # Second tomcat server 30. # - 31. worker.tomcat2.port=8009 32. worker.tomcat2.host=63 33. worker.tomcat2.type=ajp13 34. 35
21、. # Specify the size of the open connection cache. 36. #worker.tomcat2.cachesize 37. 38. # 39. # Specifies the load balance factor when used with 40. # a
22、160;load balancing worker. 41. # Note: 42. # -> lbfactor must be > 0 43. # -> Low lbfactor means less work done by the worker. 44. worker.tomcat2.lbf
23、actor=100 45. 46. 47. # - 48. # Load Balancer worker 49. # - 50. 51. # 52. # The loadbalancer (type lb) worker performs weighted round-robin
24、 53. # load balancing with sticky sessions. 54. # Note: 55. # -> If a worker dies, the load balancer will check its state 56. # once in a w
25、hile. Until then all work is redirected to peer 57. # worker. 58. worker.loadBalancer.type=lb 59. worker.loadBalancer.balanced_workers=tomcat1, tomcat2 60. 61. # 62. # END
26、160;perties 63. # # perties# list the workers by nameworker.list=tomcat1, tomcat2, loadBalancer# -# First tomcat server# -worker.tomcat1.port=8009worker.tomcat1.host=07worker.tomcat1.type=ajp13# Specify the size of the open connection cache.#wo
27、rker.tomcat1.cachesize# Specifies the load balance factor when used with# a load balancing worker.# Note:# -> lbfactor must be > 0# -> Low lbfactor means less work done by the worker.worker.tomcat1.lbfactor=100# -# Second tomcat server# -worker.tomcat2.port=8009worker.tomcat2.host=192.168.0
28、.163worker.tomcat2.type=ajp13# Specify the size of the open connection cache.#worker.tomcat2.cachesize# Specifies the load balance factor when used with# a load balancing worker.# Note:# -> lbfactor must be > 0# -> Low lbfactor means less work done by the worker.worker.tomcat2.lbfactor=100#
29、 -# Load Balancer worker# -# The loadbalancer (type lb) worker performs weighted round-robin# load balancing with sticky sessions.# Note:# -> If a worker dies, the load balancer will check its state# once in a while. Until then all work is redirected to peer# worker.worker.loadBalancer.type=lbwor
30、ker.loadBalancer.balanced_workers=tomcat1, tomcat2# END perties# 對于jk2模塊的負(fù)載均衡配置可參見相關(guān)站點(diǎn),值得提的是jk2的負(fù)載均衡還支持權(quán)重分配等優(yōu)秀功能。測試:在跑tomcat1實(shí)例的機(jī)器的 webapps/testGroup 測試項目目錄生成如下內(nèi)容的index.jsp文件:(設(shè)置session)Jsp代碼 1. <% 2. System.out.println("=");
31、60;3. System.out.println(session.getAttribute("test"); 4. session.setAttribute("test","Session"); 5. %> 6. 7. <html> 8. <body bgcolor="red&q
32、uot;> 9. <center> 10. <h1>Tomcat 1</h1> 11. </body> 12. </html> <% System.out.println("="); System.out.println(session.getAttribut
33、e("test"); session.setAttribute("test","Session");%><html> <body bgcolor="red"> <center> <h1>Tomcat 1</h1> </body></html> 同理,在跑tomcat2實(shí)例的機(jī)器的 webapps/testGroup 測試項目目錄生成如下內(nèi)容的index.jsp文件:Jsp代碼 1. <%
34、; 2. System.out.println("="); 3. System.out.println(session.getAttribute("test"); 4. %> 5. 6. <html> 7. <body bgcolor="blue">
35、;8. <center> 9. <h1>Tomcat 2</h1> 10. </body> 11. </html> <% System.out.println("="); System.out.println(session.getAttribute("test");%
36、><html> <body bgcolor="blue"> <center> <h1>Tomcat 2</h1> </body></html> 啟動Tomcat1, Tomcat2 和Apache服務(wù)Apache服務(wù)器ip:05Tomcat1服務(wù)器ip:07Tomcat2服務(wù)器ip:63首先驗(yàn)證Apache服務(wù)器的靜態(tài)頁面是否正常,訪問:05/(若局域網(wǎng)內(nèi)不能訪問請檢查防火墻設(shè)
37、置)驗(yàn)證tomcat和apache配置提供負(fù)載均衡,用不同的終端訪問:05/testGroup/index.jsp如果你看到紅色的頁面,表示為tomcat1 服務(wù)器返回的,如果你看到蘭色的頁面,表示為tomcat2 服務(wù)器返回的。 2.3 配置tomcat(綁定session) 同屬于一個集群下的兩個服務(wù)實(shí)體,要求功能的同一性,所以我們可先安裝和配置第一個tomcat,接著拷貝形成第二個tomcat,最后配置第二個tomcat。2.3.1修改tomcat1, tomcat2的server.xml 修改 <Engine /
38、> 標(biāo)簽在跑第一個tomcat實(shí)例的機(jī)器上,在大約第120行(去掉注釋), 將<Engine name="Standalone" defaultHost="localhost" debug="0">替換為:<Engine jvmRoute="tomcat1" name="Standalone" defaultHost="05" debug="0" />對跑第二個tomcat實(shí)例的機(jī)器上,替換為 jvmRo
39、ute="tomcat2".Xml代碼 1. <!- You should set jvmRoute to support load-balancing via AJP ie : 2. <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
40、0;3. -> 4. <Engine jvmRoute="tomcat2" name="Standalone" defaultHost="05" debug="0" /> 5. <!- You should set jvmRoute to support load-balancing via
41、AJP ie :<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">-> <Engine jvmRoute="tomcat2" name="Standalone" defaultHost="05" debug="0" /> 有的文檔寫得是在配置中加入一行<Engine jvmRoute="tomca
42、t1" name="Standalone" defaultHost="localhost" debug="0"></Engine>,其實(shí)是不對的,這樣負(fù)載均衡是沒有問題的,但是不能做session綁定,也就是說同一用戶會在兩臺服務(wù)器上跳來跳去。這是由于jvmRoute不正確引起的,因?yàn)槿绻皇羌尤胍恍械脑?,是沒有什么作用的,Engine仍然是原來的 Engine,而原來的Engine中jvmRoute是不正確的,所以一定要替換。而且這個jvmRoute一定要跟 perties 中的名稱
43、完全匹配,否則也不能實(shí)現(xiàn)session綁定. 修改 <Cluster /> 標(biāo)簽將<Cluster />標(biāo)簽注釋去掉,代碼如下:Xml代碼 1. <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" 2.
44、0;managerClassName="org.apache.catalina.cluster.session.DeltaManager" 3. expireSessionsOnShutdown="false" 4.
45、; useDirtyFlag="true" 5. notifyListenersOnReplication="true"> 6. 7.
46、160; <Membership 8. className="org.apache.catalina.cluster.mcast.McastService" 9.
47、60; mcastAddr="" 10. mcastPort="45564" 11.
48、60; mcastFrequency="500" 12. mcastDropTime="3000"/> 13. 14.
49、0; <Receiver 15. className="org.apache.catalina.cluster.tcp.ReplicationListener" 16.
50、 tcpListenAddress="auto" 17. tcpListenPort="4001" 18. &
51、#160; tcpSelectorTimeout="100" 19. tcpThreadCount="6"/> 20. 21.
52、 <Sender 22. className="org.apache.catalina.cluster.tcp.ReplicationTransmitter" 23.
53、160; replicationMode="pooled" 24. ackTimeout="15000" 25.
54、0; waitForAck="true"/> 26. 27. <Valve className="org.apache.catalina.cluster.tcp.ReplicationValve" 28. &
55、#160; filter=".*.gif;.*.js;.*.jpg;.*.png;.*.htm;.*.html;.*.css;.*.txt;"/> 29. 30.
56、160; <Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer" 31. temp
57、Dir="/tmp/war-temp/" 32. deployDir="/tmp/war-deploy/" 33.
58、160; watchDir="/tmp/war-listen/" 34. watchEnabled="false"/> 35.
59、 36. <ClusterListener className="org.apache.catalina.cluster.session.Cl
60、usterSessionListener"/> 37. </Cluster> <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster" managerClassName="org.apache.catalina.cluster.session.DeltaManager" expireSessionsOn
61、Shutdown="false" useDirtyFlag="true" notifyListenersOnReplication="true"> <Membership className="org.apache.catalina.cluster.mcast.McastService" mcastAddr="" mcastPort="45564" mcastFrequency="500" mcastDropTime="3000"/> <Receiver className="org.apache.catalina.cluster.tcp.ReplicationListener&
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 出口碗碟采購合同范本
- 業(yè)務(wù)代表合同范本
- Unit 2 SectionA(1a~1e)教學(xué)設(shè)計- 2024-2025學(xué)年人教版(2024)七年級英語下冊
- 2024年招商銀行唐山分行社會招聘考試真題
- 出租稻田土地合同范本
- 2024年咸陽市實(shí)驗(yàn)中學(xué)教師招聘筆試真題
- 借款公證合同范本
- 買車退款合同范本
- 住建備案合同范本
- 分包轉(zhuǎn)讓合同范本
- 代理分銷銷售協(xié)議書
- 2024年江蘇農(nóng)牧科技職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫參考答案
- 中國類風(fēng)濕關(guān)節(jié)炎診療指南(2024版)解讀
- 《社會治理概論》課程教學(xué)大綱
- 讀書分享《非暴力溝通》課件(圖文)
- 鉗工實(shí)訓(xùn)安全
- 2024年3月30日事業(yè)單位聯(lián)考D類《職業(yè)能力傾向測驗(yàn)》試題
- 通信施工安全培訓(xùn)
- 智慧生活:AI與智能家居-揭秘未來智能化生活趨勢
- 大單元教學(xué)設(shè)計基本步驟
- 消渴癥護(hù)理查房
評論
0/150
提交評論