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