環境:
操做系統均爲:CentOS 5.1
Apache2.X服務器一臺:IP地址192.168.232.4;安裝路徑/usr/local/apache;
Tomcat6服務器一臺:IP地址192.168.232.5;安裝路徑/usr/local/tomcat;
Tomcat6服務器一臺:IP地址192.168.232.6;安裝路徑/usr/local/tomcat;
配置:
Apache安裝:
#./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 --enable-rewrite
註釋:激活tomcat集羣須要的 enable-proxy,enable-proxy-http,enable-proxy-connect,enable-proxy-ajp和enable-proxy-balancer,其中proxy-ajp和proxy-balancer必須依賴proxy,若是是自定義的編譯除了以上幾個必須的模塊外,mod_status也要編譯進去,切記。enable-proxy-ftp能夠不編譯。
#make;make install
製做Apache啓動項:
#cp support/apachectl /etc/rc.d/init.d/httpd
#vi /etc/rc.d/init.d/httpd
添加如下內容:(包括#號)
# Startup script for the Apache Web Server
# chkconfig: 2345 85 15
# description: Apache is a World Wide Web server .It is used to server
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache/log/httpd.pid
# config: /usr/local/apache/conf/httpd.conf
增長服務項
#chkconfig --add httpd
#chmod 755 /etc/rc.d/init.d/httpd
#chkconfig --level 345 httpd on
JDK安裝:
#chmod a+x jdk-6u4-linux-i586-rpm.bin
#./jdk-6u4-linux-i586-rpm.bin
JAVA環境變量設置:
#vi /etc/profile
在文件最後添加如下內容:
JAVA_HOME=/usr/java/jdk1.6.0_04
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CLASSPATH PATH CATALINA_HOME
執行以下命令使環境變量生效:
source /etc/profile
測試配置是否成功:
java –version
Tomcat安裝:
#wget [url]http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.tar.gz[/url]
#tar zxvf apache-tomcat-6.0.16.tar.gz
#mv apache-tomcat-6.0.16 /usr/local/tomcat
Tomcat隨機啓動:
#vi /etc/rc.local
添加如下內容:
/usr/local/tomcat/bin/startup.sh
tomcat6配置文件server.xml:
把
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
改爲
<!-- You should set jvmRoute to support load-balancing via AJP ie :
-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcatX">
<!--
<Engine name="Catalina" defaultHost="localhost">
-->
說明:
第一臺tomcat就把jvmRoute="tomcat1"
第二臺tomcat就把jvmRoute="tomcat2"
把
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
去掉註釋變爲
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
***羣集詳細配置***
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<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="4000"
autoBind="100"
selectorTimeout="5000"
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"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<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>
配置應用的web.xml:
在每一個webapps應用中,修改配置文件web.xml文件 添加元素<distributable/>
在web.xml文件中<web-app>元素下增長如下內容:
<!--此應用將與羣集服務器複製Session-->
<distributable/>
具體修改以下:
修改前:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="[url]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
version="2.5">
</web-app>
修改後:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="[url]http://java.sun.com/xml/ns/javaee[/url]"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
version="2.5">
<!--此應用將與羣集服務器複製Session-->
<distributable/>
</web-app>
配置apache的ajp負載均衡功能:
確保將如下Module的註釋去掉
LoadModule proxy_module modules/mod_proxy.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_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule status_module modules/mod_status.so
增長如下內容:
# Proxypass Config
Include conf/extra/httpd-modproxy.conf
創建文件httpd-modproxy.conf輸入內容:
<Location /server-status>
SetHandler server-status
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>
ProxyRequests Off
ProxyPass / balancer://tomcatcluster stickysession=jsessionid nofailover=On
<Proxy balancer://tomcatcluster>
BalancerMember [url]http://192.168.232.5:8080[/url] loadfactor=1
BalancerMember [url]http://192.168.232.6:8080[/url] loadfactor=2
</Proxy>
註釋:
ProxyRequests Off 表示啓用反向代理,必須開啓;
ProxyPass爲代理轉發的Url,即將全部訪問/的請求轉發到羣集balancer://tomcatcluster,這裏爲/即將全部訪問/的請求轉發到羣集balancer://tomcatcluster的/test目錄;
BalancerMember爲羣集的成員,即羣集服務器1或2,負載均衡服務器會根據均衡規則來將請求轉發給BalancerMember;
調試負載均衡集羣系統:
訪問apache服務器的web服務:[url]http://192.168.232.4/balancer-manager[/url]
若是顯示負載均衡有關信息則說明成功了,接着能夠訪問[url]http://192.168.232.4/[/url]即訪問到了tomcat的應用
***必須先啓動Tomcat服務再啓動Apache服務!***
參考文檔:
[url]http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html[/url]
[url]http://tomcat.apache.org/tomcat-6.0-doc/balancer-howto.html[/url]
[url]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_proxy.html[/url]
[url]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/mod/mod_proxy_balancer.html[/url]php