Apache,Tomcat负载均衡和集群

Apache,Tomcat负载均衡和集群

软件下载:
apache:httpd-2.2.19-win32-x86-no_ssl.msi
http://httpd.apache.org/download.cgi#apache22

 

tomcat:apache-tomcat-6.0.24
http://tomcat.apache.org/

 

jk:mod_jk-1.2.31-httpd-2.2.3.so
http://tomcat.apache.org/download-connectors.cgi

 

负载均衡

1.解压两份Tomcat, 路径分别为 apache-tomcat-1,apache-tomcat-2

 

2.在apache安装目录下conf目录中配置http.conf
在文件最后加上下面一句
include "D:\Program Files\Apache Software Foundation\Apache2.2\conf\mod_jk.conf"

 

3.在apache安装目录下conf目录中创建mod_jk.conf文件,内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so

 

#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties

 

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /*.jsp controller

 

4.在apache安装目录下conf目录中创建workers.properties文件,内容如下:
worker.list = controller,tomcat1,tomcat2 

 

#server 列表#========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配置,默认8009
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=1

 

5.修改tomcat配置文件server.xml
打开tomcat2/conf/server.xml文件
<Server port="9005" shutdown="SHUTDOWN">  改为9005
<Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />     改为9080
 <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />改为9009

 

6.编写test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.lang.String"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println("=basePath is:="+basePath+"=end=");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>test</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <script language="javascript">

 </script>
  </head> 
  <body>
  <%
   System.out.println("===========================");
  %>
  </body>
</html>

 

7.在apache-tomcat-1\webapps目录下创建test目录并将test.jsp放入
在apache-tomcat-2\webapps目录下创建test目录并将test.jsp放入

 

8.将mod_jk-1.2.31-httpd-2.2.3.so复制到

D:\Program Files\Apache Software Foundation\Apache2.2\modules目录下

 

9.启动apache,tomcat1,tomcat2,进行测试
http://localhost/test/test.jsp
控制台上可以看到===========================


配置集群
       其中任何一个tomcat的添加的session,是要同步复制到其它tomcat, 集群内的tomcat都有相同的session

 

1.修改tomcat1, tomcat2的server.xml
 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>  启用
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> 启用
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"> 启用

 

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

 

2.编写index.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 list</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">
    key:<input type=text size=20 name="dataName">
     <br>
    value:<input type=text size=20 name="dataValue">
     <br>
    <input type=submit>
   </form>
</body>

 

3.将index.jsp文件放入\webapps\test目录下(两个tomcat)


4.新建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/>

 

5.重启apache,tomcat1,tomcat2
http://localhost/test/index.jsp

 

 

Done!

 

各软件、配置文件及test目录见附件

你可能感兴趣的:(负载均衡)