apache + tomcat 集群

apache2.2与tomcat集成(可以多个tomcat)

 需求概况: 

有3个服务: localhost:9091, localhost:9190。 localhost:9191分别对应3个tomcat下的3个web应用。

由apache httpd作为互联网接入服务器,在80端口接收对这3个服务的请求。apache httpd再将这3个请求分别对应到不同的后端web服务器(Tomcat)处理。

一、软件准备

1. httpd-2.2.25-win32-x86-no_ssl  

2. apache-tomcat-7.0.47 

注意:由于Apache和Tomcat项目与集群相关的模块均处于持续发展和优化过程中,因此笔者不保证本文配置方法对所有Apache和Tomcat版本均适用。

 

二、软件安装

1. 安装apache2.2

把Apache安装为运行在80端口的Windows服务,安装成功后在系统服务列表中可以看到Apache2.2服务。对于已安装IIS的机器,在启动Apache服务之前必须首先停止IIS Admin服务,不然会因为端口冲突而无法启动。服务启动后在浏览器中输入http://localhost进行测试,如果能看到一个"It works!"的页面就代表Apache已经正常工作了。

1. 安装tomcatt-7.0.47

解压tomcat zip文件到两个文件夹,分别为apache-tomcat-7.0.47-2和apache-tomcat-7.0.47-1, 配置JAVA_HOME和CLASSPATH系统环境变量,分别启动t1和t2,确保tomcat可用,然后关闭tomcat。

本文仅为讲解配置过程,Apache和tomcat均工作在同一台机器上。实际部署时没有任何限制,Apache和单个tomcat可以分别部署在不同的服务器上。

三、Apache配置

Apache 2.2集成了mod_jk功能,相对于1.3版本,不需要再进行繁琐的worker.properties配置,配置过程大幅简化。

 

首先,在Apache安装目录下找到conf/httpd.conf文件,以文本编辑器打开。

去掉以下文本前的注释符(#)以便让Apache在启动时自动加载代理(proxy)模块。

1 LoadModule proxy_module modules/mod_proxy.so  
2 LoadModule proxy_ajp_module modules/mod_proxy_ajp.so  
3 LoadModule proxy_balancer_module modules/mod_proxy_balancer.so  
4 LoadModule proxy_connect_module modules/mod_proxy_connect.so  
5 LoadModule proxy_ftp_module modules/mod_proxy_ftp.so  
6 LoadModule proxy_http_module modules/mod_proxy_http.so 

 

向下拉动文档找到节点,在DirectoryIndex index.html后加上index.jsp,这一步只是为了待会配置完tomcat后能看到小猫首页,可以不做。

 

这里选择的是mod_jk-1.2.31-httpd-2.2.3.so,如果http服务器是Apache2.0.X版本,则必须选择mod_jk-1.2.31-httpd-2.0.52.so,页面下方有英文的说明,大家可以看下。

其实我也在网上看到Apache2.2已经集成Tomcat插件模块了,可以不用JK插件就可以实现Tomcat负载均衡,但也需要一些配置,而JK这个配置相比也不复杂,就先记录下来。

 

我的Apache安装在D:\Apache2.2,找到conf目录下的httpd.conf,在文件的最后一行添加

include "D:\Apache2.2\conf\mod_jk.conf"

2、新建mod_jk.conf文件,内容如下:

LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so

JkWorkersFile conf/workers.properties

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名

JkMount /*.jsp controller

3、将下载的JK插件mod_jk-1.2.31-httpd-2.2.3.so复制到Apache安装目录的modules目录下。

 

4、新建并编辑workers.properties文件,内容如下:

#server

worker.list = controller

#========tomcat1========

worker.tomcat1.port=11000

worker.tomcat1.host=localhost

worker.tomcat1.type=ajp13

worker.tomcat1.lbfactor = 1

#========tomcat2========

worker.tomcat2.port=12000

worker.tomcat2.host=localhost

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor = 1

#========tomcat3========

worker.tomcat3.port=13000

worker.tomcat3.host=192.168.0.80 

worker.tomcat3.type=ajp13

worker.tomcat3.lbfactor = 1

 

#========controller,负载均衡控制器========

worker.controller.type=lb

worker.controller.balanced_workers=tomcat1,tomcat2,tomcat3

worker.controller.sticky_session=false

worker.controller.sticky_session_force=1

#worker.controller.sticky_session=1

  

这里可以配置任意多个Tomcat,此处配置了3个Tomat服务器,2个本地,1个远程,所以为了它们都能够顺利启动起来,本地的服务器端口都是不同的,如果Tomcat不再同一机器上,没必要改端口的。

 

配置Tomcat

配置3个Tomcat服务器,将Tomcat解压后复制3份,我将每个文件夹分别命名为Tomcat1,Tomcat2和Tomcat3,修改每一份的server.xml配置,将Tomcat1中修改部分如下 :

  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- Note:  A "Server" is not itself a "Container", so you may not
 19      define subcomponents such as "Valves" at this level.
 20      Documentation at /docs/config/server.html
 21  -->
 22 <Server port="9001" shutdown="SHUTDOWN">
 23   <!-- Security listener. Documentation at /docs/config/listeners.html
 24   <Listener className="org.apache.catalina.security.SecurityListener" />
 25   -->
 26   <!--APR library loader. Documentation at /docs/apr.html -->
 27   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 28   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
 29   <Listener className="org.apache.catalina.core.JasperListener" />
 30   <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 31   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 32   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 33   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 34 
 35   <!-- Global JNDI resources
 36        Documentation at /docs/jndi-resources-howto.html
 37   -->
 38   <GlobalNamingResources>
 39     <!-- Editable user database that can also be used by
 40          UserDatabaseRealm to authenticate users
 41     -->
 42     <Resource name="UserDatabase" auth="Container"
 43               type="org.apache.catalina.UserDatabase"
 44               description="User database that can be updated and saved"
 45               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 46               pathname="conf/tomcat-users.xml" />
 47   </GlobalNamingResources>
 48 
 49   <!-- A "Service" is a collection of one or more "Connectors" that share
 50        a single "Container" Note:  A "Service" is not itself a "Container",
 51        so you may not define subcomponents such as "Valves" at this level.
 52        Documentation at /docs/config/service.html
 53    -->
 54   <Service name="Catalina">
 55 
 56     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
 57     <!--
 58     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 59         maxThreads="150" minSpareThreads="4"/>
 60     -->
 61 
 62 
 63     <!-- A "Connector" represents an endpoint by which requests are received
 64          and responses are returned. Documentation at :
 65          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
 66          Java AJP  Connector: /docs/config/ajp.html
 67          APR (HTTP/AJP) Connector: /docs/apr.html
 68          Define a non-SSL HTTP/1.1 Connector on port 8080
 69     -->
 70     <Connector port="9091" protocol="HTTP/1.1"
 71                connectionTimeout="20000"
 72                redirectPort="8443" />
 73     <!-- A "Connector" using the shared thread pool-->
 74     <!--
 75     <Connector executor="tomcatThreadPool"
 76                port="8080" protocol="HTTP/1.1"
 77                connectionTimeout="20000"
 78                redirectPort="8443" />
 79     -->
 80     <!-- Define a SSL HTTP/1.1 Connector on port 8443
 81          This connector uses the JSSE configuration, when using APR, the
 82          connector should be using the OpenSSL style configuration
 83          described in the APR documentation -->
 84     <!--
 85     <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
 86                maxThreads="150" scheme="https" secure="true"
 87                clientAuth="false" sslProtocol="TLS" />
 88     -->
 89 
 90     <!-- Define an AJP 1.3 Connector on port 8009 -->
 91     <Connector port="11000" protocol="AJP/1.3" redirectPort="8443" />
 92 
 93 
 94     <!-- An Engine represents the entry point (within Catalina) that processes
 95          every request.  The Engine implementation for Tomcat stand alone
 96          analyzes the HTTP headers included with the request, and passes them
 97          on to the appropriate Host (virtual host).
 98          Documentation at /docs/config/engine.html -->
 99 
100     <!-- You should set jvmRoute to support load-balancing via AJP ie :
101     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
102     -->
103     <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
104 
105       <!--For clustering, please take a look at documentation at:
106           /docs/cluster-howto.html  (simple how to)
107           /docs/config/cluster.html (reference documentation) 
108      -->
109          <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
110 
111     <!-- 
112 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   
113 channelSendOptions="8" >          
114 <Manager className="org.apache.catalina.ha.session.DeltaManager"   
115 expireSessionsOnShutdown="false"   
116 notifyListenersOnReplication="true" />  
117 
118 <Channel className="org.apache.catalina.tribes.group.GroupChannel" >  
119 <Membership className="org.apache.catalina.tribes.membership.McastService"   
120 address="228.0.0.4"   
121 port="45564"   
122 frequency="500"   
123 dropTime="3000" />  
124 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"   
125 address="auto"   
126 port="4000"   
127    
128 selectorTimeout="5000"   
129 maxThreads="6" />  -->
130 <!-- timeout="60000" 
131 <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter" >  
132 <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"  />  
133 </Sender>  
134 <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />  
135 <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />  
136 <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />  
137 </Channel>  
138 
139 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />  
140 <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
141 
142 <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"   
143 tempDir="/tmp/war-temp/"   
144 deployDir="/tmp/war-deploy/"   
145 watchDir="/tmp/war-listen/"   
146 watchEnabled="false" />  
147 
148 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
149 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />  
150 
151 
152 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />  
153 </Cluster>      
154 -->  
155   
156 
157       <!-- Use the LockOutRealm to prevent attempts to guess user passwords
158            via a brute-force attack -->
159       <Realm className="org.apache.catalina.realm.LockOutRealm">
160         <!-- This Realm uses the UserDatabase configured in the global JNDI
161              resources under the key "UserDatabase".  Any edits
162              that are performed against this UserDatabase are immediately
163              available for use by the Realm.  -->
164         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
165                resourceName="UserDatabase"/>
166       </Realm>
167 
168       <Host name="localhost"  appBase="webapps"
169             unpackWARs="true" autoDeploy="true"
170             xmlValidation="false" xmlNamespaceAware="false">
171     <Context path="" docBase="D:\Clusters\static\upload" reloadable="true" crossContext="true"/>
172         <!-- SingleSignOn valve, share authentication between web applications
173              Documentation at: /docs/config/valve.html -->
174         <!--
175         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
176         -->
177 
178         <!-- Access log processes all example.
179              Documentation at: /docs/config/valve.html
180              Note: The pattern used is equivalent to using pattern="common" -->
181         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
182                prefix="localhost_access_log." suffix=".txt"
183                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
184 
185       </Host>
186     </Engine>
187   </Service>
188 </Server>

tomcat2

  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- Note:  A "Server" is not itself a "Container", so you may not
 19      define subcomponents such as "Valves" at this level.
 20      Documentation at /docs/config/server.html
 21  -->
 22 <Server port="9002" shutdown="SHUTDOWN">
 23   <!-- Security listener. Documentation at /docs/config/listeners.html
 24   <Listener className="org.apache.catalina.security.SecurityListener" />
 25   -->
 26   <!--APR library loader. Documentation at /docs/apr.html -->
 27   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 28   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
 29   <Listener className="org.apache.catalina.core.JasperListener" />
 30   <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 31   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 32   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 33   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 34 
 35   <!-- Global JNDI resources
 36        Documentation at /docs/jndi-resources-howto.html
 37   -->
 38   <GlobalNamingResources>
 39     <!-- Editable user database that can also be used by
 40          UserDatabaseRealm to authenticate users
 41     -->
 42     <Resource name="UserDatabase" auth="Container"
 43               type="org.apache.catalina.UserDatabase"
 44               description="User database that can be updated and saved"
 45               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 46               pathname="conf/tomcat-users.xml" />
 47   </GlobalNamingResources>
 48 
 49   <!-- A "Service" is a collection of one or more "Connectors" that share
 50        a single "Container" Note:  A "Service" is not itself a "Container",
 51        so you may not define subcomponents such as "Valves" at this level.
 52        Documentation at /docs/config/service.html
 53    -->
 54   <Service name="Catalina">
 55 
 56     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
 57     <!--
 58     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 59         maxThreads="150" minSpareThreads="4"/>
 60     -->
 61 
 62 
 63     <!-- A "Connector" represents an endpoint by which requests are received
 64          and responses are returned. Documentation at :
 65          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
 66          Java AJP  Connector: /docs/config/ajp.html
 67          APR (HTTP/AJP) Connector: /docs/apr.html
 68          Define a non-SSL HTTP/1.1 Connector on port 8080
 69     -->
 70     <Connector port="9092" protocol="HTTP/1.1"
 71                connectionTimeout="20000"
 72                redirectPort="8443" />
 73     <!-- A "Connector" using the shared thread pool-->
 74     <!--
 75     <Connector executor="tomcatThreadPool"
 76                port="8080" protocol="HTTP/1.1"
 77                connectionTimeout="20000"
 78                redirectPort="8443" />
 79     -->
 80     <!-- Define a SSL HTTP/1.1 Connector on port 8443
 81          This connector uses the JSSE configuration, when using APR, the
 82          connector should be using the OpenSSL style configuration
 83          described in the APR documentation -->
 84     <!--
 85     <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
 86                maxThreads="150" scheme="https" secure="true"
 87                clientAuth="false" sslProtocol="TLS" />
 88     -->
 89 
 90     <!-- Define an AJP 1.3 Connector on port 8009 -->
 91     <Connector port="12000" protocol="AJP/1.3" redirectPort="8443" />
 92 
 93 
 94     <!-- An Engine represents the entry point (within Catalina) that processes
 95          every request.  The Engine implementation for Tomcat stand alone
 96          analyzes the HTTP headers included with the request, and passes them
 97          on to the appropriate Host (virtual host).
 98          Documentation at /docs/config/engine.html -->
 99 
100     <!-- You should set jvmRoute to support load-balancing via AJP ie :
101     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
102     -->
103     <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
104 
105       <!--For clustering, please take a look at documentation at:
106           /docs/cluster-howto.html  (simple how to)
107           /docs/config/cluster.html (reference documentation) 
108      -->
109       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
110   <!-- 
111 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   
112 channelSendOptions="8" >          
113 <Manager className="org.apache.catalina.ha.session.DeltaManager"   
114 expireSessionsOnShutdown="false"   
115 notifyListenersOnReplication="true" />  
116 
117 <Channel className="org.apache.catalina.tribes.group.GroupChannel" >  
118 <Membership className="org.apache.catalina.tribes.membership.McastService"   
119 address="228.0.0.4"   
120 port="45564"   
121 frequency="500"   
122 dropTime="3000" />  
123 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"   
124 address="auto"   
125 port="4000"   
126    
127 selectorTimeout="5000"   
128 maxThreads="6" />  -->
129 <!-- timeout="60000" 
130 <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter" >  
131 <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"  />  
132 </Sender>  
133 <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />  
134 <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />  
135 <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />  
136 </Channel>  
137 
138 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />  
139 <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
140 
141 <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"   
142 tempDir="/tmp/war-temp/"   
143 deployDir="/tmp/war-deploy/"   
144 watchDir="/tmp/war-listen/"   
145 watchEnabled="false" />  
146 
147 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
148 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />  
149 
150 
151 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />  
152 </Cluster>      
153 -->  
154 
155       <!-- Use the LockOutRealm to prevent attempts to guess user passwords
156            via a brute-force attack -->
157       <Realm className="org.apache.catalina.realm.LockOutRealm">
158         <!-- This Realm uses the UserDatabase configured in the global JNDI
159              resources under the key "UserDatabase".  Any edits
160              that are performed against this UserDatabase are immediately
161              available for use by the Realm.  -->
162         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
163                resourceName="UserDatabase"/>
164       </Realm>
165 
166       <Host name="localhost"  appBase="webapps"
167             unpackWARs="true" autoDeploy="true"
168             xmlValidation="false" xmlNamespaceAware="false">
169     <Context path="" docBase="D:\Clusters\static\upload" reloadable="true" crossContext="true"/>
170         <!-- SingleSignOn valve, share authentication between web applications
171              Documentation at: /docs/config/valve.html -->
172         <!--
173         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
174         -->
175 
176         <!-- Access log processes all example.
177              Documentation at: /docs/config/valve.html
178              Note: The pattern used is equivalent to using pattern="common" -->
179         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
180                prefix="localhost_access_log." suffix=".txt"
181                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
182 
183       </Host>
184     </Engine>
185   </Service>
186 </Server>

 

tomcat3 

  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- Note:  A "Server" is not itself a "Container", so you may not
 19      define subcomponents such as "Valves" at this level.
 20      Documentation at /docs/config/server.html
 21  -->
 22 <Server port="9003" shutdown="SHUTDOWN">
 23   <!-- Security listener. Documentation at /docs/config/listeners.html
 24   <Listener className="org.apache.catalina.security.SecurityListener" />
 25   -->
 26   <!--APR library loader. Documentation at /docs/apr.html -->
 27   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 28   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
 29   <Listener className="org.apache.catalina.core.JasperListener" />
 30   <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 31   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 32   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 33   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 34 
 35   <!-- Global JNDI resources
 36        Documentation at /docs/jndi-resources-howto.html
 37   -->
 38   <GlobalNamingResources>
 39     <!-- Editable user database that can also be used by
 40          UserDatabaseRealm to authenticate users
 41     -->
 42     <Resource name="UserDatabase" auth="Container"
 43               type="org.apache.catalina.UserDatabase"
 44               description="User database that can be updated and saved"
 45               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 46               pathname="conf/tomcat-users.xml" />
 47   </GlobalNamingResources>
 48 
 49   <!-- A "Service" is a collection of one or more "Connectors" that share
 50        a single "Container" Note:  A "Service" is not itself a "Container",
 51        so you may not define subcomponents such as "Valves" at this level.
 52        Documentation at /docs/config/service.html
 53    -->
 54   <Service name="Catalina">
 55 
 56     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
 57     <!--
 58     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 59         maxThreads="150" minSpareThreads="4"/>
 60     -->
 61 
 62 
 63     <!-- A "Connector" represents an endpoint by which requests are received
 64          and responses are returned. Documentation at :
 65          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
 66          Java AJP  Connector: /docs/config/ajp.html
 67          APR (HTTP/AJP) Connector: /docs/apr.html
 68          Define a non-SSL HTTP/1.1 Connector on port 8080
 69     -->
 70     <Connector port="9093" protocol="HTTP/1.1"
 71                connectionTimeout="20000"
 72                redirectPort="8443" />
 73     <!-- A "Connector" using the shared thread pool-->
 74     <!--
 75     <Connector executor="tomcatThreadPool"
 76                port="8080" protocol="HTTP/1.1"
 77                connectionTimeout="20000"
 78                redirectPort="8443" />
 79     -->
 80     <!-- Define a SSL HTTP/1.1 Connector on port 8443
 81          This connector uses the JSSE configuration, when using APR, the
 82          connector should be using the OpenSSL style configuration
 83          described in the APR documentation -->
 84     <!--
 85     <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
 86                maxThreads="150" scheme="https" secure="true"
 87                clientAuth="false" sslProtocol="TLS" />
 88     -->
 89 
 90     <!-- Define an AJP 1.3 Connector on port 8009 -->
 91     <Connector port="13000" protocol="AJP/1.3" redirectPort="8443" />
 92 
 93 
 94     <!-- An Engine represents the entry point (within Catalina) that processes
 95          every request.  The Engine implementation for Tomcat stand alone
 96          analyzes the HTTP headers included with the request, and passes them
 97          on to the appropriate Host (virtual host).
 98          Documentation at /docs/config/engine.html -->
 99 
100     <!-- You should set jvmRoute to support load-balancing via AJP ie :
101     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
102     -->
103     <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat3">
104 
105       <!--For clustering, please take a look at documentation at:
106           /docs/cluster-howto.html  (simple how to)
107           /docs/config/cluster.html (reference documentation) -->
108      
109       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
110     
111  <!-- 
112 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"   
113 channelSendOptions="8" >          
114 <Manager className="org.apache.catalina.ha.session.DeltaManager"   
115 expireSessionsOnShutdown="false"   
116 notifyListenersOnReplication="true" />  
117 
118 <Channel className="org.apache.catalina.tribes.group.GroupChannel" >  
119 <Membership className="org.apache.catalina.tribes.membership.McastService"   
120 address="228.0.0.4"   
121 port="45564"   
122 frequency="500"   
123 dropTime="3000" />  
124 <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"   
125 address="auto"   
126 port="4000"   
127    
128 selectorTimeout="5000"   
129 maxThreads="6" />  -->
130 <!-- timeout="60000" 
131 <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter" >  
132 <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"  />  
133 </Sender>  
134 <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />  
135 <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />  
136 <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />  
137 </Channel>  
138 
139 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />  
140 <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />  
141 
142 <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"   
143 tempDir="/tmp/war-temp/"   
144 deployDir="/tmp/war-deploy/"   
145 watchDir="/tmp/war-listen/"   
146 watchEnabled="false" />  
147 
148 <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
149 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;" />  
150 
151 
152 <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />  
153 </Cluster>      
154 -->  
155 
156       <!-- Use the LockOutRealm to prevent attempts to guess user passwords
157            via a brute-force attack -->
158       <Realm className="org.apache.catalina.realm.LockOutRealm">
159         <!-- This Realm uses the UserDatabase configured in the global JNDI
160              resources under the key "UserDatabase".  Any edits
161              that are performed against this UserDatabase are immediately
162              available for use by the Realm.  -->
163         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
164                resourceName="UserDatabase"/>
165       </Realm>
166 
167       <Host name="localhost"  appBase="webapps"
168             unpackWARs="true" autoDeploy="true">
169     <Context path="" docBase="D:\Clusters\static\upload" reloadable="true" crossContext="true"/>
170         <!-- SingleSignOn valve, share authentication between web applications
171              Documentation at: /docs/config/valve.html -->
172         <!--
173         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
174         -->
175 
176         <!-- Access log processes all example.
177              Documentation at: /docs/config/valve.html
178              Note: The pattern used is equivalent to using pattern="common" -->
179         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
180                prefix="localhost_access_log." suffix=".txt"
181                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
182 
183       </Host>
184     </Engine>
185   </Service>
186 </Server>

 

 

 

测试

建立测试项目

建立test项目,需要在项目的web.xml中添加<distributable/>

建立test2.jsp,内容如下(网上都用这个测试,我就省的麻烦了):

 1 <%@ page contentType="text/html; charset=GBK" %>
 2 
 3 <%@ page import="java.util.*" %>
 4 
 5 <html><head><title>Cluster App Test</title></head>
 6 
 7 <body>
 8 
 9 Server Info:
10 
11 <%
12 
13 out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
14 
15 <%
16 
17   out.println("<br> ID " + session.getId()+"<br>");
18 
19   // 如果有新的 Session 属性设置
20 
21   String dataName = request.getParameter("dataName");
22 
23   if (dataName != null && dataName.length() > 0) {
24 
25      String dataValue = request.getParameter("dataValue");
26 
27      session.setAttribute(dataName, dataValue);
28 
29   }
30 
31   out.println("<b>Session 列表</b><br>");
32 
33   System.out.println("============================");
34 
35   Enumeration e = session.getAttributeNames();
36 
37   while (e.hasMoreElements()) {
38 
39      String name = (String)e.nextElement();
40 
41      String value = session.getAttribute(name).toString();
42 
43      out.println( name + " = " + value+"<br>");
44 
45          System.out.println( name + " = " + value);
46 
47    }
48 
49 %>
50 
51   <form action="test2.jsp" method="POST">
52 
53     名称:<input type=text size=20 name="dataName">
54 
55      <br>
56 
57     值:<input type=text size=20 name="dataValue">
58 
59      <br>
60 
61     <input type=submit>
62 
63    </form>
64 
65 </body>
66 
67 </html>

 

上面的测试页面就不解释了,很好理解。

节点插拔测试

 

插拔意思是应该保证当运行的集群中某节点中关闭或者启动时,集群正常工作并且节点能够正常工作。

下面描述测试过程了,贴图太占地方了。

关闭Tomcat2,刷新页面,则不断访问Tocmat1和Tomcat3,再关闭Tomcat1后,则只访问一个Tomcat3,说明节点关闭时运行正常。

如果重启Tomcat2,无论怎么刷新,始终访问Tomcat3,难道Apache不能将请求转发给中途启动的Tomcat2?。。。这时利用另外台机器访问页面,发现Tomcat2正常,然后在刷本地页面,又可以访问Tomcat2了。

从上面可以看出Apache的负载均衡时的算法了,对于每个新来的session,Apache按照节点配置中的lbfactor比重选择访问节点,如果某节点node1不能访问,则寻找下一可访问节点,并且将此node1就在该访问session的访问黑名单中,以后该session的访问直接不考虑node1,即使node1又可以访问了。而新来的session是无黑名单的,如果新的session能够访问到node1了,则会将node1在其他所有session访问的黑名单删除,这样其他session就又能访问node1节点了。以上只是个人经过测试后的猜想。

经过以上测试,说明Tomcat集群和负载均衡已经实现了。

 

关于集群我还有些疑问,所以又测试了下,直接把结论写出来:

1.集群下的相同的应用可以名称不同(好像没必要啊),只要配置server.xml中host下的context具有相同的path即可。

2. 如果应用名称可以不同,那么应用下内容是否可以不同呢(这里考虑将不同应用通过集群看起来是一个应用,并且共享session),然后集群下不同应用映射为相同的访问path,具有相同的路径则负载,如果某路径只某个应用具有,则一直访问该应用。可现实很骨干啊,答案是否定的,至少我以上的配置不能实现。如果访问只有某应用具有的特别路径,那么只有负载到该应用才可以访问,否则直接路径未找到的错误页面了。

 

 

 如果您看过网上其他Apache+Tomcat的集群配置,您可能有的疑问?

1.网上大部分的文章配置2个tocmat的集群,有的将workers.properties下的worker.controller.sticky_session=1,
然后tomcat1中的server.xml中的jvmRoute设置为tomcat2,将tomcat2中的jvmRoute设置为tocmat1,当然我这样设置
也成功了,但是如果3个或者更多tocmat呢,怎么设置每个tomcat的jvmRoute,我不会所以才考虑现在的配置

2.server.xml中的Cluster配置问题,网上大部分都是使用BackupManager方式,即Cluster下又粘贴了一堆配置。其实
只要将其中注释掉的<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>去掉注释就完成session的集群
复制了。只是这俩种复制采用的方式不同而已。http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
这页面已经说的挺清楚了,集群的session复制默认是DeltaManager,是all to all的复制,意思是将集群下1个tomcat应用下的session
对所有的集群中的节点进行复制,即使那个节点没有发布应用。显然是不好的方式,但这在小规模的集群下并没神马问题。
而采用BackupManager,就是众多网上配置那样,对于需要复制的节点设置BackupManager自然也没问题,
但是它的性能并没有DeltaManager 好使“ Downside of the BackupManager: not quite as battle tested as the delta manager”。
因此,具体怎么设置就看大家了,通常说如果不是大规模集群,就默认就好了。反正我自己翻译的就是这个意思了,希望没有误导大家。

来源 :http://www.iteye.com/topic/1017961

 

posted @ 2013-12-13 12:00  mjorcen  阅读(801)  评论(0编辑  收藏  举报