Apache2.2+Tomcat6配置集群、负载均衡、session复制随笔

一直以来都在javaeye潜水,得到了很多人的经验分享,这两天在学习tomcat配置集群的过程中,积累了一点东西觉得应该与大家分享。关于Tomcat和Apache的连接方式,请参阅 http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/index.html 。详细的配置过程,不再重复描述,请先看看罗代均http://hi.baidu.com/luodaijun/blog/item/5bbe4cfb5ffef864034f56a1.html ,我把配置过程中几个不同和关键的地方重点提出来。
软件版本有所不同,罗列如下:
Apache  :apache_2.2.11-win32-x86-openssl-0.9.8i.msi 1个
Tomcat:  apache-tomcat-6.0.14.zip  2个
mod_jk: mod_jk-1.2.27-httpd-2.2.10.so 1个
  我准备了两台机器,192.1168.1.5 192.168.1.3,你也可以在一台机器上进行测试。
把apache和tomcat安装好以后,配置过程中有如下一些地方改动或者改进。
2.修改Apache配置文件http.conf
引入mod_jk时候,可以使用
Include conf/mod_jk.conf 可以不使用全路径引入,方便管理。
3.在http.conf同目录下新建 workers.properties文件,内容如下

  1. #========tomcat1========  
  2. worker.tomcat1.port=8009          #ajp13 端口号,在tomcat下server.xml配  置,默认 8009   
  3. worker.tomcat1.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址  
  4. worker.tomcat1.type=ajp13  
  5. worker.tomcat1.lbfactor = 1    #server的加权比重,值越高,分得的请求越多  
  6.   
  7. #========tomcat2========  
  8. worker.tomcat2.port=9009        #ajp13 端口号,在tomcat下server.xml配置,  
  9. worker.tomcat2.host=192.168 . 1.3  #tomcat的主机地址,如不为本机,请填写ip地址  
  10. worker.tomcat2.type=ajp13  
  11. worker.tomcat2.lbfactor = 1    #server的加权比重,值越高,分得的请求越多  
  12. #========controller,负载均衡控制器========  
  13. worker.controller.type=lb  
  14. worker.controller.balanced_workers=tomcat1,tomcat2   #指定分担请求的tomcat  
  15. worker.controller.sticky_session=false  #worker.controller.sticky_session= 1   
#========tomcat1========
worker.tomcat1.port=8009         #ajp13 端口号,在tomcat下server.xml配  置,默认8009
worker.tomcat1.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1   #server的加权比重,值越高,分得的请求越多

#========tomcat2========
worker.tomcat2.port=9009       #ajp13 端口号,在tomcat下server.xml配置,
worker.tomcat2.host=192.168.1.3 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1   #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2   #指定分担请求的tomcat
worker.controller.sticky_session=false #worker.controller.sticky_session=1


以上要说明的是原文中worker.controller.sticky_session=1,等同于 worker.controller.sticky_session=true.此处指定集群是否需要会话复制,如果设为true,则表明为会话粘性,不进行会话复制,当某用户的请求第一次分发到哪台Tomcat后,后继的请求会一直分发到此Tomcat服务器上处理;如果设为false,则表明需求会话复制。这就是说,如果你要想进行均衡负载,并在192.168.1.5和192.168.1.3两台机器的Tomcat实例上实现集群全局session复制,此处值要设置成false。
第二部分,配置集群

 

  1. 把server.xml中注释部分  
  2. <!--   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" />-->  
  3. 替换如下cluster配置内容:  
  4. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   
  5.                  channelSendOptions="8" >  
  6.           <!--    
  7.             
  8.           <Manager className="org.apache.catalina.ha.session.BackupManager"   
  9.                     expireSessionsOnShutdown="false"   
  10.                     notifyListenersOnReplication="true"   
  11.                     mapSendOptions="8" /> -->  
  12.           <!--   -->  
  13.           <Manager className="org.apache.catalina.ha.session.DeltaManager"   
  14.                    expireSessionsOnShutdown="false"   
  15.                    notifyListenersOnReplication="true" />  
  16.   
  17.           <Channel className="org.apache.catalina.tribes.group.GroupChannel" >  
  18.             <Membership className="org.apache.catalina.tribes.membership.McastService"   
  19.                         address="228.0.0.4"   
  20.                         port="45564"   
  21.                         frequency="500"   
  22.                         dropTime="3000" />  
  23.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"   
  24.                       address="auto"   
  25.                       port="4000"   
  26.                       autoBind="100"   
  27.                       selectorTimeout="5000"   
  28.                       maxThreads="6" />  
  29.             <!-- timeout="60000" -->  
  30.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter" >  
  31.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"  />  
  32.             </Sender>  
  33.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />  
  34.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />  
  35.          <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />  
  36.           </Channel>  
  37.   
  38.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"   
  39.                  filter="" />  
  40.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
  41.   
  42.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"   
  43.                     tempDir="/tmp/war-temp/"   
  44.                     deployDir="/tmp/war-deploy/"   
  45.                     watchDir="/tmp/war-listen/"   
  46.                     watchEnabled="false" />  
  47.   
  48.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />  
  49.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />  
  50.         </Cluster>      
把server.xml中注释部分
<!--   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>-->
替换如下cluster配置内容:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">
          <!--  
		  
		  <Manager className="org.apache.catalina.ha.session.BackupManager"
					expireSessionsOnShutdown="false"
					notifyListenersOnReplication="true"
					mapSendOptions="8"/> -->
		  <!--   -->
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>

          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>
            <!-- timeout="60000"-->
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
         <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>    


按罗君一文配置完后,请分别启动两个tomcat(启动一个tomcat,等它完全启动后,再启动另外一个tomcat),再启动apache。启动完成后,其中一个tomcat的控制台信息片段如下:

  1. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.ha.session.DeltaManager start  
  2. ??: [color=red]Starting clustering manager at  
  3. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.ha.session.DeltaManager getAllClusterSess  
  4. ions  
  5. ??: Manager [localhost#], requesting session state from org.apache.catalina.tr  
  6. ibes.membership.MemberImpl[tcp://3645A51F0B7E4CA:4001,3645A51F0B7E4CA,4001, aliv   
  7. e=29031 ,id={ 112   55   65   24   117   66   68   118  - 122  - 5  - 53   2  - 35  - 90  - 8  - 95  }, payload={  
  8. }, command={}, domain={}, ]. This operation will timeout if  no session state has  
  9.  been received within 60  seconds.[/color] 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.tribes.group.interceptors.ThroughputInter  
  10. ceptor report  
  11. ??: ThroughputInterceptor Report[  
  12.         Tx Msg:1  messages  
  13.         Sent:0.00  MB (total)  
  14.         Sent:0.00  MB (application)  
  15.         Time:0.02  seconds  
  16.         Tx Speed:0.03  MB/sec (total)  
  17.         TxSpeed:0.03  MB/sec (application)  
  18.         Error Msg:0   
  19.         Rx Msg:1  messages  
  20.         Rx Speed:0.00  MB/sec (since 1st msg)  
  21.         Received:0.00  MB]  
  22.   
  23. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.ha.session.DeltaManager waitForSendAllSes  
  24. sions  
  25. ??: Manager [localhost#]; session state send at 09 - 1 - 28  ?? 2 : 47  received in  2   
  26. 65  ms.  
  27. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.ha.session.JvmRouteBinderValve start  
  28. ??: JvmRouteBinderValve started  
  29. 2009 - 1 - 28   14 : 47 : 12  org.apache.coyote.http11.Http11Protocol start  
  30. ??: Starting Coyote HTTP/1.1  on http- 8000   
  31. 2009 - 1 - 28   14 : 47 : 12  org.apache.jk.common.ChannelSocket init  
  32. ??: JK: ajp13 listening on /0.0 . 0.0 : 8009   
  33. 2009 - 1 - 28   14 : 47 : 12  org.apache.jk.server.JkMain start  
  34. ??: Jk running ID=0  time= 0 / 31   config= null   
  35. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.startup.Catalina start  
  36. ??: Server startup in 2818  ms  
2009-1-28 14:47:12 org.apache.catalina.ha.session.DeltaManager start
??: [color=red]Starting clustering manager at
2009-1-28 14:47:12 org.apache.catalina.ha.session.DeltaManager getAllClusterSess
ions
??: Manager [localhost#], requesting session state from org.apache.catalina.tr
ibes.membership.MemberImpl[tcp://3645A51F0B7E4CA:4001,3645A51F0B7E4CA,4001, aliv
e=29031,id={112 55 65 24 117 66 68 118 -122 -5 -53 2 -35 -90 -8 -95 }, payload={
}, command={}, domain={}, ]. This operation will timeout if no session state has
 been received within 60 seconds.[/color]2009-1-28 14:47:12 org.apache.catalina.tribes.group.interceptors.ThroughputInter
ceptor report
??: ThroughputInterceptor Report[
        Tx Msg:1 messages
        Sent:0.00 MB (total)
        Sent:0.00 MB (application)
        Time:0.02 seconds
        Tx Speed:0.03 MB/sec (total)
        TxSpeed:0.03 MB/sec (application)
        Error Msg:0
        Rx Msg:1 messages
        Rx Speed:0.00 MB/sec (since 1st msg)
        Received:0.00 MB]

2009-1-28 14:47:12 org.apache.catalina.ha.session.DeltaManager waitForSendAllSes
sions
??: Manager [localhost#]; session state send at 09-1-28 ??2:47 received in 2
65 ms.
2009-1-28 14:47:12 org.apache.catalina.ha.session.JvmRouteBinderValve start
??: JvmRouteBinderValve started
2009-1-28 14:47:12 org.apache.coyote.http11.Http11Protocol start
??: Starting Coyote HTTP/1.1 on http-8000
2009-1-28 14:47:12 org.apache.jk.common.ChannelSocket init
??: JK: ajp13 listening on /0.0.0.0:8009
2009-1-28 14:47:12 org.apache.jk.server.JkMain start
??: Jk running ID=0 time=0/31  config=null
2009-1-28 14:47:12 org.apache.catalina.startup.Catalina start
??: Server startup in 2818 ms


红色部分显示了当前配置使用的 className="org.apache.catalina.ha.session.DeltaManager"是一个全局session复制使用的控制器,把session在集群的所有tomcat进行复制。而在大型系统中使用全局session复制是不合适的,应该使用 className="org.apache.catalina.ha.session.BackupManager"实现tomcat配对 session复制,如tomcat1和tomcat2配对时,在workers.properties中配置实现tomcat2作为tomcat1的后备,当tomcat1当掉时候tomcat2接受发给tomcat1的请求和session内容。详情请参考tomcat的官方 workers.properties configuration说明。
   测试负载均衡与session复制时候,可以在界面上和tomcat后台看到。

  其中一个tomcat后台打印片段,另外一个没有拷贝下来,省略。

2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.ha.session.JvmRouteBinderValve start  
  1. ??: JvmRouteBinderValve started  
  2. 2009 - 1 - 28   14 : 47 : 12  org.apache.coyote.http11.Http11Protocol start  
  3. ??: Starting Coyote HTTP/1.1  on http- 8000   
  4. 2009 - 1 - 28   14 : 47 : 12  org.apache.jk.common.ChannelSocket init  
  5. ??: JK: ajp13 listening on /0.0 . 0.0 : 8009   
  6. 2009 - 1 - 28   14 : 47 : 12  org.apache.jk.server.JkMain start  
  7. ??: Jk running ID=0  time= 0 / 31   config= null   
  8. 2009 - 1 - 28   14 : 47 : 12  org.apache.catalina.startup.Catalina start  
  9. ??: Server startup in 2818  ms  
  10. 55  =  55   
  11. gg = gg  
  12. 44  =  44   
  13. 55  =  55   
  14. gg = gg  
  15. 11  =  11   
  16. 77  =  77  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值