首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache 2.2.x+Tomcat6配备集群

2012-11-01 
Apache 2.2.x+Tomcat6配置集群在我们部署Web应用时,不可避免大多都会涉及到集群问题,此文作为Apache 2.2.x

Apache 2.2.x+Tomcat6配置集群
在我们部署Web应用时,不可避免大多都会涉及到集群问题,此文作为Apache 2.2.x+Tomcat6配置集群的备忘。

配置Tomcat6 的conf\server.xml文件,启用集群设置,如果在本地同时运行多个Tomcat进行测试的话,需要修改部分端口号,以避免冲突
打开AJP:<Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />
打开集群:
Xml代码
   <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm2">   
 
     <!--For clustering, please take a look at documentation at:  
         /docs/cluster-howto.html  (simple how to)  
         /docs/config/cluster.html (reference documentation) --> 
       
      
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  channelSendOptions="6">      
    <Manager className="org.apache.catalina.ha.session.BackupManager"      
    expireSessionsOnShutdown="false"      
    notifyListenersOnReplication="true"      
    mapSendOptions="6"/>      
    <!--      
    <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="5001"      
    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> 
 
 
         
 
     <!-- The request dumper valve dumps useful debugging information about  
          the request and response data received and sent by Tomcat.  
          Documentation at: /docs/config/valve.html --> 
     <!-- 
     <Valve className="org.apache.catalina.valves.RequestDumperValve"/> 
     --> 
 
     <!-- This Realm uses the UserDatabase configured in the global JNDI  
          resources under the key "UserDatabase".  Any edits  
          that are performed against this UserDatabase are immediately  
          available for use by the Realm.  --> 
     <Realm className="org.apache.catalina.realm.UserDatabaseRealm" 
            resourceName="UserDatabase"/> 
 
     <!-- Define the default virtual host  
          Note: XML Schema validation will not work with Xerces 2.2.  
      --> 
     <Host name="localhost"  appBase="webapps" 
           unpackWARs="true" autoDeploy="true" 
           xmlValidation="false" xmlNamespaceAware="false"> 
 
       <!-- SingleSignOn valve, share authentication between web applications  
            Documentation at: /docs/config/valve.html --> 
       <!-- 
       <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 
       --> 
 
       <!-- Access log processes all example.  
            Documentation at: /docs/config/valve.html --> 
       <!--  
       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"    
              prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> 
       --> 
 
     </Host> 
   </Engine> 

    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm2">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
     
    
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  channelSendOptions="6">   
<Manager className="org.apache.catalina.ha.session.BackupManager"   
expireSessionsOnShutdown="false"   
notifyListenersOnReplication="true"   
mapSendOptions="6"/>   
<!--   
<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="5001"   
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>


       

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" 
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

      </Host>
    </Engine>2.打开Apache下的conf\httpd.conf文件,打开注释:

Html代码
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 

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

在文件末尾添加:

Html代码
ProxyRequests Off     
<Proxy balancer://cluster>      
    BalancerMember ajp://localhost:8009 loadfactor=1 route=jvm1 
    BalancerMember ajp://localhost:9009 loadfactor=1 route=jvm2 
</Proxy>  

ProxyRequests Off  
<Proxy balancer://cluster>   
BalancerMember ajp://localhost:8009 loadfactor=1 route=jvm1
BalancerMember ajp://localhost:9009 loadfactor=1 route=jvm2
</Proxy> 配置VirtualHost:

Xml代码
<VirtualHost *:80>     
         ServerAdmin test@126.com     
         ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On     
         ProxyPassReverse / balancer://cluster/   
         ServerName www.mytest.com  
         ServerAlias mytest.com    
</VirtualHost>  

<VirtualHost *:80>  
         ServerAdmin test@126.com  
         ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On  
         ProxyPassReverse / balancer://cluster/
         ServerName www.mytest.com
         ServerAlias mytest.com 
</VirtualHost> 

在应用程序的web.xml结尾加入:

Html代码
<distributable/> 

<distributable/>

运行http://www.mytest.com,从tomcat日志可看出均衡和集群都起了作用,以上配置在WinXP +  Apache 2.2.8 + tomcat6上测试通过!

热点排行