为记录apache和tomcat负载均衡过程
测试环境:
xp
Jdk 1.6
Apache 2.2.25 http://httpd.apache.org/download.cgi
Tomcat tomcat6.0.37 http://tomcat.apache.org/download-60.cgi
一、apache httpserver 安装过程不再介绍
tomcat免安装版,如果是在同一主机上配置两个tomcat需要改相应的端口号防止冲突(本例是在一台主机上两个tomcat)
二、配置开始,修改httpd.conf文件
1、在apache的安装路径中找到httpd.conf文件
LoadModuleproxy_module modules/mod_proxy.so
LoadModuleproxy_ajp_module modules/mod_proxy_ajp.so
LoadModuleproxy_balancer_module modules/mod_proxy_balancer.so
LoadModuleproxy_connect_module modules/mod_proxy_connect.so
LoadModuleproxy_ftp_module modules/mod_proxy_ftp.so
LoadModuleproxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_modulemodules/mod_proxy_scgi.so
2、找到以下的信息,将Includeconf/extra/httpd-vhosts.conf 这行前面的#号去掉
# Virtual hosts
# Includeconf/extra/httpd-vhosts.conf
三、找到Apache2.2\conf\extra目录下的httpd-vhosts.conf,安装以下的方式进行修改
1.将这个文件下的
ServerAdmin webmaster@dummy-host. com.cn
ServerName dummy-host.com.cn
ServerAlias www.dummy-host.com.cn
ProxyRequests Off
ProxyPreserveHost on
ProxyPass / balancer://tomcat-cluster/stickysession=JSESSIONID|jsessionid nofailover=Off
ProxyPassReverse /balancer://tomcat-cluster/
ErrorLog"logs/dummy-host2.sm.com-error.log"
CustomLog"logs/dummy-host2.sm.com-access.log" common
BalancerMember ajp://127.0.0.1:18003loadfactor=1 route=jvm1
BalancerMember ajp://127.0.0.1:19003loadfactor=1 route=jvm2
ProxySet lbmethod=bybusyness
解释:
BalancerMemberajp://127.0.0.1:18003 loadfactor=1 route=jvm1
BalancerMember ajp://127.0.0.1:19003loadfactor=1 route=jvm2这两行对应的是配置的tomcat相关内容,18003和19003这两个端口对应tomcat中的server.xml中的
四、tomcat配置
1、tomcat1配置
2、tomcat2配置
五、编写测试页面
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster App Test
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
out.println("
ID " + session.getId()+"
");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.println("Session 列表
");
System.out.println("============================");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"
");
System.out.println( name + " = " + value);
}
%>