linux+nginx+tomcat负载均衡,实现session同步

软件及环境是:
虚拟机上装centos 5.5
IP为:192.168.0.51 装上nginx和tomcat  6.0.32 命名为 Tomcat1
一台win7上装tomcat  6.0.32  IP为:192.168.0.50  命名为 Tomcat2

首先装nginx,我是参照http://blog.s135.com/nginx_php_v6/ 配了一个Nginx + PHP(FastCGI)环境,然后再加上Tomcat的转发。具体配置已略!

下一步是配置Tomcat集群。分别打开tomcat1和tomcat2下conf中server.xml
<Server port="8005" shutdown="SHUTDOWN">
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080" maxHttpHeaderSize="8192"
   maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
   enableLookups="false" redirectPort="8443" acceptCount="100"
   connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

找到<Engine name="Catalina" defaultHost="localhost">
Tomcat1下的修改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
Tomcat2下的修改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

tomcat1找到
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
修改为 

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
            channelSendOptions="6">

            <Manager className="org.apache.catalina.ha.session.DeltaManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"/>
            <!--
            <Manager className="org.apache.catalina.ha.session.BackupManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"
                mapSendOptions="6"/>
            -->
            <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService" 
                    address="228.0.0.5" 
                    bind="192.168.0.51" 
                    port="45564" 
                    frequency="500" 
                    dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
                    address="192.168.0.51" 
                    autoBind="100" 
                    port="4001" 
                    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"/>
            </Channel>
            <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
            
            <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
            <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>



tomcat2找到
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
修改为 

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
            channelSendOptions="6">

            <Manager className="org.apache.catalina.ha.session.DeltaManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"/>
            <!--
            <Manager className="org.apache.catalina.ha.session.BackupManager"
                expireSessionsOnShutdown="false"
                notifyListenersOnReplication="true"
                mapSendOptions="6"/>
            -->
            <Channel className="org.apache.catalina.tribes.group.GroupChannel">
                <Membership className="org.apache.catalina.tribes.membership.McastService" 
                    address="228.0.0.5" 
                    bind="192.168.0.50" 
                    port="45564" 
                    frequency="500" 
                    dropTime="3000"/>
                <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
                    address="192.168.0.50" 
                    autoBind="100" 
                    port="4000" 
                    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"/>
            </Channel>
            <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
            
            <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
            <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>



还需要在程序的web.xml里面</web-app>前面加入以下这句话
<distributable/> 
session同步到这里设置完毕

服务器的启动顺序如下: 
tomcat1 --> |tomcat2 --> |nginx 

先启动tocmat1,tomcat1启动完全的时候再启动tomcat2,等两个tocmat全启动之后,再启动nginx

负载的时候可以用APACHENGINX,如果什么都不用,两台一接防问了话,可能会出错极session不同步的问题,网上也很多人介意不要做seeeion同步,这样会降低机器的性能,有一个方法可以,就是NGINXip_hash,这样至少可以保证客户端去防问同一台TOCMAT,除非那台TOCMAT挂了


你可能感兴趣的:(linux+nginx+tomcat负载均衡,实现session同步)