Apache-based tomcat load balancing and cluster configuration

Apache-based tomcat load balancing and cluster configuration

1] Download and install

       httpd-2.2.15-win32-x86-no_ssl.msi    web server

       32-bit Windows zip                            tomcat

       mod_jk-1.2.30-httpd-2.2.3.so             Apache/IIS module used to connect to the background Tomcat, supports clustering and load balancing

       JK is divided into two versions 1,x and 2.x, of which 2.x is not the latest version, it is another branch of JK, and for some reason it did not continue to develop, so the 2.x version has been abandoned

       Install httpd-2.2.15-win32-x86-no_ssl.msi to the specified directory, I installed C:\Program Files\Apache2.2, this directory will be replaced by Apache_Home in the future

       To install tomcat, I am clustering two tomcats, so I install two tomcats, the paths are: F:\tomcat\tomcat1, F:\tomcat\tomcat2, these two directories will be replaced by tomcat1_Home, tomcat2_Home in the future

2] Configure Apache_Home\conf\httpd.conf

Write include "C:\Program Files\Apache2.2\conf\mod_jk.conf"       in the last line of the httpd.conf file

3] Configure the mod_jk.conf file

      Create mod_jk.conf file under Apache_Home\conf

      Configuration content:

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

         Put the downloaded mod_jk-1.2.30-httpd-2.2.3.so file under Apache_Home\modules
      # Configure mod_jk
      # Load the workers in the cluster
         JkWorkersFile conf/workers.properties
      # Load the request processing allocation file for workers
         JkMountFile conf/ uriworkermap.properties #Specify
      the log output file of jk
         JkLogFile logs/mod_jk.log #Specify the
      log level
         JkLogLevel warn #Specify
     which requests are to be processed by tomcat, "controller" is the load distribution controller name specified in workers.propertise
         JkMount /* controller

4] Configure the workers.propertise file

       Create the workers.propertise file under Apache_Home\conf

       Configuration content:

       #server list
         worker.list=controller,tomcat1,tomcat2
      #========tomcat1========
      #ajp13 Port number, configured in server.xml under tomcat, default 8009
        worker.tomcat1.port =8009
      #The host address of the tomcat, if it is not a local machine, please fill in the ip address
        worker.tomcat1.host=localhost
        worker.tomcat1.type=ajp13 #The
     weighted proportion of the server, the higher the value, the more requests are allocated to the
        worker. tomcat1.lbfactor=1
     #========tomcat2========
     #ajp13 port number, configured in server.xml under tomcat, default 8009
       worker.tomcat2.port=9009
     #host address of tomcat , if it is not a local machine, please fill in the ip address
      worker.tomcat2.host=localhost
      worker.tomcat2.type=ajp13 #server
    weighted proportion, the higher the value, the more requests will be
       awarded worker.tomcat2.lbfactor=1
    #========controller, load balancing controller ========
    #server named controller, used for load balancing
      worker.controller.type=lb
   #retries
      worker.retries=3
   # Specify tomcat
     worker.controller.balanced_workers=tomcat1, tomcat2
   to share the request Processed on the node, Session does not span nodes. In a cluster environment, this value is generally set to False
     worker.controller.sticky_session=false #Set
  whether the session of the server used for load balancing can be shared. There are many articles that say that it is possible to set it to 1, and it is also possible to set it to 0.
     worker.controller.sticky_session=1
  #worker.controller.sticky_session_force=1
  #worker.status.type=status

5] Configure the uriworkermap.properties file

      Create the uriworkermap.properties file under Apache_Home\conf

      Configuration content:

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

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326881697&siteId=291194637