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

 

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\tomcat1F:\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
端口号,在tomcatserver.xml 配置, 默认8009
        worker.tomcat1.port=8009
      #tomcat
的主机地址,如不为本机,请填写ip 地址
        worker.tomcat1.host=localhost
        worker.tomcat1.type=ajp13
     #server
的加权比重,值越高,分得的请求越多
        worker.tomcat1.lbfactor=1
     #========tomcat2========
     #ajp13
端口号,在tomcatserver.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
  #
设置用于负载均衡的serversession 可否共享 有不少文章说设置为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 配置

      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 中存放的为javabeanjavabean 必须实现Serializable 接口,如果没有实现 会报错误 Exception thrown: class java.lang.IllegalArgumentException

 

你可能感兴趣的:(apache,html,tomcat,xml,IIS)