此類話題已經很多,可以參考的文檔也不少,我是參考這個blog的文檔做的:
http://blogger.org.cn/blog/blog.asp?name=lhwork
1)環境和版本:
Apache2.0.59+Tomcat5.5.15(兩個),一開始用的是Tomcat5.0.28,一直有問題(在后面總結),就升級到5.5去做試驗了,冤枉5.0.28兄弟了:)
步驟小結為:
1、安裝(忽略);
2、配置Tomcat:
???1)第一個Tomcat:
??????A.啟用jvmRoute??????
??? <!-- You should set jvmRoute to support load-balancing via AJP ie :-->
??? <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">??????????
??? <!-- Define the top level container in our container hierarchy
??? <Engine name="Catalina" defaultHost="localhost">-->
??????B.啟用原來禁用的Cluster設置
??????
??????? <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"
????????????????mcastBindAddress="127.0.0.1"
??????????????? mcastAddr="224.1.2.3"
??????????????? mcastPort="2525"
??????????????? 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"/>
??????????? <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>
??????C.搞定
???2)配置第二個Tomcat(要注意端口沖突了):
??????A.變更端口
??????<Server port="8005" shutdown="SHUTDOWN">
??????=》
??????<Server port="8004" shutdown="SHUTDOWN">
??????B.變更端口
??????<Connector port="8009"
?????????????? enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
??????=》
??????<Connector port="9009"
?????????????? enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
??????C.啟用jvmRoute??????
??? <!-- You should set jvmRoute to support load-balancing via AJP ie :-->
??? <Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat2">??????????
??? <!-- Define the top level container in our container hierarchy
??? <Engine name="Catalina" defaultHost="localhost">-->
??????D.啟用原來禁用的Cluster設置??????
??????? <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"
????????????????mcastBindAddress="127.0.0.1"
??????????????? mcastAddr="224.1.2.3"
??????????????? mcastPort="2525"
??????????????? mcastFrequency="500"
??????????????? mcastDropTime="3000"/>
??????????? <Receiver
??????????????? className="org.apache.catalina.cluster.tcp.ReplicationListener"
??????????????? tcpListenAddress="auto"
??????????????? tcpListenPort="4002"
??????????????? tcpSelectorTimeout="100"
??????????????? tcpThreadCount="6"/>
??????????? <Sender
??????????????? className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
??????????????? replicationMode="pooled"
??????????????? ackTimeout="15000"/>
??????????? <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>
??????E.搞定
3、配置apache:
???1)修訂conf/httpd.conf??????
??????#add by zhengxq
??????LoadModule jk2_module modules/mod_jk2.so
???2)新增worders2.properties并放到conf下
??????[shm]
info=Scoreboard. Requried for reconfiguration and status with multiprocess servers.
file=anon
# Defines a load balancer named lb. Use even if you only have one machine.
[lb:lb]
worker=ajp13:tomcat1
worker=ajp13:tomcat2
# Example socket channel, override port and host.
[channel.socket:localhost:9009]
port=9009
host=127.0.0.1
# define the worker
[ajp13:localhost:9009]
channel=channel.socket:localhost:9009
group=lb
# Example socket channel, override port and host.
[channel.socket:localhost:8009]
port=8009
host=127.0.0.1
# define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
group=lb
# Map the Tomcat examples webapp to the Web server uri space
[uri:/clusterapp/*]
group=lb
??????3)搞定
3.測試程序,請參考上述參考文檔,如下:
<%@ page contentType="text/html; charset=GBK" import="java.util.*"%>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info: <%out.print(request.getLocalAddr() + " : " + request.getLocalPort());%>
<%
out.println("<br> ID " + session.getId());
// 如果有新的 Session 屬性設置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
? String dataValue = request.getParameter("dataValue");
? session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
? String name = (String)e.nextElement();
? String value = session.getAttribute(name).toString();
? out.println( name + " = " + value);
? }
%>
<form action="index.jsp" method="POST">
? 名稱:<input type=text size=20 name="dataName">
? <br>
? 值:<input type=text size=20 name="dataValue">
? <br>
? <input type=submit>
? </form>
</body>
</html>
???所需要注意的是:
???1、測試應用的web.xml必須加上:<distributable/>
???2、我搞了很久,發現三個東西起來后,可以訪問,但只能做到負載均衡,不能session復制,這點我重復配置了一次,就是不行,郁悶之下找了tomcat的郵件列表,這樣做:
??????1)測試廣播是否正常
?????????A. download this jar
????????????http://cvs.apache.org/~fhanik/tomcat-replication.jar
?????????B. Open two terminals?
??????????? a) Terminal one run
???????????????java -cp tomcat-replication.jar MCaster 239.255.10.10 2525 Terminal1
??????????? b) Terminal two run
???????????????java -cp tomcat-replication.jar MCaster 239.255.10.10 2525 Terminal2
?????????發現結果如下:
?????????C:\>java -cp tomcat-replication.jar MCaster 224.1.2.3 2525 Terminal1
Usage MCaster [address port message]
BEGIN TO RECEIVE
SENT:Terminal11
SENT:Terminal12
SENT:Terminal13
SENT:Terminal14
SENT:Terminal15
SENT:Terminal16
SENT:Terminal17
SENT:Terminal18
?????????表明廣播有問題,氣死了,難怪Tomcat總是在啟動的時候提示:
?????????信息: Manager [/clusterapp]: skipping state transfer. No members active in cluster group.
??????后來終于發現貓膩,必須注意在server.xml的<Cluster ..<Membership里面加上mcastBindAddress="127.0.0.1"。
???????為什么呢?很簡單,因為我安裝了VPN,這家伙會設置一個虛擬網卡,導致綁定失?。]地方責怪了,只好讓它墊背)?!這是通過郵件列表里面所描述的多網卡綁定問題舉一反三得到的判斷。具體請參考:http://www.servlets.com/archive/servlet/ReadMsg?msgId=475067&listName=tomcat-user
??????