Tomcat是J2EE开发当中使用最多的应用服务器,本章就来介绍一下使用Apache2应用服务器加上Tomcat6一起实现应用集群与负载均衡。这里我们使用的Tomcat为7.0.26;Apache版本为2.2,如果您机器上没有,请到www.apache.org上下载,Tomcat7.0.26有两个,我们这里称之为tomcat1和tomcat2,下文中如果没特指tomcat1还是tomcat2,那么tomcat1和tomcat2都要进行操作,apache2.2只有一个。所以都准备好之后,接下来我们就来看看如何进行配置。
打开tomcat下conf/server.xml文件,找到<Engine name="Catalina" defaultHost="localhost">,增加jvmRoute属性,以支持AJP负载均衡。
tomcat1改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
tomcat2改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
因为我们的两个Tomcat在是一台机器上运行,所以我们有必要修改其中一个Tomcat采用端口,以保证两个同时运行时不会产生冲突(当然,如果两个tomcat不在一台机器上运行,那么这个步骤就可以省略)。我们这里修改的是tomcat2,打开tomcat2下conf/server.xml文件,修改如下:
<Server port=
"8005"
shutdown=
"SHUTDOWN"
>
修改为
<Server port=
"9005"
shutdown=
"SHUTDOWN"
>
|
<Connector port=
"8080"
protocol=
"HTTP/1.1"
connectionTimeout=
"20000"
redirectPort=
"8443"
/>
修改为
<Connector port=
"9080"
protocol=
"HTTP/1.1"
connectionTimeout=
"20000"
redirectPort=
"9443"
/>。
|
<Connector port=
"8009"
protocol=
"AJP/1.3"
redirectPort=
"8443"
/>
修改为
<Connector port=
"9009"
protocol=
"AJP/1.3"
redirectPort=
"9443"
/>
|
端口修改完成,我们需要来配置一个集群环境中的session复制,打开tomcat下conf/server.xml文件,在<Engine></Engine>节点内加入下面代码:
<Cluster className=
"org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions=
"6"
>
<Manager className=
"org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown=
"false"
notifyListenersOnReplication=
"true"
mapSendOptions=
"6"
/>
<!--
<Manager className=
"org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown=
"false"
notifyListenersOnReplication=
"true"
/>
-->
<Channel className=
"org.apache.catalina.tribes.group.GroupChannel"
>
<Membership className=
"org.apache.catalina.tribes.membership.McastService"
address=
"228.0.0.4"
port=
"45564"
frequency=
"500"
dropTime=
"3000"
/>
<Receiver className=
"org.apache.catalina.tribes.transport.nio.NioReceiver"
address=
"auto"
port=
"5000"
selectorTimeout=
"100"
maxThreads=
"6"
/>
<Sender className=
"org.apache.catalina.tribes.transport.ReplicationTransmitter"
>
<Transport className=
"org.apache.catalina.tribes.transport.nio.PooledParallelSender"
/>
</Sender>
<Interceptor className=
"org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"
/>
<Interceptor className=
"org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"
/>
<Interceptor className=
"org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"
/>
</Channel>
<Valve className=
"org.apache.catalina.ha.tcp.ReplicationValve"
filter=
".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"
/>
<Deployer className=
"org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir=
"/tmp/war-temp/"
deployDir=
"/tmp/war-deploy/"
watchDir=
"/tmp/war-listen/"
watchEnabled=
"false"
/>
<ClusterListener className=
"org.apache.catalina.ha.session.ClusterSessionListener"
/>
</Cluster>
|
其中同台机器上的TCP端口不能有冲突,所以在上面的配置当中,对于Tomcat2需要修改节点Culster->Channel->Reciver的port属性,默认为5000,tomcat识别的范围为4000-4100,这里把tomcat2的这个port属性修改为4001。
这些配置都完成后,接下来我们就可以在这两个Tomcat当中添加一个简单应用来实际测试一下集群效果。
在tomcat/webapps下,新建一文件夹为test,test文件夹下创建WEB-INF目录,创建标准web.xml文件,文件中一定要加入<distributable/>标签以支持分布式应用。
web.xml文件内容如下:
<?xml version=
"1.0"
encoding=
"ISO-8859-1"
?> <web-app xmlns=
"<a href="http://java.sun.com/xml/ns/javaee" "="">http://java.sun.com/xml/ns/javaee"
xmlns:xsi=
"<a href="http://www.w3.org/2001/XMLSchema-instance" "="">http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee <a href="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" "="">http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version=
"3.0"
metadata-complete=
"true"
>
<display-name>Welcome to Test Tomcat7 Cluster</display-name>
<description>Welcome to Test Tomcat7 Cluster</description>
<distributable/>
</web-app>
|
在test文件夹下建立test.jsp文件,文件内容如下:
<%@ page contentType=
"text/html; charset=UTF-8"
%>
<%@ 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>"
);
// 如果有新的 Session 属性设置
String dataName = request.getParameter(
"dataName"
);
if
(dataName !=
null
&& dataName.length() >
0
) {
String dataValue = request.getParameter(
"dataValue"
);
session.setAttribute(dataName, dataValue);
}
out.println(
"<b>Session 列表</b><br>"
);
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+
"<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>
|
这个简单的JSP可以把提交的值保存到Session当中,同时显示出来。
启动tomcat1,等tomcat1启动完成后,启动tomcat2。先访问tomcat1对应的test工程http://localhost:8080/test.jsp页面,输入值并提交,然后把链接转到tomcat2对应的test工程http://localhost:9080/test.jsp页面,看看session内容是否相同,如果相同则说明session复制成功,也就是群集成功。
接下来我们在这两个Tomcat前端添加一个Apache Http Server,所有访问请求通过这个Http Server进行转发,根据tomcat1或2的忙碌情况决定转发给tomcat1或者是tomcat2去处理用户请求。
打开并修改apache安装目录下conf/httpd.conf文件,找到下面这些内容:
#LoadModule negotiation_module modules/mod_negotiation.so
#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
|
去掉这些#,也就是启用这些被注释掉的功能,并且在文件的未尾处添加下面这些内容:
ProxyPass / balancer:
//cluster/ stickysession=JSESSIONID
ProxyPassReverse / balancer:
//cluster/
<proxy balancer:
//cluster>
BalancerMember ajp:
//127.0.0.1:8009 loadfactor=1 route=jvm1
BalancerMember ajp:
//127.0.0.1:9009 loadfactor=1 route=jvm2
</proxy>
|
重新启动apache,以apache作为负载均衡器的tomcat集群建立完成,所有请求都可以通过请求apache http server,通过这个http server转发到具体的tomcat服务器。
<Cluster className=
"org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions=
"8"
>
|
Tomcat集群的主元素,在Cluster元素里面配置了集群的详细信息。目前Tomcat仅提供了org.apache.catalina.ha.tcp.SimpleTcpCluste作为唯一的实现类。下面是Cluster详细属性:
属性 |
描述 |
||
---|---|---|---|
className |
Cluste的实现类,当前org.apache.catalina.ha.tcp.SimpleTcpCluste作为唯一的实现类。 |
||
channelSendOptions |
Session发送方式,默认值为:8。当消息通过SimpleTcpCluster发送时,用来决定如何发送信息。
如果使用异步(ASYNCHRONOUS)加应答(USE_ACK)方式来发送消息,那么值应该是10(8+2)或者0x000B。 |
<Manager className=
"org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown=
"false"
notifyListenersOnReplication=
"true"
/>
|
Manager用来在Tomcat节点之前复制Session,当前有两个实现类,分别为:org.apache.catalina.ha.session.DeltaManagert和org.apache.catalina.ha.session.BackupManager。DeltaManager调用SimpleTcpCluster.send方法来发送信息,复制并发送Session到集群下所有的节点,不管这个节点有没有部署当前程序。
BackupManager通过自己直接调用channel来发送信息,复制并发送Session到集群下部署了当前程序的节点。
DaltaManager的优点是经过实践确认和证明的,非常可靠,而BackupManager在可靠性上不如DaltaManager。DaltaManager缺点是要求集群节点必须部署了同样的程序,节点必须是同种类的。
下面是Manager的属性描述:
属性 |
描述 |
---|---|
className |
Manager的实现类。 |
notifyListenersOnReplication |
如果设置为true,当session属性被复制和移动的时候,session listener被通知 |
expireSessionsOnShutdown |
当一个web程序被结束时,tomcat分发销毁命令到每个Session,并通知所有session listener执行。当集群下某个节点被停止时,如果想销毁所有节点下的的Session,设置为true,默认为false 。 |
BackupManager属性:
属性 |
描述 |
---|---|
mapSendOptions |
BackupManager通过一个可复制的map来实现session接受和发送,通过设置mapSendOptions来设定这个map以何种方式来发送信息,默认为:6(asynchronous)。 |
<Channel className=
"org.apache.catalina.tribes.group.GroupChannel"
>
|
Channel是Apache Tribes的主组件,channel管理一组子组件,并和它们一起组成了tomcat实例间的通讯框架。在tomcat集群中,DeltaManager通过SimpleTcpCluster调用channel来实现信息传递,而BackupManager自己调用channel以及子组件这些组件来实现信息传递。ReplicatedContext也会调用channel传递context属性。
下面是channel子组件
*MemberShip
MemberShip组件自动检索发现集群里的新节点或已经停止工作的节点,并发出相应的通知。默认使用组播(Multicast)实现。
*Sender
Sender组件管理从一个节点发送到另外一个节点的出站连接和数据信息,允许信息并行发送。默认使用TCP Client Sockets。
*Receiver
Receiver组件负责监听接收其他节点传送过来的数据。默认使用non-blocking TCP Server sockets。
*Interceptor
Channel通过Interceptor堆栈进行消息传递,在这里可以自定义消息的发送和接收方式,甚至MemberShip的处理方式。
<Valve className=
"org.apache.catalina.ha.tcp.ReplicationValve"
filter=
" .*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.css;.*\.txt; "
/>
<Valve className=
"org.apache.catalina.ha.session.JvmRouteBinderValve"
/>
|
Cluster下的Value同其他tomcat value一样,value在调用Http Request 链中起着拦截器的作用,用来决定什么情况下数据需要被复制。
org.apache.catalina.ha.tcp.ReplicationValve,ReplicationValue在Http Request结尾判断当前数据是否需要被复制。它的具体属性描述如下表所示:
属性 |
描述 |
---|---|
className |
org.apache.catalina.ha.tcp.ReplicationValve,ReplicationValue |
filter |
Filter内容为url或者文件结尾,当访问链接配置filter时,不论实际session有没有改变,集群会认为session没有任何变化,从而不会复制和发送改变的session属性。Filter写法如下:filter=" .\.gif;.\.js;.\.jpg;.\.png;.\.css;.\.txt; "。filter使用正则表达式,每个url或者后缀以逗号分开。 |
使集群支持farmed deployment。这个功能还很弱,在变化和更改中。
<ClusterListener className=
"org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"
/>
<ClusterListener className=
"org.apache.catalina.ha.session.ClusterSessionListener"
/></Cluster>
|
Clusterlistener用来追踪信息发送和接收。JvmRouteSessionIDBinderListener用来监听session id的变化。这个listener仅当使用mod_jk并且属性有jvmRoute时才起作用。当session id 改变后,JvmRouteBinderValve将广播这个信息并被此listener捕获。ClusterSessionListener用来监听集群组件接收信息,当使用DeltaManager的时候,信息被集群接收,并通过ClusterSessionListener传递给Session Manager。
ProxyPass / balancer:
//cluster/ stickysession=JSESSIONID
ProxyPassReverse / balancer:
//cluster/
<proxy balancer:
//cluster>
BalancerMember ajp:
//127.0.0.1:8009 loadfactor=1 route=jvm1
BalancerMember ajp:
//127.0.0.1:9009 loadfactor=1 route=jvm2
</proxy>
|
通过Apache自带的mod_proxy某块来使用代理技术连接tomcat, ajp_proxy需要tomcat提供ajp服务。