Ok, our tutorial will end by adding Apache web server to load balance the traffic between both Tomcats.
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.
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.
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.

