Tomcat 和 Apache集群开始
环境说明
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
下载mod_jk
2.修改Apache配置文件http.conf
在apache安装目录下conf目录中找到http.conf
在文件最后加上下面一句话就可以了
include "E:\ide\apache\Apache2\conf\mod_jk.conf"
ITPUB
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
System.out.println("===========================");
%>
<%@ 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>
<!-- You should set jvmRoute to support load-balancing via JK/JK2 ie : -->
<Engine name="Standalone" defaultHost="localhost" debug="0" jvmRoute="tomcat1">