環(huán)境:
Tomcat 5.5.20
Apache 2.2.3
1.機(jī)器A 里有TomcatA\webapps\Test目錄, 機(jī)器B里TomcatB\webapps\Test目錄。其web.xml文件一樣,兩個(gè)都在</web-app>之前加上<distributable/>
2.兩個(gè)Tomcat\conf\server.xml的如下內(nèi)容去掉注釋?zhuān)瑳](méi)有就在</Host>之前加上去:
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
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>
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
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.接下來(lái)配置Apache\conf\httpd.conf
其中這三個(gè)去掉注釋?zhuān)?br>
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
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://192.168.0.100:8080
BalancerMember http://192.168.0.101:8080
</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://192.168.0.100:8080
BalancerMember http://192.168.0.101:8080
</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>
說(shuō)明:
ProxyPass /helloworld balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://
BalancerMember http://
</Proxy>
ProxyPass為代理轉(zhuǎn)發(fā)的Url,即將所有訪(fǎng)問(wèn)/helloworld的請(qǐng)求轉(zhuǎn)發(fā)到群集balancer://mycluster
BalancerMember為群集的成員,即群集服務(wù)器A或B,負(fù)載均衡服務(wù)器會(huì)根據(jù)均衡規(guī)則來(lái)將請(qǐng)求轉(zhuǎn)發(fā)給BalancerMember。
4.測(cè)試頁(yè)面,
TomcatA\webapps\Test\index.html內(nèi)容如下:
<html>
<head><title>Tomcat 5.5.20 群集1</title></head>
<body>
<center>Tomcat 1號(hào)機(jī)
</center>
</body>
</html>
<head><title>Tomcat 5.5.20 群集1</title></head>
<body>
<center>Tomcat 1號(hào)機(jī)
</center>
</body>
</html>
TomcatB\webapps\Test\index.html內(nèi)容如下:
<html>
<head><title>Tomcat 5.5.20 群集2</title></head>
<body>
<center>Tomcat 2號(hào)機(jī)在TomcatB機(jī)上
</center>
</body>
</html>
<head><title>Tomcat 5.5.20 群集2</title></head>
<body>
<center>Tomcat 2號(hào)機(jī)在TomcatB機(jī)上
</center>
</body>
</html>
啟動(dòng)TomcatA和TomcatB,再啟動(dòng)Apache
http://localhost/helloworld/Test/index.html
后有出現(xiàn)上面兩個(gè)頁(yè)面的任一個(gè)說(shuō)明配置成功。
不斷刷新,這個(gè)頁(yè)面不斷交替。
:)