Skip to main content

Session stickiness with Apache,mod_proxy and Glassfish

Posted by bhaktimehta on April 25, 2012 at 1:31 PM PDT

Loadbalancers can set thier cookies indicating which backend server can serve a user's requests and direct future requests to the same server.
I was experimenting with session stickiness using apache, mod_proxy and Glassfish and here is a short summary of the steps I executed.

Apache 2 is installed on Mac OSX in /etc/apache2
mod_proxy,mod_proxy_http, mod_proxy_ajp and mod_proxy_balancer comes as part of
standard Apache 2.2+ distribution

This link shows the layout of Apache HTTPD on different operating systems

For the Mac OSX you can see the file layout as shown here

ServerRoot              ::      /usr
Primary Config Fle      ::      /etc/apache2/httpd.conf
DocumentRoot            ::      /Library/WebServer/Documents
ErrorLog                ::      /var/log/apache2/error_log
AccessLog               ::      /var/log/apache2/access_log
cgi-bin                 ::      /Library/WebServer/CGI-Executables (empty by default)
binary                  ::      /usr/sbin/httpd
start/stop              ::      /usr/sbin/apachectl (start|stop|restart|fullstatus|status|graceful|graceful-stop|configtest|help)

These are the following steps I followed to configure sticky sessions.

edit the /etc/apache2/httpd.conf to add these following lines


        BalancerMember http://localhost:28080 route=i1
        BalancerMember http://localhost:28081 route=i2
        BalancerMember http://localhost:28083 route=i3


ProxyPreserveHost On

ProxyPass /clusterjsp balancer://hacluster/clusterjsp stickysession=JSESSIONID


SetHandler balancer-manager
Order Deny,Allow
Allow from all

Here I have added 3 BalancerMembers which are 3 glassfish instances I have created.
I have also enabled the balancer-manager support using the above lines with Location /balancer-manager

I am working with the glassfish 4.0 installation from the trunk but this should work with any Glassfish 3.x release. This is a very simple setup with 3 instances.

cd $GF_HOME/bin
asadmin start-domain
asadmin create-cluster c1

Create 3 instances
asadmin create-local-instance --cluster c1 i1
asadmin create-local-instance --cluster c1 i2
asadmin create-local-instance --cluster c1 i3
asadmin start-cluster c1

asadmin deploy --target c1 clusterjsp.ear  The javaee samples can be downloaded from http://glassfish-samples.java.net/,
I have attached the ear in the zip file below

asadmin create-system-properties --target i1 INSTANCE=i1
asadmin create-system-properties --target i2 INSTANCE=i2
asadmin create-system-properties --target i3 INSTANCE=i3
asadmin create-jvm-options --target c1 -DjvmRoute=\${INSTANCE}

Note the values for the system properties INSTANCE i1 and i2 and i3 are the same as what is mentioned in the route element of the httpd.conf above

Now start apache2
sudo su
/etc/apache2/apachectl start

On the browser send a get request to http://localhost/clusterjsp/HaJsp.jsp
You can see the http headers using Chrome->View->Developer Tools ->Network tab

JSESSION_ID added to Response

You will see the Set-Cookie header in the response has the JSESSION_ID appended which points to i2

You can Reload the page and see future requests are directed to the instance i2

Sticky sessions

You can also experiment with failover by stopping one of the instances in which case the request gets directed to a new server and session affinity starts with that server.
You can also access load balancer manager by using a Web browser to access the page http://localhost/balancer-manager

AttachmentSize
cluster-jsp.zip4.13 KB
Screen_Shot_2012-04-25_at_10.46.58_AM.png114.07 KB
Screen_Shot_2012-04-25_at_10.47.46_AM.png137.5 KB
Related Topics >>