知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

apache2.2.21 + mod_jk1.2.32 + tomcat6.0.33 配置负载均衡

发布时间:2014-09-05 16:43:10作者:知识屋

 

系统环境:Windows XP SP3 ,jdk1.6.0_13

软件版本:httpd-2.2.21-win32-x86-openssl-0.9.8r.msi  apache-tomcat-6.0.33   tomcat-connectors-1.2.32-windows-i386-

 

 

httpd-2.2.x

 

 

我是在一台机器上作的测试,因为我完成了apache的负载均衡以及tomcat6的集群和session的复制,

需要修改到的地方可能比较多,先将配置列出如下:

 

 

1. 修改apache的httpd.conf:

 

 

首先要load所需要的so文件,把下面几行前面的注释去掉.

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

 

 

加入load连接器的配置

LoadModule jk_module modules/mod_jk.so

JkWorkersFile "conf/workers.properties"

JkMount /*.jsp controller

 

 

去掉虚拟主机的注释

Include conf/extra/httpd-vhosts.conf

 

 

结尾加上负载均衡配置

 

 

<Location /server-status>

SetHandler server-status

Order Deny,Allow

Deny from all

Allow from all

</Location>

<Location /balancer-manager>

SetHandler balancer-manager

Order Deny,Allow

Deny from all

Allow from all

</Location>

ProxyRequests Off

ProxyPass /test balancer://tomcatcluster/ stickysession=jsessionid nofailover=On

 

 

 

 

<Proxy balancer://tomcatcluster>

BalancerMember ajp://127.0.0.1:8009 loadfactor=1  route=tomcat1

BalancerMember ajp://127.0.0.1:9009 loadfactor=1  route=tomcat2

</Proxy>

 

 

 

2. apache的httpd-vhosts.conf文件中加入:

 

 

 

 

<VirtualHost *:80>

         ServerAdmin webmaster@163.com

         ServerName localhost

         ServerAlias localhost

         ProxyPass / balancer://tomcatcluster/ stickysession=jsessionid nofailover=On

         ProxyPassReverse / balancer://tomcatcluster/

</VirtualHost>

 

 

3.创建workers.properties文件

 

 

#========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=localhost #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 

 

 

4.修改tomcat的server.xml:

 

除了把两个tomcat冲突的端口都改掉,还需要加入下的集群设置

 

 

 

 

  <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> --另一个为tomcat2       

 

 

     

         <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">

       <!-- 

        <Manager className="org.apache.catalina.ha.session.BackupManager"

                 expireSessionsOnShutdown="false"

                 notifyListenersOnReplication="true"

                 mapSendOptions="6"/>

         -->

        <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="5001"

                      selectorTimeout="100"

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

            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>

        </Channel>

        <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

               filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>

       <!--

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

 

 

 

 

5.webapps应用下web.xml配置

 

 

 

 

Webapps目录下的应用程序的web.xm中必须添加

<distributable/> 此应用将与群集服务器复制Session

 

 

配置完成,先启动所有tomcat服务器,最后启动apache2.2服务器.

 

 

6.下面jsp可以作为测试程序部署在tomcat1,tomcat2

<%@ 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>");

  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="index.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>

 

 

成功配置的结果是tomcat1,tomcat2的后台打印session内容相同,通过apache访问请求被分发

 

摘自我行我诉

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜