作者:罗代均 ldj_work#126.com,转载请保持完整性
0, 环境说明
Apache :apache_2.0.55 1 个
Tomcat: apache-tomcat-5.5.17 (zip版) 2个
mod_jk:: mod_jk-apache-2.0.55.so 1个
第一部分:负载均衡
负载均衡,就是apache将客户请求均衡的分给tomcat1,tomcat2....去处理
1.安装apche,tomcat
http://httpd.apache.org/ 下载Apache 2.0.55
http://tomcat.apache.org/download-55.cgi 下载tomcat5.5 zip版本(解压即可,绿色版)
http://apache.justdn.org/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.15/ 下载mod_jk,注意和 apache版本匹配
按照jdk,我的路径为:E:\ide\apache\Apache2
解压两份Tomcat, 路径分别为 E:\ide\tomcat1,E:\ide\tomcat2
![](http://hiphotos.baidu.com/luodaijun/pic/item/9d61f003d5034eec09fa935b.jpg)
下载mod_jk
![](http://hiphotos.baidu.com/luodaijun/pic/item/52855e82373a3ea20df4d224.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/46899c50d44e3b628435245b.jpg)
2.修改Apache配置文件http.conf
在apache安装目录下conf目录中找到http.conf
在文件最后加上下面一句话就可以了
include "E:\ide\apache\Apache2\conf\mod_jk.conf"
![](http://hiphotos.baidu.com/luodaijun/pic/item/d1c28cd49b8d6901a08bb720.jpg)
LoadModule jk_module modules/mod_jk-apache-2.0.55.so
JkWorkersFile conf/workers.properties
worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
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
![](http://hiphotos.baidu.com/luodaijun/pic/item/a3d133a8ed4063b1ca130c36.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/8c3cae7727b25d1bb151b937.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/56b04b085ba7f3950b7b8230.jpg)
System.out.println("===========================");
%>
![](http://hiphotos.baidu.com/luodaijun/pic/item/acf2fcf2c8b77d13b17ec53d.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/aa4ab5fb0f3582224f4aeab2.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/89b84c3627ecc1dda3cc2b92.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/cc4b858b603d47d3fd1f10b2.jpg)
![](http://hiphotos.baidu.com/luodaijun/pic/item/52dfdac4bc506ecb38db4959.jpg)
<%@ 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);
}
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>
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
![](http://hiphotos.baidu.com/luodaijun/pic/item/79a9317af99aa5ed2e73b385.jpg)