LINUX 下配置Apache+Tomcat负载均衡(一机多实例)
(1)解压安装
1)安装JDK
#./jdk-6u10-rc-bin-b28-linux-i586-21_jul_2008-rpm.bin
运行后会显示一大篇协议,按空格跳过。看到最后出现[yes]时输入yes回车确认。
添加JDK的环境变量
用文本编辑器打开/etc/profile,在文件的最后添加以下内容:
JAVA_HOME=/usr/java/jdk1.6.0_10
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export JAVA_HOME CLASSPATH PATH
保存退出,然后注销一次或运行:source /etc/profile命令使系统环境变量生效。
确定环境变量生效后可以终端中运行:java -version命令测试是否安装成功。
java version "1.6.0_10-rc"
Java(TM) SE Runtime Environment (build 1.6.0_10-rc-b28)
Java HotSpot(TM) Client VM (build 11.0-b15, mixed mode, sharing)
若出现以上文字那么JDK已经OK了。
2)安装apache
tar -zxvf httpd-2.2.16.tar.gz
程序解压后得到名为:httpd-2.2.16的文件夹。继续运行以下命令:
cd httpd-2.2.16
./configure --prefix=/usr/local/apache --enable-modules=so --enable-mods-shared-all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer
make
make install
3)配置apache文件
修改apache的配置文件httpd.conf
vi /usr/local/apache/conf/httpd.conf命令进行编辑。
<IfModuledir_module>
DirectoryIndex index.html index.jsp
</IfModule>
找到#Include conf/extra/httpd-vhosts.conf,将前面的注释符#去掉。
在文件的最后添加以下内容并保存:
ProxyRequests Off
<proxy balancer://RHEL5-1>
BalancerMember ajp://localhost:8009 loadfactor=2 route=jvm1
BalancerMember ajp://localhost:8010loadfactor=2 route=jvm2
BalancerMember ajp://localhost:8011loadfactor=2 route=jvm3
BalancerMember ajp://localhost:8012loadfactor=2 route=jvm3
</proxy>
修改apache的配置文件httpd-vhosts.conf
该文件所在路径为:/usr/local/apache/conf/extra/httpd-vhosts.conf
将文件中以下内容注释掉:
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot "/usr/local/apache/docs/dummy-host.example.com"
# ServerName dummy-host.example.com
# ServerAlias [url]www.dummy-host.example.com[/url]
# ErrorLog "logs/dummy-host.example.com-error_log"
# CustomLog "logs/dummy-host.example.com-access_log" common
#</VirtualHost>
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host2.example.com
# DocumentRoot "/usr/local/apache/docs/dummy-host2.example.com"
# ServerName dummy-host2.example.com
# ErrorLog "logs/dummy-host2.example.com-error_log"
# CustomLog "logs/dummy-host2.example.com-access_log" common
#</VirtualHost>
在文件末尾添加以下内容并保存:
<VirtualHost *:80>
ServerAdmin cntangjun@test.com
ServerName localhost
ServerAlias
ProxyPass / balancer://RHEL5-1/ stickysession=jsessionidnofailover=On
ProxyPassReverse / balancer://RHEL5-1/
</VirtualHost>
修改apache的配置文件httpd-info.conf
<Location /server-status>
SetHandler server-status
Order deny,allow
# Deny from all
Allow from all
</Location>
#
# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#
ExtendedStatus On
#
# Allow remote server configuration reports, with the URL of
# [url]http://servername/server-info[/url] (requires that mod_info.c be loaded).
# Change the ".example.com" to match your domain to enable.
#
<Location /server-info>
SetHandler server-info
Order deny,allow
# Deny from all
Allow from all
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
# Deny from all
Allow from all
</Location>
(2)配置Tomcat
先在/usr/local目录中建一个名为tomcat的文件夹,然后在终端中运行:
tar -zxvf apache-tomcat-6.0.20.tar.gz
程序解压后得到名为:apache-tomcat-6.0.20的文件夹,将它复制四份到/usr/local/tomcat目录并重命名为tomcat6-1、tomcat6-2、tomcat6-3、tomcat6-4、
进入tomcat6-1目录,修改conf目录下的server.xml文件(以下两个项目不变)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
找到文件中<Engine name="Catalina" defaultHost="localhost">这一行,将它改为:
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
说明:这里的jvmRoute的值必须对应于httpd.conf文件中最末添加的route的值。
接着在刚才修改的那一行下添加以下内容并保存:
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6" />
- <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[U1] " port="4000" autoBind="100" 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;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />
<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.JvmRouteSessionIDBinderListener" />
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />
</Cluster>
将保存后的server.xml文件复制一分到tomcat6-2/3/4的对应文件夹下替换掉原文件。并打开修改下面几处:
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm2">
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4001" autoBind="100" selectorTimeout="100" maxThreads="6"/>
分别对应修改端口,不要冲突就可以了,另外ajp端口一定要对应上httpd.conf中配置的端口
(3)启动服务
先启动各tomcat:
/usr/local/tomcat/tomcat6-1/catalina.sh run
/usr/local/tomcat/tomcat6-2/catalina.sh run
/usr/local/tomcat/tomcat6-3/catalina.sh run
/usr/local/tomcat/tomcat6-4/catalina.sh run
再启动apache:
/usr/local/apache/bin/httpd -k start
重启apache
/usr/local/apache/bin/httpd -k restart
打开浏览器,通过http://127.0.0.1来访问一下,应该就会出现大家熟悉的猫猫了。
然后再分别用
来访问一下,
它们显示的内容应该是和http://127.0.0.1一样的,这样就说明apache和tomcat整合成功!
(4)给tomcat增加管理用户(有时间的话分别给tomcat下conf文件下user.xml中添加管理员用户。)
<tomcat-users>
<role rolename=”manager”/>
<role rolename=”admin”/>
<user username= “admin”password=”admin″ roles=”manager,admin”/>
</tomcat-users>
(5)最后完工,重启tomcat,apahce测试平衡负载,新建测试目录test和test.jsp页面
<%
Runtime lRuntime = Runtime.getRuntime();
out.println("*** BEGIN MEMORY STATISTICS ***<br/>");
out.println("Free Memory: "+lRuntime.freeMemory()/1024/1024+"M<br/>");
out.println("Max Memory: "+lRuntime.maxMemory()/1024/1024+"M<br/>");
out.println("Total Memory: "+lRuntime.totalMemory()/1024/1024+"M<br/>");
out.println("Available Processors : "+lRuntime.availableProcessors()+"<br/>");
out.println("*** END MEMORY STATISTICS ***");
%>
<br>
<%= request.getSession().getId() %>
分别放入到tomcat的ROOT目录中测试
测试session复制
修改测试项目test修改test.jsp,内容如下
<meta http-equiv="content-type" content="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.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="test.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>
然后在test 新建WEB-INF目录,WEB-INF下新建web.xml,内容如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
注意:在你的应用的web.xml加入<distributable/>即可
把test复制到tomcat6-1/2/3/4的webapps下,重启apache,tomcat,
输入网址 http://localhost/test/test.jsp 新建一个名称为:tangjun,值为中国的session,提交查询,新开一个ie窗口,再提交查询,如图,可以看到,两个tomcat 是负载均衡,并且session同步的,也可以不断的刷新,可以看到轮流访问四个页面,但是值仍然是一样的。
关于tomcat热备份
需要修改apache的httpd.conf文件
ProxyPass / balancer://hotcluster/
<Proxy balancer://hotcluster>
BalancerMember ajp://localhost:8009
BalancerMember ajp://localhost:8010 status=+H
ProxySet lbmethod=bytraffic
</Proxy>
然后重启apache