While application gets complex and start serving concurrent users, there is always need to make maximum use of available hardware and ensure every request is served within reasonable time. While its possible to increase heap size (using Xmx, Xms options), the option is discouraged as it ends up introducing unnecessary GC pauses in JVM and instead of improving application performance it will lead to serious bottlenecks. The other option would be distribute the user requests over cluster of servers and thereby ensuring the load is evenly distributed against many servers. The option will also enable maximum availability of application and will serve more requests than bombarding one server. Also the underlying OS places constraints on the resources consumed by an individual process and hence it is always recommended to distribute the job across multiple processes.
Load balancing can give you a better option to make full use of hardware and system resources. There are numerous open source / freeware / commercial implementations are available with variety of features to meet the application requirements. While I was exploring how to configure one, I stumbled upon Apache HTTP server’s load balancing feature. Apache Load balancer provides 3 algorithms to configure the load balancing. One must carefully select the algorithm depending upon the server infrastructure otherwise the resources may remain under utilized.
I thought of exploring this feature and configured the basic version of Load Balancer. Here are the details for same.
Environment
1. Apache Http Server 2.2
2. Apache Tomcat 6.0
3. Eclipse 3.7 (Indigo) – (Though this doesn’t play any role in load balancing, I just used it to develop sample application.)
Steps to configure basic version of Load Balancer
1. Create sample web application with “index.jsp” file as welcome file. Note that this is just a sample application to output response when user hits URL. Check following sample code of JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>Hello World Hello World
<% System.out.println("You called me " + System.currentTimeMillis()); %>
2. Create 3 copies of Apache Tomcat installation, refer following screenshot of what I have done.
3. Open “server.xml” available in “conf” folder of each Tomcat server and increment each port. This is required to ensure that when Tomcat is started on single machine, the ports doesn’t clash with each other.
4. Open “httpd.conf” available in “conf” folder of Apache HTTP server. Configure following lines at the end of the file.
BalancerMember http://localhost:8080 BalancerMember http://localhost:8081 BalancerMember http://localhost:8082 ProxyPass / balancer://mycluster/
(Note the “/” in ProxyPass statement after “mycluster”, if you remove it, you will find erroneous result.)
5. Locate following lines and remove the “#” in front of them.
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_http_module modules/mod_proxy_http.so
And… Woyala… you are done with basic load balancer configuration. Easy huh!
Now start all Tomcat instances and then start Apache HTTP Server. Hit the browser (note you must hit the URL with port on which Apache HTTP server has been started and not Tomcat URL) and you should see something like
(I have named my war as “HelloWorld.war”)
If you check the console, you should see something like.
Tomcat instance – 1
Tomcat instance – 2
Tomcat instance – 3
For detailed configuration details on load balancing refer following URL.
Apache Website
If you want to configure AJP version of Tomcat, check following URL
Apache Tomcat
Stackoverflow
2 replies on “Load balancer with Apache HTTP Server 2.2 and Apache Tomcat 6”
Where should I put my index.jsp on application server.
index.jsp is still part of your Application WAR file. It is a practice to put the index.jsp file at root of the application directory (sibling of WEB-INF directory).