Sessions are always recreated after enabling clustering

284 views Asked by At

Apache/Tomcat setup:

            Apache
              |
             / \
           /     \
         C1       C2           Clusters
         |         |
        / \       / \
      /     \   /     \
     T1     T2  T3     T4      Tomcats
  • Windows Server 2012
  • Apache 2.2.25
  • mod_jk 1.2.20
  • Tomcat 7.0.42

I have load balancing on C1 and C2. T1 and T2 are a group and T3 and T4 are another group. C1 and C2 are independant.

workers.properties:

worker.list=jkstatus,Cluster1,Cluster2,C1T1,C1T2,C2T1,C2T2
worker.jkstatus.type=status

# Configuration Cluster 1
worker.Cluster1.type=lb
worker.Cluster1.balance_workers=C1T1,C1T2

worker.C1T1.port=8119
worker.C1T1.host=localhost
worker.C1T1.type=ajp13
worker.C1T1.lbfactor=1
worker.C1T1.redirect=C1T2

worker.C1T2.port=8129
worker.C1T2.host=localhost
worker.C1T2.type=ajp13
worker.C1T2.lbfactor=1
worker.C1T2.redirect=C1T1

# Configuration Cluster 2
worker.Cluster2.type=lb
worker.Cluster2.balance_workers=C2T1,C2T2

worker.C2T1.port=8219
worker.C2T1.host=localhost
worker.C2T1.type=ajp13
worker.C2T1.lbfactor=1
worker.C2T1.redirect=C2T2

worker.C2T2.port=8229
worker.C2T2.host=localhost
worker.C2T2.type=ajp13
worker.C2T2.lbfactor=1
worker.C2T2.redirect=C2T1

Apache redirect every requests either to Cluster1 or Cluster2 this way:

JkMount /MyApp/* Cluster1

server.xml:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="C1T1">
    <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="228.0.0.4" port="45562" frequency="500" dropTime="3000"/>
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
                <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
            <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=""/>
        <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>

    <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
    </Realm>

    <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
        <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>

T1/T2 have the same address/port and T3/T4 have the same address/port (different from T1/T2).

I followed this: http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html and this: http://tomcat.apache.org/connectors-doc/reference/workers.html

Load balacing works fine. If I shut down one Tomcat on a Cluster, Apache redirect all requests to the other Tomcat. Sessions are replicated too (I used JConsole to track the number of active sessions on both Tomcat).

Now, here is my problem:

Every time I hit F5 a new session is created (I use @SessionBean on Tomcat). If I bypass the cluster by accessing directly the Tomcat with its HTTP port, a new session is created, but no new sessions are created when I hit F5.

I would like to know why when accessed with Apache, a new session is created on every request (and how to fix it!).

1

There are 1 answers

0
Simon Arsenault On

Finally figured it out.

Clustering did not cause this. It was the JSESSIONID cookie of JSF: its path was /MyApp/ instead of / (apache forward / to /MyApp/).

EDIT: Had to set sessionCookiePath="/" on the <Context ...></Context> file of the tomcats (conf/context.xml) like this:

<Context sessionCookiePath="/">
    ...
</Context>