Sihan’s Blog

Tech Talks

Archive for the ‘Server Scalability’ Category

Load Balancing Tomcat with Apache

with 4 comments

Recently I was working on server scalability issue of one of my projects. So I had to went through some RnD on load balancing and finally was able to configure it. I used tomcat as servlet container and apache as load balancer. I used mod_proxy and mod_proxy_balancer module of apache for load balancing between two tomcat instances running on port 8080 and 3333.

Here is the details:

Server used:
Tomcat 6.0.16 as servlet container x 2
Apache 2.2.6 as load balancer x 1

Testing Tool:

Jakarta JMeter 2.6.1

Apache Configuration:

Here, I created a cluster named mycluster using 2 tomcat server and sticksession named as JSESSIONID. My test application myapp was running on both of the tomcat server.

I used loadfactor 50 on both of the tomcat server (meaning both of them will be sharing equal load). I

1. Enable mod_proxy: In httpd.conf enable/add

LoadModule proxy_module modules/mod_proxy.so

2. Enable mod_proxy_balancer: In httpd.conf

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

3. Configure the load balancer on the cluster and balancer manager:

In httpd.conf add these lines:

ProxyPass /myapp balancer://mycluster stickysession=JSESSIONID

<Proxy balancer://mycluster>

BalancerMember http://localhost:8080/myapp route=tomcat1 loadfactor=50
BalancerMember http://localhost:3333/myapp route=tomcat2 loadfactor=50

</Proxy>

<Location /balancer-manager>
SetHandler balancer-manager
</Location>

After restarting the server and hitting http://localhost/balancer-manager in a browser it showed me the configurations of the cluster.

Tomcat Configuration:

1. In server.xml file of tomcat running on 8080 port I configured the jvm route as tomcat1 as :

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

By editing the engine tag.

2. In server.xml file of tomcat running on 3333 port I configured the jvm route as tomcat1 the same way:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

The testing application:

I used a single servlet for the test application myapp.

Here is the code snippet of the servlet named myservlet:

ServletContext sc = this.getServletContext();
String servername = sc.getInitParameter(
"servername");
out.print(
"This is from ::" + servername);

In web.xml of the application I added a context parameter named servername which value was server1 while deploying into tomcat1(on port 8080) and server2 on tomcat2(running on 3333 port).

Testing:

With JMeter I created a script for hiitting the url http://localhost/myapp/myservlet using 400 concurrent thread in 20 loops to see the effect.