Tuesday, October 16, 2012

Terracotta and Tomcat Clustering - Page 3

Ok, our tutorial will end by adding Apache web server to load balance the traffic between both Tomcats.

install Apache Web Server 

just run the msi installer or install it from an RPM or from the OS repositories, i will refer to the installation directory as ${APACHE_HOME}. 

point your browser to localhost and make sure you are welcomed with the most famous message

It Works!


install mod_jk

extract the file tomcat-connectors-x.x.xx-windows-arch-httpd-2.2.x.zip, copy mod_jk.so to ${APACHE_HOME}\modules

Configure Apache Web Server


httpd.conf

edit the file ${APACHE_HOME}\conf\httpd.conf to add the following settings:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkMount /examples loadbalancer
JkMount /examples/* loadbalancer
JkMount /status status
JkMount /status/* status

here we are loading the mod_jk module, the mod_jk will load cluster members (Tomcats) from a file named workers.properties under ${APACHE_HOME}\conf directory.
also, any requests coming to apache at /examples will be forwarded to one of our Tomcats.
while /status will direct us to the mod_jk control panel.


workers.properties

create a new file under the directory ${APACHE_HOME}\conf named workers.properties with the following content:
# Define list of workers that will be used
worker.list=loadbalancer,status
# Define tomcat1
worker.tomcat1.port=8009
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
# Define tomcat2
worker.tomcat2.port=8010
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1,tomcat2
worker.loadbalancer.sticky_session=0
# Status worker for managing load balancer
worker.status.type=status

note that the worker names must match the jvmRoute configurations on the Tomcats. also, you can enable sticky sessions or disable to fit your need, however, i have turned it off here.

restart apache web server, if everything went as it should, navigating to http://localhost/examples would result in this page.

if you have debugging installed on your browser, like firebug, you can check your JSESSIONID value, you will find it post fixed with the jvmRoute value of the tomcat server who served the response.
if you hit the refresh button you can see that you are moving for every hit from one Tomcat to the other.

Testing Sessions Failover

point your browser to http://localhost/examples/servlets/servlet/SessionExample add a couple of values in the session.

now it is up to you, bring any Tomcat down (not both, of course) or any Terracotta (not both, of course), play around, your minimal setup is one Tomcat and  one Terracotta server.
your values in the session should never disappear!!!

Have Fun.


11 comments:

hazem hegazi said...

Nice topic...
I wanted to give terracotta a test drive; thanks for sharing this :)

hodgester said...

This is good info, but I'm a bit confused - it looks like you are following the express install for terracotta, which to my understanding requires objects to be serializable? But then again, you have a dso section, but no instrumented classes?

moutaz salem said...

hodgester,
thanks for stopping by.
you are right express install requires serialized objects, but i just wanted to point out the capability of terracotta.
for the instrumented classes, we used the tomcat client which is provided by terracotta themselves so i guess there are extra information being sent to the server other than the xml configurations.

thanks again.

moutaz salem said...

hazem hegazi,

thanks a lot for you comment, you should try terracotta.

Yurii Luchkiv said...

Where I can take - tomcat-connectors-x.x.xx-windows-arch-httpd-2.2.x.zip?????

moutaz salem said...

Hi Yurii,

please check this URL http://mirror.cc.columbia.edu/pub/software/apache//tomcat/tomcat-connectors/jk/binaries/windows/

Unknown said...

Thank you so much for this detailed tutorial. It helped me a lot.

ආගන්තුකයා | Stranger said...

This is working for Spring also. But When We use vaadin sessions it is not working. Can you help me with That? ?

Alpesh Bhavsar said...

Nice post. I tried terracotta and its working now. Thank you.

Brahanmadhav V said...

HI moutaz,

While restarting the server its saying "Cant load mod_jk.so into server: The specified procedure could not be found"
But I've loaded the file into the modules folder. Could you help me with that?

Thanks in advance

prathap kumar said...

Great Article

Java EE course
Java J2EE Training Institutes in Chennai
Java Training in Chennai

Java Training




Books for Hibernate frameworkk
Hibernate Interview Questions