Tomcat群集配置
| 文章出處:www.figol.cn | 日期: |
大型的企業(yè)應用每天都需要承受巨大的訪(fǎng)問(wèn)量,在著(zhù)巨大訪(fǎng)問(wèn)量的背后有數臺服務(wù)器支撐著(zhù),如果一臺服務(wù)器崩潰了,那么其他服務(wù)器可以使企業(yè)應用繼續運行,用戶(hù)對服務(wù)器的運作是透明化的,如何實(shí)現這種透明化呢?由如下問(wèn)題需要解決。
一.Session的復制
二.如何將請求發(fā)送到正常的服務(wù)器
針對以上問(wèn)題,可以使用群集和負載均衡來(lái)解決,整體架構如下:

中間由一臺服務(wù)器做負載均衡(Load Balancer),它將所有請求,根據一定的負載均衡規則發(fā)送給指定的群集服務(wù)器(Cluster),群集服務(wù)器擁有著(zhù)相同的狀態(tài)和相同的應用程序,并且他們的Session是相互復制的,這樣,不管訪(fǎng)問(wèn)哪臺服務(wù)器都具有相同的結果,即使一臺服務(wù)器崩潰掉以后,可以由其他集群服務(wù)器繼續負責應用程序的運行。
我們假設有如下場(chǎng)景,一臺負載均衡服務(wù)器負責請求的均衡,群集服務(wù)器A和群集服務(wù)器B組成一個(gè)群集,當某個(gè)群集服務(wù)器崩潰后,另外一臺繼續負責應用程序的運行。
一. 配置Tomcat
修改Tomcat配置文件server.xml
1.群集服務(wù)器A的端口號與B不沖突,即使Server Port,Connector,Coyote/JK2 AJP Connector的端口號唯一
2.在Host元素下增加以下內容:
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<!--每個(gè)群集服務(wù)器都需要有相同的Membership配置-->
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<!--tcpListenAddress:本機IP地址 服務(wù)器將此地址廣播給其他群集服務(wù)器-->
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="15000"
waitForAck="true"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
3.修改Web應用程序配置文件web.xml
在web.xml文件中<web-app>元素下增加以下內容:
<!--此應用將與群集服務(wù)器復制Session-->
<distributable/>
二. 配置Tomcat
與群集服務(wù)器A配置基本相同,唯一不同的地方就是server.xml文件中
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="
tcpListenPort="4002"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
tcpListenAddress應為本機地址,如果兩臺群集服務(wù)器在一臺機器上,則端口號要不同
注意:B的其他端口不要與A沖突。
三. 群集服務(wù)器具體配置結果
| 配置參數 | 群集服務(wù)器 A | 群集服務(wù)器B |
| | 9005 | 10005 |
| Connector | 9080 | 10080 |
| Coyote/JK2 AJP Connector | 9009 | 10009 |
| Cluster mcastAddr | 228.0.0.4 | 228.0.0.4 |
| Cluster mcastPort | 45564 | 45564 |
| tcpListenAddress | 本機IP地址 | 本機IP地址 |
| Cluster tcpListenPort | 4001 | 4002 |
Mcast* 用于廣播,所有群集服務(wù)器需要填寫(xiě)相同的配置
tcpListen* 本機的IP,群集服務(wù)器啟動(dòng)時(shí),會(huì )將自己的IP和端口號廣播出去,其他群集服務(wù)器收到后,響應廣播發(fā)出者。
四. 測試群集
啟動(dòng)群集服務(wù)器A,再啟動(dòng)群集服務(wù)器B會(huì )顯示群集服務(wù)器的信息,表示群集服務(wù)器配置成功
五. 配置負載均衡服務(wù)器Apache
現在雖然群集已經(jīng)有了相同的狀態(tài),但需要不同的IP地址才能訪(fǎng)問(wèn)到服務(wù)器A與B,現在我們配置一臺負載均衡服務(wù)器來(lái)實(shí)現統一的入口訪(fǎng)問(wèn),和負載的均衡。
下載Apache服務(wù)器
修改httpd.conf文件
將以下Module的注釋去掉
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
并增加以下元素
ProxyRequests Off
ProxyPass /helloworld balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://
BalancerMember http://
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all
</Location>
其中
ProxyPass /helloworld balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://
BalancerMember http://
</Proxy>
ProxyPass為代理轉發(fā)的Url,即將所有訪(fǎng)問(wèn)/helloworld的請求轉發(fā)到群集balancer://mycluster
BalancerMember為群集的成員,即群集服務(wù)器A或B,負載均衡服務(wù)器會(huì )根據均衡規則來(lái)將請求轉發(fā)給BalancerMember。
配置好后,啟動(dòng)Apahce服務(wù)器,訪(fǎng)問(wèn)localhost/hellworld就會(huì )看到群集服務(wù)器中應用返回的結果。恭喜你,負載均衡和群集已經(jīng)配置成功了。
參考文檔:
Clustering and Load Balancing in Tomcat 5, Part 1 by Srini Penchikala
Clustering and Load Balancing in Tomcat 5, Part 2 by Srini Penchikala
聯(lián)系客服