配置实例:单台机器模拟,配置二个Tomcat服务的集群.. Apache/2.2.6 Tomcat/5.0.28 j2sdk/1.4.2.08
1.Apache&Tomcat的负载均衡
Tomcat配置
由于二个Tomcat位于同一机器,刚各个Tomcat服务的端口须注意。
Tomcat1 /home/huaying/tomcat1 Server_port:8005 Conn_port:7518 Conn_AJP13_port:8009 Conn_redirectPort:8443
Tomcat2 /home/huaying/tomcat2 Server_port:8006 Conn_port:7519 Conn_AJP13_port:8010 Conn_redirectPort:8444
二Tomcat启动/关闭脚本稍作修改 #vi startup.sh shutdown.sh 修改如下行:
TOMCAT_HOME=/home/huaying/tomcat2&tomcat1 此处是二Tomcat启动/关闭的差异
Tomcat1/Tomcat2 项目目录(网站根目录)webapps下建test一软链接,皆指向Apache Vhost 80的网站根目
录下的/usr/local/apache2/htdocs/test
http://IP:7518/test/session.jsp 与 http://IP:7519/test/session.jsp 指向同一文件
即:http://IP/test/session.jsp
htdocs下有一目录test目录下有个jsp测试页面session.jsp.
#cat session.jsp
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body><center><%=new java.util.Date()%>
<%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);
}%> </center></body></html>
此脚本能显示当前的jsp session ID, 确定当前页面是由哪个Tomcat来解析。。
除了做上述修改后,仍需修改二Tomcat服务的server.xml
#vi server.xml (tomcat1&tomcat2)
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> 增加jvmRoute属性
#vi /usr/local/apache2/htdocs/WEB-INF/web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
Apache配置
Apache整合部分请参照上篇博文:(主要为装载mod_jk模块,增加mod_jk.conf)
#vi conf/workers.properties
worker.list=controller,tomcat1,tomcat2 //worker列表,请求都转给controller
worker.tomcat1.port=8009 //ajp13端口
worker.tomcat1.host=localhost //主机地址,可以是IP
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1 //权重,决定分配请求的多少
worker.tomcat2.port=8010
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 //指定分担的worker
worker.controller.sticky_session=false
另每个worker还可有以下属性(测试时为效果明显,所以没加以下选项)
worker.tomcat1.cachesize=1
worker.tomcat1.cache_timeout=600
worker.tomcat1.socket_keepalive=1
worker.tomcat1.socket_timeout=300
注:此种均衡方式,当某台提供服务的tomcat宕掉后,Apache不会知道,仍会分配请求过来。
2.Apache&Tomcat集群
基于Apache&Tomcat的负载均衡配置,所增加的配置如下:
#vi server.xml
在Host元素下增加以下内容:
<Cluster
... ...
<!--每个群集服务器都需要有相同的Membership配置-->
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4" 约定的广播地址,所有集群服务器相同
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<!--tcpListenAddress:本机IP地址服务器将此地址广播给其他群集服务器-->
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="60.28.201.170"
tcpListenPort="4001" 集群通讯端口/如二集群服务器为一台机器需注意
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
... ...
</Cluster>
二台集群服务器各参数对比如下:
Server Port 8005 8006
Connector 7518 7519
Connector AJP13 8009 8010
Cluster mcastAddr 228.0.0.4 228.0.0.4
Cluster mcastPort 45564 45564
tcpListenAddress 本地IP 本地IP
Cluster tcpListenPort 4001 4002
#vi workers.properties
worker.list=controller,tomcat1,tomcat2
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.redirect=tomcat2
worker.tomcat2.port=8010
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.redirect=tomcat1
worker.tomcat2.activation=disabled
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
此集群默认把请求都转给tomcat1,当tomcat1宕机,请求自动转给tomcat2.相应mod_jk.log会记录日志
应用中一般结合Apache&Tomcat的负载均衡和集群方案,四组Tomcat服务器,二组均衡,另二组分别作集群.
3.Apache&Tomcat集群另一种配置
Tomcat端的配置同上,Apache端配置如下:
#vi httpd.conf
将以下Module的注释去掉
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
并增加以下元素
ProxyRequests Off
ProxyPass /test balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://10.10.13.145:9080
BalancerMember http://10.10.13.145:10080 smax=1 loadfactor=20
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all
</Location>
其中
ProxyPass为代理转发的Url,即将所有访问/test的请求转发到群集balancer://mycluster
BalancerMember为群集的成员,即群集服务器A或B,负载均衡服务器会根据均衡规则来将请求转发给
BalancerMember。