作者:罗代均 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
![](https://i-blog.csdnimg.cn/blog_migrate/b8ed0bc1f589ae6073ac362318d83d57.jpeg)
下载mod_jk
![](https://i-blog.csdnimg.cn/blog_migrate/03ab2b4f1cc6d254f6c68a308c138782.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b365eccafdb5bb542ad206be5fb81a77.jpeg)
2.修改Apache配置文件http.conf
在apache安装目录下conf目录中找到http.conf
在文件最后加上下面一句话就可以了
include "E:\ide\apache\Apache2\conf\mod_jk.conf"
![](https://i-blog.csdnimg.cn/blog_migrate/b942a9019b389df3f06c9bfd25702581.jpeg)
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
![](https://i-blog.csdnimg.cn/blog_migrate/f6d297432442a2a44f18f851f93a74e7.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/e2a50c1746fef89d64f786ed0914f4c6.jpeg)
System.out.println("===========================");
%>
![](https://i-blog.csdnimg.cn/blog_migrate/ba2e8dcbfda1acc40e65335c4bc7b46a.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/be25eb949648c4256619bfbb5bd83515.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/0f914689e354ec308fc048d378bcd630.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/688d9ec73550aae165968f3107b5ef72.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/449558ef9367f7e48925e9c156fb4205.jpeg)
<%@ 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>
![](https://i-blog.csdnimg.cn/blog_migrate/66c8262f58ff94bce3ade26924745395.jpeg)