Apache+Tomcat负载均衡和集群的尝试

1 各软件版本
Apache 2.2.4 因为jd_mod 用的是2.2.4的,所以这里也用这个版本,以免有问题
Tomcat 5.5.26
jd-mod: mod_jk-1.2.26-httpd-2.2.4.so

2 配置Apache

引用:

LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so
JkWorkersFile D:/Apache2.2/conf/workers.properties
JkLogFile d:/Apache2.2/logs/mod_jk.log
JkLogLevel info
#JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /servlet/* myloadbalancer
JkMount /*.jsp myloadbalancer



3 workers.properties

引用:
worker.list=myloadbalancer

worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.lbfactor=1

worker.tomcat2.type=ajp13
worker.tomcat2.host=localhost
worker.tomcat2.port=9009
worker.tomcat2.lbfactor=1

worker.tomcat3.type=ajp13
worker.tomcat3.host=localhost
worker.tomcat3.port=9019
worker.tomcat3.lbfactor=1

worker.myloadbalancer.type=lb
worker.myloadbalancer.balance_workers=tomcat1,tomcat2,tomcat3
worker.myloadbalancer.sticky_session=false



使用了3个tomcat,其侦听端口分别为 8009,9009和9019
worker.myloadbalancer.sticky_session=false 声明即使是同一个SESSIONID,也不用同一个Worker进行处理,这样更有利于负载均衡。
如果为true,则如果SESSIONID相同,则会自动使用上一次对应的worker

4 apache-tomcat-5.5.26-1\conf\server.xml

  1. <Server port="8005" shutdown="SHUTDOWN">  
  2. ....   
  3. <Connector port="8080" maxHttpHeaderSize="8192"  
  4. ....   
  5. <Connector port="8009"  
  6. ........   
  7. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  
  8. ....   
  9. <!-- 这里删除注释的起始标志 -->  
  10. <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  11.   ........   
  12.             <Receiver    
  13.                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  
  14.                 tcpListenAddress="192.168.0.1"  
  15.                 tcpListenPort="4001"  
  16.                 tcpSelectorTimeout="100"  
  17.                 tcpThreadCount="6"/>  
  18. ........   
  19.         </Cluster>  
  20. <!-- 这里删除注释的终止标志 -->  
<Server port="8005" shutdown="SHUTDOWN">
....
<Connector port="8080" maxHttpHeaderSize="8192"
....
<Connector port="8009"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
  ........
            <Receiver 
                className="org.apache.catalina.cluster.tcp.ReplicationListener"
                tcpListenAddress="192.168.0.1"
                tcpListenPort="4001"
                tcpSelectorTimeout="100"
                tcpThreadCount="6"/>
........
        </Cluster>
<!-- 这里删除注释的终止标志 -->



5 apache-tomcat-5.5.26-2\conf\server.xml

  1. <Server port="9005" shutdown="SHUTDOWN">  
  2. ....   
  3. <Connector port="9080" maxHttpHeaderSize="8192"  
  4. ....   
  5. <Connector port="9009"  
  6. ........   
  7. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">  
  8. ....   
  9. <!-- 这里删除注释的起始标志 -->  
  10. <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  11.   ........   
  12.             <Receiver    
  13.                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  
  14.                 tcpListenAddress="192.168.0.1"  
  15.                 tcpListenPort="4002"  
  16.                 tcpSelectorTimeout="100"  
  17.                 tcpThreadCount="6"/>  
  18. ........   
  19.         </Cluster>  
  20. <!-- 这里删除注释的终止标志 -->  
<Server port="9005" shutdown="SHUTDOWN">
....
<Connector port="9080" maxHttpHeaderSize="8192"
....
<Connector port="9009"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
  ........
            <Receiver 
                className="org.apache.catalina.cluster.tcp.ReplicationListener"
                tcpListenAddress="192.168.0.1"
                tcpListenPort="4002"
                tcpSelectorTimeout="100"
                tcpThreadCount="6"/>
........
        </Cluster>
<!-- 这里删除注释的终止标志 -->



6 apache-tomcat-5.5.26-3\conf\server.xml

  1. <Server port="9006" shutdown="SHUTDOWN">  
  2. ....   
  3. <Connector port="9088" maxHttpHeaderSize="8192"  
  4. ....   
  5. <Connector port="9019"  
  6. ........   
  7. <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">  
  8. ....   
  9. <!-- 这里删除注释的起始标志 -->  
  10. <Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"  
  11.   ........   
  12.             <Receiver    
  13.                 className="org.apache.catalina.cluster.tcp.ReplicationListener"  
  14.                 tcpListenAddress="192.168.0.1"  
  15.                 tcpListenPort="4003"  
  16.                 tcpSelectorTimeout="100"  
  17.                 tcpThreadCount="6"/>  
  18. ........   
  19.         </Cluster>  
  20. <!-- 这里删除注释的终止标志 -->  
<Server port="9006" shutdown="SHUTDOWN">
....
<Connector port="9088" maxHttpHeaderSize="8192"
....
<Connector port="9019"
........
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
....
<!-- 这里删除注释的起始标志 -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
  ........
            <Receiver 
                className="org.apache.catalina.cluster.tcp.ReplicationListener"
                tcpListenAddress="192.168.0.1"
                tcpListenPort="4003"
                tcpSelectorTimeout="100"
                tcpThreadCount="6"/>
........
        </Cluster>
<!-- 这里删除注释的终止标志 -->



7 测试用的程序
使用了网上普遍采用的一段代码
webapps/test/index.jsp

  1.     
  2. <%@ page c %>   
  3. <%@ page import="java.util.*" %>   
  4. <html><head><title>Cluster App Test</title></head>   
  5. <body>   
  6. Server Info:   
  7. <%   
  8. System.out.println("================");   
  9. out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>   
  10. <%   
  11.   out.println("<br> ID " + session.getId()+"<br>");   
  12.   // 如果有新的 Session 属性设置   
  13.   String dataName = request.getParameter("dataName");   
  14.   if (dataName != null && dataName.length() > 0) {   
  15.      String dataValue = request.getParameter("dataValue");   
  16.      session.setAttribute(dataName, dataValue);   
  17.   }   
  18.   out.print("<b>Session 列表</b>");   
  19.   Enumeration e = session.getAttributeNames();   
  20.   while (e.hasMoreElements()) {   
  21.      String name = (String)e.nextElement();   
  22.      String value = session.getAttribute(name).toString();   
  23.      out.println( name + " = " + value+"<br>");   
  24.          System.out.println( name + " = " + value);   
  25.    }   
  26. %>   
  27.   <form action="index.jsp" method="POST">   
  28.     名称:<input type=text size=20 name="dataName">   
  29.      <br>   
  30.     值:<input type=text size=20 name="dataValue">   
  31.      <br>   
  32.     <input type=submit>   
  33.    </form>   
  34. </body>   
  35. </html>  
 
<%@ page c %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
System.out.println("================");
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="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>


webapps/test/WEB-INF/web.xml

  1.     
  2. <?xml version="1.0" encoding="ISO-8859-1"?>  
  3. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
  6.     version="2.4">  
  7.   <display-name>Tomcat Simple Load Balancer Example App</display-name>  
  8.   <description>  
  9.     Tomcat Simple Load Balancer Example App   
  10.   </description>  
  11.   <distributable/>  
  12. </web-app>  
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>Tomcat Simple Load Balancer Example App</display-name>
  <description>
    Tomcat Simple Load Balancer Example App
  </description>
  <distributable/>
</web-app>


其中的 distributable 是关键

8 遇到的问题
1)启动时产生 Error receiving mcast package
经测试发现,我用的ADSL,如果我联网,则会报这个异常,如果断网则不会产生。看来那个
tcpListenAddress="192.168.0.1"
需要使用外网的IP地址才可以。
2)启动异常,一般是端口被占用
请仔细看各个server.xml配置文件,各个端口不能相同

 

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