linux apahce tomcat 实现负载均衡和Session 共享

困扰几天的Apahce tomcat 实现负载均衡和Session 共享今天终于在本机实现了。期间参考了大量的网上资料,但只实现了Apahce的负载均衡,但就是实现不了Session 共享。下面就先说下Apahce的负载均衡吧,从网上看有三种方式ajp,http,mod_jk。我在这里只说下我用的ajp方式,其他方法网上都能查到。

环境:

我用的是中标操作系统,内置的httpd服务,下载了tomcat7,复制一份命名为tomcat70。即一个 apache作负载均衡器,两个tomcat 实现集群的搭配。http默认安装位置:/etc/httpd,修改conf/httpd.conf文件,主要加入反向代理方面的内容:


ProxyRequests Off
<proxy balancer://loadbalancer>
BalancerMember ajp://127.0.0.1:8009  route=jvm1
BalancerMember ajp://127.0.0.1:8010  route=jvm2
</proxy> 


打开VirtualHost*80的注释,加入下面内容:


ServerName 127.0.0.1
 ServerAlias localhost
 ProxyPass / balancer://loadbalancer/ stickysession=jsessionid nofailover=On
 ProxyPassReverse / balancer://loadbalancer/  

我的两个tomcat的ajp端口分别是 8009和8010。route的jvm1,jvm2分别与tomcat中的配置文件一致,下面就介绍两个tomcat的配置。


tomcat7的配置文件如下:


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

  <!-- Security listener. Documentation at /docs/config/listeners.html

  <Listener className="org.apache.catalina.security.SecurityListener" />

  -->

  <!--APR library loader. Documentation at /docs/apr.html -->

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <Listener className="org.apache.catalina.core.JasperListener" />

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />



  <!-- Global JNDI resources

       Documentation at /docs/jndi-resources-howto.html

  -->

  <GlobalNamingResources>

    <!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    -->

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



  <!-- A "Service" is a collection of one or more "Connectors" that share

       a single "Container" Note:  A "Service" is not itself a "Container",

       so you may not define subcomponents such as "Valves" at this level.

       Documentation at /docs/config/service.html

   -->

  <Service name="Catalina">



    <!--The connectors can use a shared executor, you can define one or more named thread pools-->

    <!--

    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"

        maxThreads="150" minSpareThreads="4"/>

    -->





    <!-- A "Connector" represents an endpoint by which requests are received

         and responses are returned. Documentation at :

         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)

         Java AJP  Connector: /docs/config/ajp.html

         APR (HTTP/AJP) Connector: /docs/apr.html

         Define a non-SSL HTTP/1.1 Connector on port 8080

    -->

    <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

    <!-- A "Connector" using the shared thread pool-->

    <!--

    <Connector executor="tomcatThreadPool"

               port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

    -->

    <!-- Define a SSL HTTP/1.1 Connector on port 8443

         This connector uses the JSSE configuration, when using APR, the

         connector should be using the OpenSSL style configuration

         described in the APR documentation -->

    <!--

    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

               maxThreads="150" scheme="https" secure="true"

               clientAuth="false" sslProtocol="TLS" />

    -->



    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />





    <!-- An Engine represents the entry point (within Catalina) that processes

         every request.  The Engine implementation for Tomcat stand alone

         analyzes the HTTP headers included with the request, and passes them

         on to the appropriate Host (virtual host).

         Documentation at /docs/config/engine.html -->



    <!-- You should set jvmRoute to support load-balancing via AJP ie :

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

    -->

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">



      <!--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="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="224.0.0.4" bind="127.0.0.1"  port="45564" frequency="500" dropTime="3000" />     
  <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
address="auto" port="4001" 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" />     
  <Interceptor      
className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />     
  </Channel>     
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter="" />     
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>      
 <ClusterListener       
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />     
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />     
  </Cluster>



      <!-- Use the LockOutRealm to prevent attempts to guess user passwords

           via a brute-force attack -->

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <!-- 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"/>

      </Realm>



      <Host name="localhost"  appBase="webapps"

            unpackWARs="true" autoDeploy="true">



        <!-- 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

             Note: The pattern used is equivalent to using pattern="common" -->

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log." suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />



      </Host>

    </Engine>

  </Service>

</Server>

tomcat70的配置文件如下:



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

  <!-- Security listener. Documentation at /docs/config/listeners.html

  <Listener className="org.apache.catalina.security.SecurityListener" />

  -->

  <!--APR library loader. Documentation at /docs/apr.html -->

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <Listener className="org.apache.catalina.core.JasperListener" />

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />



  <!-- Global JNDI resources

       Documentation at /docs/jndi-resources-howto.html

  -->

  <GlobalNamingResources>

    <!-- Editable user database that can also be used by

         UserDatabaseRealm to authenticate users

    -->

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



  <!-- A "Service" is a collection of one or more "Connectors" that share

       a single "Container" Note:  A "Service" is not itself a "Container",

       so you may not define subcomponents such as "Valves" at this level.

       Documentation at /docs/config/service.html

   -->

  <Service name="Catalina">



    <!--The connectors can use a shared executor, you can define one or more named thread pools-->

    <!--

    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"

        maxThreads="150" minSpareThreads="4"/>

    -->





    <!-- A "Connector" represents an endpoint by which requests are received

         and responses are returned. Documentation at :

         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)

         Java AJP  Connector: /docs/config/ajp.html

         APR (HTTP/AJP) Connector: /docs/apr.html

         Define a non-SSL HTTP/1.1 Connector on port 8080

    -->

    <Connector port="8081" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

    <!-- A "Connector" using the shared thread pool-->

    <!--

    <Connector executor="tomcatThreadPool"

               port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

    -->

    <!-- Define a SSL HTTP/1.1 Connector on port 8443

         This connector uses the JSSE configuration, when using APR, the

         connector should be using the OpenSSL style configuration

         described in the APR documentation -->

    <!--

    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

               maxThreads="150" scheme="https" secure="true"

               clientAuth="false" sslProtocol="TLS" />

    -->



    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />





    <!-- An Engine represents the entry point (within Catalina) that processes

         every request.  The Engine implementation for Tomcat stand alone

         analyzes the HTTP headers included with the request, and passes them

         on to the appropriate Host (virtual host).

         Documentation at /docs/config/engine.html -->



    <!-- You should set jvmRoute to support load-balancing via AJP ie :

    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

    -->

    <Engine name="Catalina" 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="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="224.0.0.4" bind="127.0.0.1"  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" />     
  <Interceptor      
className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />     
  </Channel>     
  <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""  />     
  <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve" />     
  <ClusterListener      
className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />     
  <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener" />     
  </Cluster>



      <!-- Use the LockOutRealm to prevent attempts to guess user passwords

           via a brute-force attack -->

      <Realm className="org.apache.catalina.realm.LockOutRealm">

        <!-- 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"/>

      </Realm>



      <Host name="localhost"  appBase="webapps"

            unpackWARs="true" autoDeploy="true">



        <!-- 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

             Note: The pattern used is equivalent to using pattern="common" -->

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

               prefix="localhost_access_log." suffix=".txt"

               pattern="%h %l %u %t &quot;%r&quot; %s %b" />



      </Host>

    </Engine>

  </Service>

</Server>

大家可以从上面看出两个文件基本一致,主要是几个端口不同而已。刚开始我也基本配置到如此,但是测试总是不行session 还是变来变去,也不知道看了网上多少篇文章,感觉都一模一样,但还是解决不了,我又仔细对比,终于在一篇文章中看到了Membership 中的一个bind属性没设置,加上这个属性居然好了。不明白为啥?我又试着去去掉,发现去掉就不行,有人知道吗?不过总算弄出来了。我走了很多弯路,希望能给遇到同样问题的人带来点帮助仅次而已。


在配置过程中还遇到tomcat的一个如下错误,刚开始怎么都知道什么原因,看了很多解决方法,都没解决,后来查看了日志,发现不能访问Catalina这个文件夹,才明白了是权限问题,我总结在linux下遇到问题,应该首先想到权限问题


org.apache.jasper.JasperException: java.lang.NullPointerException  

参考文章:http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/index.html

你可能感兴趣的:(apache,tomcat,linux)