Jan 24, 2010

Load Balancing & Clustering with Tomcat, Apache web server & JK Connector

Clustering allows us to run an application on several parallel servers (i.e. cluster nodes). The load is distributed across different servers, and even if any of the servers fails the application is still accessible via other cluster nodes. Clustering is crucial for scalable enterprise applications, as you can improve performance by simply adding more nodes to the cluster.

This document provides step by step instructions for configuring Apache server which handles  
request for static contents and delegates request for dynamic pages (i.e. JSP or Servlet) to two  instances of Tomcat server using JK Connectors.

  • Installation of Apache Server
  • Setting up tomcat instances 
  • Configuration of Clustering for tomcat instances with JK Connectors 
Installation of Apache Server

Download windows installer for Apache Server 2.0.63 from http://httpd.apache.org/download.cgi and install the same into C:\Program Files\Apache 
Software Foundation.

Setting up tomcat instances

Download apache-tomcat-6.0.20 from http://tomcat.apache.org. Create 3 instances of tomcat named tomcatA, tomcatB with following configuration in conf/server.xml.

Uncomment the following entry in server.xml in all the instances of tomcat.

<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->

Define the HTTP, AJP Connector, Shutdown ports for all the tomcat instances as below,


tomcatA

HTTP8081
Shutdown8105
AJP Connector 8109
jvmRoutetomcatA

tomcatB

HTTP8082
Shutdown8205
AJP Connector 8209
jvmRoutetomcatB

Deploying web application in all tomcat instances:


Deploy the Hello world JSP web application into all the tomcat instances, Make your HelloWorld.jsp with following content to feel the load balancing and session replication,


tomcatA – hello.jsp


<%
session.setAttribute(“name” , “bala”);
%>
From tomcatA - Name from session - <%= session.getAttribute(“name”); %>
Session ID - <%= session.getID() %>

tomcatB – hello.jsp


From tomcatB - Name from session - <%= session.getAttribute(“name”); %>
Session ID - <%= session.getID() %>

<distributable/> tag should be defined in web.xml of your application, so that session replication will be supported by the web container.


web.xml


<webapp>

..
<distributable/>
..
..
</webapp>

Configuring JK Connector for routing request to Tomcat from Apache Server:


Download JK connector 1.2.8 from http://tomcat.apache.org/connectors-doc and place mod_jk-1.2.28-httpd-2.2.3.so in modules/ directory of Apache server.


Add following configuration to httpd.conf file of Apache server,


LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.3.so
JkWorkersFile C:\cluster\workers.properties
JkLogFile "logs/mod_jk.log"
JkLogLevel trace
JkMount /cluster loadbalancer
JkMount /cluster/* loadbalancer

workers.properties should have the details about the tomcat instances which we have created as below,


workers.tomcat_home=/tomcatA
workers.java_home=$JAVA_HOME
ps=/
worker.list=tomcatA,tomcatB,loadbalancer
worker.tomcatA.port=8109
worker.tomcatA.host=localhost
worker.tomcatA.type=ajp13
worker.tomcatA.lbfactor=1
worker.tomcatB.port=8209
worker.tomcatB.host=localhost
worker.tomcatB.type=ajp13
worker.tomcatB.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcatA,tomcatB
worker.loadbalancer.sticky_session=1

Now restart the Apache Server, TomcatA and TomcatB instances and access the HelloWorld application via URL, http://localhost/Helloworld/hello.jsp.


Now you can see the session created by tomcatA will available in tomcatB also when accessing

the above URL repeatedly.

0 comments:

Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by