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

基于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\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

     <Connector port="8080" 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>      

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

7】修改tomcat2_Home\conf\server.xml配置

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

     <Connector port="8899" 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>   
  

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

 

8】编写一个测试jsp

建立一个目录test,里面新建一个test.jsp,内容为
<%
   System.out.println("===========================");
%>
把test放到tomcat1,tomcat2的webapps下
9】启动apache,tomcat1,tomcat2,进行测试(测试负载均衡)
通过 http://localhost/test/test.jsp 访问,查看tomcat1的窗口,可以看到打印了一行"=========="
再刷新一次,tomcat2也打印了一条,再刷新,可以看到请求会被tomcat1,tomcat2轮流处理,实现了负载均衡
基于apache的tomcat负载均衡和集群配置

 

10修改测试项目test(测试session同步)

修改test.jsp,内容如下
  <%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
  out.println("<br> ID " + session.getId()+"<br>");
  // 如果有新的 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+"<br>");
         System.out.println( name + " = " + value);
   }
%>
  <form action="test.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>
然后在test 新建WEB-INF目录,WEB-INF下新建web.xml,内容如下
<web-app xmlns=" http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
       <display-name>TomcatDemo</display-name>
       <distributable/>
</web-app>
注意:在你的应用的web.xml加入  <distributable/> 即可
ok,讲test复制到tomcat1,tomcat2的webapps下,重启apache,tomcat1,tomcat2,
新建一个 名称为 xiaoluo  ,值为 cdut 的session,提交查询,新开一个ie窗口,再提交查询,可以看到,两个tomcat 是负载均衡,并且session同步的

(引用http://www.iteye.com/topic/675914

http://hi.baidu.com/luodaijun/blog/item/5bbe4cfb5ffef864034f56a1.html

略有改动)

 

 

#

Host节点的域名配置方法:修改C:\WINDOWS\system32\drivers\etc 下的 hosts文件增加一行

127.0.0.1       saas

#

 

你可能感兴趣的:(apache,tomcat,xml,jsp,Web)