+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
秀脱linux笔记之tomcat5_apache2.0_mod_jk2负载均衡加集群实战篇
Q Q : 6541657
E_MAIL: xiutuo@gmail.com
环境:
--------------------------------------------
一台apache2.0服务器,三台tomcat服务器:
apache2.0服务器
1.ip:192.168.1.20
2.只装apache软件:apache2.0.55.tar.bz2
安装路径:/usr/local/apache2
tomcat服务器:均配置相同的应用。
1.集群名:balancer://tomcatcluster
2.三台集群服务器ip:
IP_1:192.168.1.30
IP_2:192.168.1.31
IP_3:192.168.1.32
3.测试应用程序test文件夹放在tomcat6的webapps目录下
操作系统均为:centos 4.5_x86
jre:1.4:系统自带 (或jdk1.5.6,自行安装)
tomcat5:编译好的二进制软件包apache-tomcat-5.5.15.tar.gz
tomcat5 安装路径:/usr/local/tomcat5
oralce的jdbc:class12.jar
软件包存放的路径:/home/xiutuo/software/
使用普通用户:xiutuo来启动tomcat5
tomcat5开机自动启动脚本:/etc/init.d/tomcat
下载路径:
http://java.sun.com/javase/downloads/index.jsp
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
一:前言:
1.apache对tomcat集群支持有俩种方式:
a.通过apache2.1之后版本后内置的proxy_ajp。
b.对于apache2.1之版本则通过tomcat的jk2.0.4的mod_jk2.so:
(该版本已经停止开发)
将解压缩后的目录中的modules目录中的mod_jk2.so
文件复制到apache的安装目录下的modules目录中。
********************
俩种方式比较:
proxy_ajp配置较简单,主要表现在proxy_ajp目前只支持配置到目录,
还不支持对文件名称的pattern模式匹配
(即还不能定义到只对jsp文件起作用)。
而jk2则可配置性强,但已经停止开发
********************
官方对ajp和jk2说明:
JK2 has been put in maintainer mode and no further development will take place.
The reason for shutting down JK2 development was the lack of developers interest.
Other reason was lack of users interest in adopting JK2,
caused by configuration complexity when compared to JK.
The latest official JK2 release is 2.0.4.
JK2 will have it's successor within core Apache2.1/2.2 distribution.
We have developed new proxy_ajp that is an addition to the mod_proxy
and uses Tomcat's AJP protocol stack. It is developped in httpd-2.1
and integrated in it. We have also developed a new proxy_balancer
module for load balancing http and ajp protocol stacks.
JK will be fully supported for all other web servers.
The next JK release is planned for the end of November.
Lots of code from JK2 has been ported to JK
2.tomcat集群方式:
a.DeltaManager
--现在采用的该方式:内部机器集群少采用
b.BackupManager
********************
两种集群方式官方说明:
using the DeltaManager to replicate session deltas.
By all-to-all we mean that the session gets replicated to
all the other nodes in the cluster. This works great for smaller cluster
but we don't recommend it for larger clusters(a lot of tomcat nodes).
Also when using the delta manager it will replicate to all nodes,
even nodes that don't have the application deployed.
To get around this problem, you'll want to use the BackupManager.
This manager only replicates the session data to one backup node,
and only to nodes that have the application deployed.Downside of
the BackupManager: not quite as battle tested as the delta manager.
********************
3.负载均衡(load-balance)定义:
在服务器端短时间内获得大量的请求,单一服务器无法在一个较短的时间内响应这些请求,
此时服务器需要一个机制,请求按照多个服务器不同的负载能力,把这些请求合理的分配。
4.集群(cluster):
在多个服务器之间共享用户信息,资源等。
tomcat5_apache2.0负载均衡加集群:高可用性至高体现。
现在的带集群功能相关软件oracle10g,mysql5,tomcat等。
二.软件安装过程
1.jdk的安装
a.在三台要安装tomcat的服务器上先安装jdk,由于使用系统自带的jdk1.4所以这里无需安装
b.配置jdk的安装路径,在环境变量path中加入jdk的bin路径,新建环境变量JAVA_HOME指向jdk的安装路径
# vi /etc/profile 内容如下,如果是jdk1.5请自行修改jdk的路径
JAVA_HOME=/usr/java/jdk1.4.2
export JAVA_HOME
CLASSPATH=/usr/java/jdk1.4.2/lib:/usr/java/jdk1.4.2/jre/lib
export CLASSPATH
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH
JRE=/usr/java/jdk1.4.2/jre
export JRE
# source /etc/profile
2.tomcat的安装
a.在三台要安装tomcat的服务器上分别安装tomcat,调试三个tomcat到能够正常启动
tomcat的安装请参照xiutuo的blog
b.tomcat的默认WEB服务端口是8080,默认的模式是单独服务,
三个tomcat的WEB服务端口修改为7777/8888/9999。
修改位置为tomcat的安装目录下的conf/server.xml
把
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
修改为
<Connector port="7777" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
依次修改三台服务器tomcat的监听端口(7777/8888/9999)
c. 配置每个集群负载均衡
分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
把
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host). -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->
<!-- Define the top level container in our container hierarchy -->
<Engine name="Catalina" defaultHost="localhost">
修改为
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host). -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
<!-- Define the top level container in our container hierarchy
<Engine name="Catalina" defaultHost="localhost">
-->
将其中的jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"和jvmRoute="tomcat2"和jvmRoute="tomcat3"
d.配置负载均衡条件下的tomcat集群
分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
把
<!--
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
-->
修改为
<!-- modify by whh -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
mcastPort="45564"
mcastFrequency="500"
mcastDropTime="3000"/>
<Receiver
className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto"
tcpListenPort="4001"
tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender
className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"
ackTimeout="5000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
<!-- modify by whh -->
将集群配置选项的注释放开即可,如上。
e.配置测试应用jsp页面
在三个tomcat的安装目录中的webapps建立相同的应用,这里的应用目录名为test,
在三个应用目录中建立相同 WEB-INF目录和页面index.jsp,index.jsp的页面内容如下
<%@ page contentType="text/html; charset=GBK" %>
<%@ 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.print("<b>Session 列表</b>");
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>
f.应用程序的负载和集群配置
对于要进行负载和集群的的tomcat目录下的webapps中的应用中的WEB-INF中的web.xml文件要添加如下一句配置
<distributable/>
配置前
<?xml version="1.0" encoding="UTF-8"?>
<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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>TomcatDemo</display-name>
</web-app>
配置后
<?xml version="1.0" encoding="UTF-8"?>
<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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
g.分别测试每个tomcat的启动是否正常
http://192.168.1.30:7777
http://192.168.1.31:8888
http://192.168.1.32:9999
h.启动tomcat
3.apache安装
a.apache服务器安装:
这里主要介绍apache2的源码安装
# cd /home/xiutuo/software/
# tar -zvxf apache2.0.55.tar.gz
# cd apache2.0.55
# mkdir -p /usr/local/apache2
# ./configure \
--prefix=/usr/local/apache2 \
--enable-modules=so \
--enable-mods-shared=all
注释:这里测试用,编译了所有可用模块
b.编译mod_jk2模块
# cd /home/xiutuo/software/
# tar -zxvf jakarta-tomcat-connectors-jk2-2.0.4-src.tar.gz
# cd jakarta-tomcat-connectors-jk2-2.0.4-src
# ./configure --with-apxs=/usr/local/apache2/apxs
# make
# cp ./apache-2.0/mod_jk2.so /usr/local/apache2/modules
# vi /usr/local/apache2/conf/http.conf
在LoadModule模块配置信息的最后加上一句
LoadModule jk2_module modules/mod_jk2.so
安装后测试apache能否正常启动,调试到能够正常启动http://192.168.1.20
c.配置负载均衡
在apache的安装目录中的conf目录下创建文件workers2.propertie,
写入文件内容如下
# fine the communication channel
[channel.socket:192.168.1.30:8009]
info=Ajp13 forwarding over socket
#配置第一个服务器
tomcatId=tomcat1 #要和tomcat的配置文件server.xml中的jvmRoute="tomcat1"名称一致
debug=0
lb_factor=1 #负载平衡因子,数字越大请求被分配的几率越高
# Define the communication channel
[channel.socket:192.168.1.31:8009]
info=Ajp13 forwarding over socket
tomcatId=tomcat2
debug=0
lb_factor=1
# Define the communication channel
[channel.socket:192.168.1.32:8009]
info=Ajp13 forwarding over socket
tomcatId=tomcat3
debug=0
lb_factor=1
[status:]
info=Status worker, displays runtime information.
[uri:/jkstatus.jsp]
info=Display status information and checks the config file for changes.
group=status:
[uri:/*]
info=Map the whole webapp
debug=0
三.整体调试。
调试步骤,先启动tomcat,在启动apache,记住这个顺序
测试负载均衡先测试apache,访问http://192.168.1.20/jkstatus.jsp
能正常访问,并查询其中的内容,有三个tomcat的相关配置信息和负载说明,
访问http://192.168.1.20/test/index.jsp看能够运行,
能运行,则已建立负载均衡。