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
- <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>
- <!-- 这里删除注释的终止标志 -->
- <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>
- <!-- 这里删除注释的终止标志 -->
<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
- <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>
- <!-- 这里删除注释的终止标志 -->
- <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>
- <!-- 这里删除注释的终止标志 -->
<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
- <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>
- <!-- 这里删除注释的终止标志 -->
- <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>
- <!-- 这里删除注释的终止标志 -->
<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
- <%@ 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>
- <%@ 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>
<%@ 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
- <?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>
- <?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>
<?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配置文件,各个端口不能相同