Apache 负载均衡+Tomcat集群

花了一下午的时间,研究了一下Apache 负载均衡+Tomcat集群的问题,至于原理高手解释下,我是照葫芦画瓢!现在与大家分享一下

 

一、本机环境

1.Windows 7 64位操作系统

2.java版本 "1.6.0_20"

3.Apache安装包httpd-2.2.21-win32-x86-no_ssl.msi,默认安装

4.tomcat压缩包(apache-tomcat-6.0.24.zip)

5.在一台机器上进行的

 

二、配置

1.前提是Apache安装正确,启动正常

 

2.解压缩多个tomcat,例如d:\tomcat_01,d:\tomcat_02

 

3.配置httpd.conf文件

 

找到如下信息,并进行修改

 

#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
#Include conf/extra/httpd-vhosts.conf

 

 去掉注释

 

在httpd.conf最后加上如下代码

 

ProxyRequests Off
<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>

 

上面的BalancerMember成员是配置tomcat集群的 

 

4.配置httpd-vhosts.conf文件

 

找到如下代码

 

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/docs/dummy-host.sm.com"
   ServerName dummy-host.sm.com
    ServerAlias www.dummy-host.sm.com
    ErrorLog "logs/dummy-host.sm.com-error.log"
    CustomLog "logs/dummy-host.sm.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/docs/dummy-host2.sm.com"
    ServerName dummy-host2.sm.com
    ErrorLog "logs/dummy-host2.sm.com-error.log"
    CustomLog "logs/dummy-host2.sm.com-access.log" common
</VirtualHost>

 

 找到这些代码删除或者注释掉

 

在httpd-vhosts.conf文件最后加入如下代码

 

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName localhost
    ServerAlias localhost
    ProxyPass / balancer://cluster/
    ProxyPassReverse / balancer://cluster/
    ErrorLog "logs/dummy-host2.sm.com-error.log"
    CustomLog "logs/dummy-host2.sm.com-access.log" common
</VirtualHost>

域名与路径根据自己需要配置

 

 

5.配置tomcat,修改conf/server.xml文件

说明,我只用了两个tomcat集群

修改第二个tomcat(两个tomcat任意设置一个)

 

 

<Server port="8005" shutdown="SHUTDOWN">

将8005改为9005,防止与第一个冲突

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />

将8080改为9080,将8443改为9443
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
将8009改为9009,将8443,改为9443

 

两个tomcat均需要做如下改动

 

找到
<Engine name="Catalina" defaultHost="localhost"  >
改成
<Engine name="Catalina" defaultHost="localhost"  jvmRoute="jvm2">

并在<Engine name="."..></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"/>
		<Channel className="org.apache.catalina.tribes.group.GroupChannel">
			<Membership className="org.apache.catalina.tribes.membership.McastService"
				address="228.0.0.4" port="45565" frequency="500" dropTime="3000"/>
			<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
				address="auto" port="5002" 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;"/>
		<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>

 

第一个tomcat配置为<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

改动Membership port 的值与另一个设置为不同
改动Receiver port的值与另一个设置为不同

为了防止冲突

 

6.所有配置已经完毕,我们现在可以进行测试

 

7.为了让效果更加明显,我们改动\tomcat_01\webapps\ROOT\index.html,在<body></dody>中加上<h1>tomcat1</h1>

   改动\tomcat_02\webapps\ROOT\index.html,在<body></dody>中加上<h1>tomcat2</h1>

 

8.在浏览器中输入http://localhost,看看效果吧!刷新一下会有意外惊喜

 

9.欢迎讨论....

 

 

你可能感兴趣的:(apache,tomcat,负载均衡,集群)