26 Oct

Tomcat load balancing with Apache using mod proxy

Configuring Tomcat Load Balancing with Apache webserver (http server) using Mod Proxy is very simple. I have listed here the following steps on how to configure Apache with Tomcat to configure Load Balancer using Mod Proxy.

Having load-balanced is always recommended in production environment for better availability.

Prerequisites

JRE is installed and already configured

Please follow the below steps for configuring load balancing with Apache Tomcat and Apache http servers.

Step 1. Download Apache 2.4 http server from

https://httpd.apache.org/download.cgi#apache24

Step 2. Download Apache Tomcat Server version 8 from

https://tomcat.apache.org/download-80.cgi

Step 3. Install Apache http server, let’s say in D drive. Here I used xampp to install the Apache 2.4 http server. The root folder of the server is D:\xampp\apache

Step 4. Install two instances of Apache Tomcat server, let’s say in C drive. Here I have used version apache-tomcat-8.0.36. So I have put two instances of apache-tomcat-8.0.36 server – C:\apache-tomcat-8.0.36 and C:\ apache-tomcat-8.0.36-2

Step 5. Change the ports in Tomcat server in server.xml file. Here we will use default ports in C:\apache-tomcat-8.0.36 and change ports in C:\ apache-tomcat-8.0.36-2. Now open the file C:\apache-tomcat-8.0.36\conf\server.xml and update as following

Step 6. Add/Uncomment below lines into httpd.conf file in Apache http server

Step 7. Add below load balancing configuration into conf file httpd.conf in Apache http server. Add proxy pass along with balancer name for application context root, for example, here it is spring-rest. In this example, I have proxy path as spring and balancer name as mycluster. It is important to include stickysession as not having this option will distribute same request to multiple tomcat servers and you will have session expiry issues in application.

Reverse Proxy

Reverse proxy is to provide Internet users access to a server that is behind a firewall. Reverse proxy is helpful when you want to access your internal application from internet. The User does not know about your internal server, and looks to them that the response is coming from the proxy server itself.

Sticky Session

As we know load balancer send the requests to the back-end servers as a round robin manner. We will have to use the sticky session feature which enables the load balancer to stick a user’s session to a specific back-end server. This would make sure all requests coming from the user during the session will be sent to the same server.

As you can see in above configuration, I have added route in BalancerMember so route value can be added to jvmRoute parameter in server.xml of Tomcat.

Step 8. Now update the server.xml files in each instance of Tomcat server. Add jvmRoute parameter in server.xml of Tomcat. This must be added in Engine name tag.

C:\apache-tomcat-8.0.36

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

C:\apache-tomcat-8.0.36-2

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

Step 9. Upadte the Logformat in httpd.conf to see which balancer was selected appended with the request URIs in log file. Add following in LogFormat directive under <IfModule log_config_module/> in httpd.conf

%{BALANCER_WORKER_ROUTE}e

The complete line looks like below

Step 10. Now put the each instance of spring-rest.war files under C:\apache-tomcat-8.0.36\webapps and C:\apache-tomcat-8.0.36-2\webapps. The war can be generated from the application posted here http://www.jeejava.com/spring-rest-optional-pathvariable/

Step 11. Now start each instance of the Tomcat server, for example, by executing startup.bat file from the cmd prompt “C:\apache-tomcat-8.0.36-2\bin>startup.bat”

Step 12. Now start the http server(here I have used to install xampp, so I can start using xampp Control Panel) and access the URL http://localhost/balancer-member in browser. From the below figure you would be able to understand the specifications.

tomcat apache load balancing

Step 13. Now access application URL by balancer path http://localhost/spring/. You can see output in the browser – “Hello World”. When you access URL http://localhost/spring/hello/Soumitra, then you will see output – “Hi Soumitra, Good Morning!”

Verification

Check access log of apache server to ensure your request is getting routed to only one tomcat instance. You will also notice your route id is appended as shown in below example.

That’s all. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Share