日期:2014-05-16  浏览次数:21255 次

Apache2.2与tomcat7 集群配置
以前就对集群比较感兴趣,今天在网上收集资料,自己也尝试配置了一个demo。
准备环境:
  • 安装Apache是http服务器,我们利用其对Tomcat进行负载均衡,我用的是apache2.2。
  • 下载Tocmat7,我这里用的是tomcat-7.0.21

集群主要有两种方式:
1.mod_proxy_blancer方式

我这里的demo是两个tomcat7的集群。
首先将apache安装目录下的conf目录下的httpd.conf文件的下图中的注释去掉

然后再这个文件最后添加下列文字
#虚拟机配置,负载均衡配置
<VirtualHost *:8000>
	ServerAdmin zengzhaoshuai@qq.com
	ServerName localhost
	ServerAlias localhost
	#小心,有些地方要有空格,要不然会出错哈哈。
	ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
	ProxyPassReverse / balancer://cluster/
	#ErrorLog "logs/error.log"
	#CustomLog "logs/access.log" common
</VirtualHost> 

#The ProxyRequests directive should usually be set off when using ProxyPass.
ProxyRequests Off
<proxy balancer://cluster>
	BalancerMember ajp://localhost:8009 loadfactor=1 route=tomcat7_a  smax=5 max=20 ttl=120 retry=300 timeout=15
	BalancerMember ajp://localhost:9009 loadfactor=1 route=tomcat7_b  smax=5 max=20 ttl=120 retry=300 timeout=15
	# status=+H为配置热备,当所有机器都over时,才会请求该机器
	#BalancerMember http://192.168.1.218:8009 status=+H
	ProxySet lbmethod=bytraffic
</proxy>

在这里特别注意的是httpd.conf文件编码一定是utf-8 ,要不然apache启动不了。
<VirtualHost *:8000>,虚拟的主页,访问时:http://localhost:8000/项目名
ajp://localhost:8009 ,一个tomcat的,8009为ajp端口
ajp://localhost:9009,另外一个一个tomcat的,9009为ajp端口
route=tomcat7_a,其中的一个tomcat
route=tomcat7_a,另外一个tomcat

最后修改tomcat的confmuluxia的server.xml配置文件件
tomcat7_a的server.xml配置文件如下:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost"   jvmRoute="tomcat7_a">
	 <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.ReplicationT