Apache + Tomcat 负载均衡与集群配置
-------------------------------------
1. 准备
下载 Apache Http Server
http://labs.renren.com/apache-mirror//httpd/binaries/win32/httpd-2.2.19-win32-x86-no_ssl.msi
下载 Tomcat
http://apache.etoak.com/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32-windows-x86.zip
2. 安装 Apache Http Server
安装完成后, 配置 conf/httpd.conf
2.1 配置HTTP端口:
Listen 80
2.2 配置负载均衡, 去掉注释符#:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
2.3 配置虚拟主机, 去掉注释符#:
Include conf/extra/httpd-vhosts.conf
2.3 配置首页
<IfModule dir_module>
DirectoryIndex index.htm index.html index.jsp
</IfModule>
2.4 配置负载均衡, 在最后面加上:
ProxyRequests Off
<proxy balancer://cluster>
BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat1
BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat2
ProxySet lbmethod=byrequests timeout=15
</proxy>
2.5 配置 conf/extra/httpd-vhosts.conf
2.5.1 注释所有虚拟主机节点:
2.5.2 添加虚拟主机, 注意格式:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName localhost
ServerAlias localhost
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=Off
ProxyPassReverse / balancer://cluster/
</VirtualHost>
3. 安装 Tomcat
解压后, 将目录取名 tomcat1, 再复制一份取名tomcat2.
3.1 配置tomcat1:
配置 tomcat1/conf/server.xml
3.1.1 配置负载均衡路由, 增加 jvmRoute="tomcat1" :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
3.1.2 配置集群, 去掉集群节点的注释符
3.1.3 配置端口, 去掉注释符:
<!-- See proxy documentation for more information about using this. -->
<Connector port="8082"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" acceptCount="100" connectionTimeout="20000"
proxyPort="80" disableUploadTimeout="true" />
3.2 配置tomcat2:
配置 tomcat2/conf/server.xml
3.2.1 配置负载均衡路由, 增加 jvmRoute="tomcat2" :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
3.2.2 配置集群, 去掉集群节点的注释符, 修改Cluster|Receiver|tcpListenPort为4002.
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4002"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
3.2.3 修改相关端口,避免与tomcat1 端口冲突:
<Server port="9005" shutdown="SHUTDOWN">
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="9080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="9009"
enableLookups="false" redirectPort="9443" protocol="AJP/1.3" />
<!-- Define a Proxied HTTP/1.1 Connector on port 8082 -->
<!-- See proxy documentation for more information about using this. -->
<Connector port="9082"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" acceptCount="100" connectionTimeout="20000"
proxyPort="80" disableUploadTimeout="true" />
4. ADSL 用户, 断开ADSL连接
5. 部署测试应用 到 tomcat1, tomcat2 webapps目录:
5.1 test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.Enumeration, java.util.Date" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>负载均衡</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);
}
out.print("<b>Session 列表</b><br />");
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="test.jsp" method="POST">
名称:<input type=text size=20 name="dataName">
<br />
值:<input type=text size=20 name="dataValue">
<br />
<input type=submit>
</form>
<%
System.out.println(new Date());
%>
</body>
</html>
5.2 配置应用 WEB-INF/web.xml, 增加<distributable />
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>cluster</display-name>
<distributable />
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
6. 测试
3.1 启动tomcat1, tomcat2, 然后启动apache.
6.2 打开测试页面:
http://localhost/cluster/test.jsp
6.3
输入aa aa 提交
输入bb bb 提交
输入cc cc 提交
输入dd dd 提交
输入ee ee 提交
输入ff ff 提交
结果:
可以看到, 请求分别轮流由tomcat1, tomcat2处理, 负载均衡成功,同时session 进行了复制,说明集群配置成功.