Apache+AJP+Tomcat6+Session复制配置

系统环境:Windows XP SP3 ,jdk1.6
软件版本:apache_2.2.11-win32-x86-openssl-0.9.8i.msi apache-tomcat-6.0.16 

备注:tomcat可以下载压缩包直接解压就可以,另外也下载整合了APR(Apache Tomcat Native library 1.1.16)
Apache和Tomcat的整合个人推荐使用AJP的方式,配置简单,而且现在tomcat官方鼓励采用这种方式呢!
我是在同一台机器上作的测试,因为我完成了apache的负载均衡以及tomcat6的集群和session的复制,所以需要修改到的地方可能比较多,先将配置列出如下:
一、AJP方式的配置:
1、 修改apache的httpd.conf
首先要load所需要的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_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so

ProxyPass /images !
ProxyPass /css !
ProxyPass /js !

<Proxy balancer://cluster/>
BalancerMember ajp://localhost:8009/ route=tomcat1 loadfactor=50
BalancerMember ajp://localhost:9009/ route=tomcat2 loadfactor=50
</Proxy>

开启虚拟主机
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

修改httpd-vhosts.conf:
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "D:\www\webapps"
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
    ProxyRequests Off
    ProxyPass / balancer://cluster/ stickysession=JSESSIONID nofailover=On
    ProxyPassReverse / balancer://cluster/
    #ProxyPassReverse /test balancer://cluster/ test
</VirtualHost>

三、修改%TOMCAT_HOME%/conf/server.xml以配置集群(跟使用AJP和JK方式没有关系,皆通用) :

把原来的<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 修改为以下配置,注意Receiver中的 port="5001"要保持每个tomcat不一致,
      <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;"/>
       <!--
        <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
        -->
        <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.JvmRouteSessionIDBinderListener"/>
        -->
        <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
      </Cluster>
四、实现session复制(在%TOMCAT_HOME%/conf/server.xml文件中) 
第一步:
Tomcat 6.0.16
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
<!--   
    <Engine name="Catalina" defaultHost="localhost">
-->
Tomcat 6.0.18
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat2">
<!--   
    <Engine name="Catalina" defaultHost="localhost">
-->

  第二步:
Webapps目录下的应用程序的web.xm中必须添加
<distributable/> 此应用将与群集服务器复制Session

因为是在同一台机器上启动多个tomcat,所以修改监听的端口是必不可少的,这里将我的配置列出如下:

首先每个tomcat的<Server port="8006" shutdown="SHUTDOWN">中的port要不一致
Tomcat 6.0.16
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" acceptCount="5000" maxThreads="2000"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Tomcat 6.0.16
<Connector port="9080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="9443" acceptCount="5000" maxThreads="2000"/>
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443" />
配置完成,先启动所有tomcat服务器,最后启动apache2.2服务器
参考:http://enet-java.iteye.com/blog/339922

你可能感兴趣的:(session)