December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

sticky sessions between Apache and tomcat app

Why sticky sessions?

Imagine you have a Apache server which load balances between two tomcat servers(tomcat1 and tomcat2) in the backend, With the use of sticky sessions, When the user sends the first request, If apache routes it to tomcat1, It will keep on sending the other requests made by the same user to the same server. If the tomcat1 is down, Then the user will be forwared to tomcat2 and will be asked to login again as the session information is lost in the tomcat1.

When you use sticky session with session replication, This issue will be solved. Considering the above scenario, When the tomcat1 is down, The session will be carried over to the tomcat2 and the user will still be able to continue the work with out any disruption.

Implementation :

Sticky sessions can be implemented in many ways, out of which I am listing the configuration with proxy and configuration with worker.properties.

Configuration with mod_proxy:

After the installation of apache, Open the http.conf file, Enable the mod_proxy.so, mod_proxy_http.so and proxy_balancer_module.

LoadModule proxy_module mod_proxy.so
LoadModule proxy_http_module mod_proxy_http.so
LoadModule proxy_balancer_module

<IfModule mod_proxy_balancer.c>
ProxyPass /context balancer://tomcatcluster/context stickysession=JSESSIONID|jsessionid
<Proxy balancer://tomcatcluster>
BalancerMember http://tomcatlocalhost:9080 route=tomcatroute1
BalancerMember http://tomcatlocalhost:9081 route=tomcatroute2
</Proxy></IfModule>

Once this configuration is done in apache, Save the httpd.conf and restart the server.

After tomcat server installation, open the server.xml and ensure http connector port is same as the port that is defined in the cluster properties of apache configuration file. For sticky sessions, Ensure that the jvm route value is same as the route value in the httpd.conf file.

tomcatroute1 worker:

<Engine defaultHost=”localhost” name=”Catalina” jvmRoute=”tomcatroute1″>
</Engine><Connector port=”9080″ protocol=”org.apache.coyote.http11.Http11Protocol” … />

tomcatroute2 worker:

<Engine defaultHost=”localhost” name=”Catalina” jvmRoute=”tomcatroute2″>
</Engine>
<Connector port=”9081″ protocol=”org.apache.coyote.http11.Http11Protocol” … />

Save the configuration, Now the sticky sessions are configured.

Configuration with worker.properties:

The tomcat configuration will be same as above, Only the apache configuration would differ.

add the following configuration in httpd.conf.

LoadModule jk_module mod_jk.so
<IfModule mod_jk.c>
JkLogFile logs/mod_jk.log
JkLogLevel debug
JkShmFile logs/jk-runtime-status
JkWorkersFile conf/workers.properties

JkMount /context loadbalancer
JkMount /context/* loadbalancer
</IfModule>

In the worker.properties file, Add the following configuration:

worker.list=loadbalancerworker.tomcatroute1.port=9080worker.tomcatroute1.host=localhostworker.tomcatroute1.type=ajp13worker.tomcatroute1.lbfactor=1worker.tomcatroute2.port=9081worker.tomcatroute2.host=localhostworker.tomcatroute2.type=ajp13worker.tomcatroute2.lbfactor=1worker.loadbalancer.type=lbworker.loadbalancer.balanced_workers=tomcatroute1,tomcatroute2

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>