itstarting:IT进行时

想自己所想,做自己所爱

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  28 随笔 :: 3 文章 :: 55 评论 :: 0 Trackbacks

此类话题已经很多,可以参考的文档也不少,我是参考这个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

      


posted on 2007-03-07 10:51 IT进行时 阅读(7228) 评论(5)  编辑  收藏 所属分类: Java Tips

评论

# re: 关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决 2007-03-08 08:48 不好说
还在用jk2啊??  回复  更多评论
  

# re: 关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决 2007-03-08 16:02 花落知多少
to 不好说 同志:老实说,这方面没有研究,纯粹拿来主意,用的是jakarta-tomcat-connectors-jk2.0.4-win32-apache2.0.49所提供的so文件。你有更好的选择?请多指教!
  回复  更多评论
  

# re: 关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决 2008-03-03 23:57 tomcat-user
我也遇到这个问题了,看到你的解决方案后解决了~ 谢谢了~  回复  更多评论
  

# re: 关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决 2009-12-14 23:31 xie
skipping state transfer. No members active in cluster group.


我加了那个 mcastBindAddress="127.0.0.1"。


可还是只能负载均衡,不能session复制...

楼主帮忙。。。  回复  更多评论
  

# re: 关于Apache+Tomcat的Cluster的郁闷问题,终于得到解决 2009-12-15 22:48 IT进行时
可以测试一下广播,应该是配置问题或是受到了干扰  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: