Tomcat群集配置
文章出處:www.figol.cn | 日期: |
大型的企業(yè)應用每天都需要承受巨大的訪問量,在著巨大訪問量的背后有數(shù)臺服務器支撐著,如果一臺服務器崩潰了,那么其他服務器可以使企業(yè)應用繼續(xù)運行,用戶對服務器的運作是透明化的,如何實現(xiàn)這種透明化呢?由如下問題需要解決。
一.Session的復制
二.如何將請求發(fā)送到正常的服務器
針對以上問題,可以使用群集和負載均衡來解決,整體架構如下:
中間由一臺服務器做負載均衡(Load Balancer),它將所有請求,根據(jù)一定的負載均衡規(guī)則發(fā)送給指定的群集服務器(Cluster),群集服務器擁有著相同的狀態(tài)和相同的應用程序,并且他們的Session是相互復制的,這樣,不管訪問哪臺服務器都具有相同的結果,即使一臺服務器崩潰掉以后,可以由其他集群服務器繼續(xù)負責應用程序的運行。
我們假設有如下場景,一臺負載均衡服務器負責請求的均衡,群集服務器A和群集服務器B組成一個群集,當某個群集服務器崩潰后,另外一臺繼續(xù)負責應用程序的運行。
一. 配置Tomcat
修改Tomcat配置文件server.xml
1.群集服務器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">
<!--每個群集服務器都需要有相同的Membership配置-->
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<!--tcpListenAddress:本機IP地址 服務器將此地址廣播給其他群集服務器-->
<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>元素下增加以下內容:
<!--此應用將與群集服務器復制Session-->
<distributable/>
二. 配置Tomcat
與群集服務器A配置基本相同,唯一不同的地方就是server.xml文件中
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="
tcpListenPort="4002"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
tcpListenAddress應為本機地址,如果兩臺群集服務器在一臺機器上,則端口號要不同
注意:B的其他端口不要與A沖突。
三. 群集服務器具體配置結果
配置參數(shù) | 群集服務器 A | 群集服務器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* 用于廣播,所有群集服務器需要填寫相同的配置
tcpListen* 本機的IP,群集服務器啟動時,會將自己的IP和端口號廣播出去,其他群集服務器收到后,響應廣播發(fā)出者。
四. 測試群集
啟動群集服務器A,再啟動群集服務器B會顯示群集服務器的信息,表示群集服務器配置成功
五. 配置負載均衡服務器Apache
現(xiàn)在雖然群集已經有了相同的狀態(tài),但需要不同的IP地址才能訪問到服務器A與B,現(xiàn)在我們配置一臺負載均衡服務器來實現(xiàn)統(tǒng)一的入口訪問,和負載的均衡。
下載Apache服務器
修改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,即將所有訪問/helloworld的請求轉發(fā)到群集balancer://mycluster
BalancerMember為群集的成員,即群集服務器A或B,負載均衡服務器會根據(jù)均衡規(guī)則來將請求轉發(fā)給BalancerMember。
配置好后,啟動Apahce服務器,訪問localhost/hellworld就會看到群集服務器中應用返回的結果。恭喜你,負載均衡和群集已經配置成功了。
參考文檔:
Clustering and Load Balancing in Tomcat 5, Part 1 by Srini Penchikala
Clustering and Load Balancing in Tomcat 5, Part 2 by Srini Penchikala