基于apache的tomcat负载均衡和集群配置

Tomcat
基于apache的tomcat负载均衡和集群配置

参考文章
http://www.iteye.com/topic/675914
http://www.iteye.com/topic/1017961


1】 下载安装

       httpd-2.2.15-win32-x86-no_ssl.msi    网页服务器

       32-bit Windows zip                            tomcat

       mod_jk-1.2.30-httpd-2.2.3.so             Apache/IIS 用来连接后台Tomcat的模块,支持集群和负载均衡

       JK 分为两个版本 1,x 和 2.x ,其中 2.x 并不是最新的版本,它是 JK 的另外一个分支,后不知何因没有继续开发,因此2.x 版本已经废弃

       安装httpd-2.2.15-win32-x86-no_ssl.msi 到指定目录,我安装的是C:\Program Files\Apache2.2,以后这个目录将用Apache_Home代替

       安装tomcat,我是将两个tomcat进行集群,所以安装两个tomcat,路径分别为:F:\tomcat\tomcat1,F:\tomcat\tomcat2,以后这两个目录将用tomcat1_Home,tomcat2_Home代替

2】 配置Apache_Home\conf\httpd.conf

      在httpd.conf文件的最后一行写入include "C:\Program Files\Apache2.2\conf\mod_jk.conf"

3】 配置mod_jk.conf文件

      在Apache_Home\conf下建立mod_jk.conf文件

      配置内容:

      #加载mod_jk Module
         LoadModule jk_module modules/mod_jk-1.2.30-httpd-2.2.3.so

         将下载的 mod_jk-1.2.30-httpd-2.2.3.so 文件放入到Apache_Home\modules下
      # 配置 mod_jk
      #加载集群中的workers
         JkWorkersFile conf/workers.properties
      #加载workers的请求处理分配文件
         JkMountFile conf/uriworkermap.properties
      #指定jk的日志输出文件
         JkLogFile logs/mod_jk.log
      #指定日志级别
         JkLogLevel warn
     #指定哪些请求交给tomcat处理,"controller"为在 workers.propertise里指定的负载分配控制器名
         JkMount /* controller

4】  配置workers.propertise文件

       在Apache_Home\conf下建立workers.propertise文件

       配置内容:

       #server 列表
         worker.list=controller,tomcat1,tomcat2
      #========tomcat1========
      #ajp13 端口号,在tomcat下server.xml配置,默认8009
        worker.tomcat1.port=8009
      #tomcat的主机地址,如不为本机,请填写ip地址
        worker.tomcat1.host=localhost
        worker.tomcat1.type=ajp13
     #server的加权比重,值越高,分得的请求越多
        worker.tomcat1.lbfactor=1
     #========tomcat2========
     #ajp13 端口号,在tomcat下server.xml配置,默认8009
       worker.tomcat2.port=9009
     #tomcat的主机地址,如不为本机,请填写ip地址
      worker.tomcat2.host=localhost
      worker.tomcat2.type=ajp13
    #server的加权比重,值越高,分得的请求越多
       worker.tomcat2.lbfactor=1
    #========controller,负载均衡控制器========
    #server名为controller,用于负载均衡
      worker.controller.type=lb
   #重试次数
      worker.retries=3
   #指定分担请求的tomcat
     worker.controller.balanced_workers=tomcat1,tomcat2
   #粘性Session(默认是打开的) 当该属性值=True(或1)时,代表Session是粘性的,即同一Session在集群中的同一个节点上处理,Session不跨越节点。在集群环境中,一般将该值设置为False
     worker.controller.sticky_session=false
  #设置用于负载均衡的server的session可否共享 有不少文章说设置为1是可以的,也有设置为0才可以的
     worker.controller.sticky_session=1
  #worker.controller.sticky_session_force=1
  #worker.status.type=status

5】 配置uriworkermap.properties文件

      在Apache_Home\conf下建立uriworkermap.properties文件

      配置内容:

     #所有请求都由controller这个server处理
     /*=controller
     #所有包含jkstatus请求的都由status这个 server处理
     #/jkstatus=status
     #这里的"!”是“非”的意思。
     !/*.gif=controller
     !/*.jpg=controller
     !/*.png=controller
     !/*.css=controller
     !/*.js=controller
     !/*.htm=controller
     !/*.html=controller

6】修改tomcat1_Home\conf\server.xml配置

      Starting Coyote HTTP/1.1 on http-8081 默认端口为8080,修改为8081


     <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

      JK: ajp13 listening on /0.0.0.0:8009 修改端口必须与workers.propertise文件内worker.tomcat1.port=8009一致

     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

     需要添加的内容:


    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">   <!--tomcat1将与tomcat2黏贴session 在这里指定黏贴对象-->

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="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"/>

            <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"/>
          </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>   

      上面部分如果需要详细的说明可以看webapps\docs\cluster-howto.html

7】修改tomcat2_Home\conf\server.xml配置
      port由"8005"改为"9005"
      <Server port="9005" shutdown="SHUTDOWN">

      Starting Coyote HTTP/1.1 on http-8082 默认端口为8080,修改为8082


     <Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

      JK: ajp13 listening on /0.0.0.0:9009 修改端口必须与workers.propertise文件内worker.tomcat2.port=9009一致

     <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

     需要添加的内容:


    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">   <!--tomcat2将与tomcat1黏贴session 在这里指定黏贴对象-->

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="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="4001"  
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <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"/>
          </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>   

      上面部分如果需要详细的说明可以看webapps\docs\cluster-howto.html

8】session黏贴(共享)方面,如果session中存放的为javabean,javabean必须实现Serializable接口,如果没有实现会报错误 Exception thrown: class java.lang.IllegalArgumentException




9】补充:



      看到jameswxx的回复发现确实有欠妥的地方,现在进行改正

      在工程的web.xml文件内加入 <distributable/>    标签

      标题名不够准确,将原名称“tomcat集群配置流程”更正为“基于apache的tomcat负载均衡和集群配置”















声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。

你可能感兴趣的:(java,tomcat)