Home » Enterprise Java » Tomcat » Apache Tomcat Load Balancing Tutorial

About Jesus Boadas

Jesus Boadas
I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.

Apache Tomcat Load Balancing Tutorial

Load balancing improves the distribution of workloads across multiple Tomcat servers. Load balancing aims to optimize resource use, maximize throughput, minimize response time, and avoid overload of any single resource.

Want to master Tomcat Server?

Subscribe to our newsletter and download the Apache Tomcat Cookbook right now!

In order to get you prepared for your Tomcat development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

 

A Tomcat cluster consists of a set of connected Tomcat Servers that work together so that, in many respects, they can be viewed as a single system. Each node, sets to perform the same task, controlled and scheduled by load balancer.

1. The tools

  • Java JDK
  • Apache Tomcat
  • Apache httpd server
  • mod_jk connector

2. Introduction

In this example, we are going to set up your own Apache Tomcat clusters using Apache Httpd as a load balancer front end for the cluster.

3. Prerequisites

  • JDK installed

4. Download Tomcat

Go to the page https://tomcat.apache.org/download-80.cgi and download the tomcat server as a zip compressed file for windows.

1 Download Tomcat for Windows

1 Download Tomcat for Windows

5. Download Apache httpd server

Go to the page http://www.apachehaus.com/cgi-bin/download.plx and download the httpd server as a zip compressed file for windows.
The Apache foundation doesn’t provide binaries for the httpd server. We are going to use a binary distribution from the recommended sites in the Apache web site.

2 download httpd server

2 download httpd server

6. Download mod_jk

Go to the page https://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/ and download the mod_jk as a zip compressed file for windows.

3 download mod_jk conector

3 download mod_jk conector

7. Tomcat Installation

7.1 Uncompress Apache Tomcat

Choose an installation directory and uncompress the Tomcat server in its own directory.

4 Uncompress Tomcat

4 Uncompress Tomcat

7.2 Install the Tomcat service

Open the Windows terminal and go to the Tomcat Installation bin directory.

Tomcat installation directory

C:\Java\Apache Tomcat 8.0.15\bin>

Install the service with the following command:

Install Tomcat service

C:\Java\Apache Tomcat 8.0.15\bin>service install

You should get an output similar to this:

install Tomcat output

Installing the service 'Tomcat8' ...
Using CATALINA_HOME:    "C:\Java\Apache Tomcat 8.0.15"
Using CATALINA_BASE:    "C:\Java\Apache Tomcat 8.0.15"
Using JAVA_HOME:        "C:\Java\jdk1.8.0_40"
Using JRE_HOME:         "C:\Java\jre1.8.0_40"
Using JVM:              "C:\Java\jre1.8.0_40\bin\client\jvm.dll"
The service 'Tomcat8' has been installed.

7.3 Start the Tomcat service

Start the service with the following command:

Start tomcat output

C:\Java\Apache Tomcat 8.0.15\bin>sc start Tomcat8

You should get an output similar to the following:

console

SERVICE_NAME: Tomcat8
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE_OUTPUT_CODE: 0  (0x0)
        CHECK-POINT        : 0x0
        START-INDICATOR    : 0x7d0
        PID                : 5552
        MARKS              :

7.4 Check that tomcat is running

Open the browser in the URL: http://localhost:8080

And you should see the Tomcat Welcome screen.

5 Tomcat Welcome

5 Tomcat Welcome

8. Apache httpd Installation

8.1 Uncompress Apache httpd server

Choose a directory and uncompress the Apache httpd server.

6 Uncompress Apache httpd server

6 Uncompress Apache httpd server

8.2 Edit the server root

Before we run the Apache httpd server we need to tell Apache where is the server root. Open the httpd server configuration file and edit the SRVROOT parameter using the appropriate directory.

SRVROOT

Define SRVROOT "C:\Java\Apache24"

8.3 Install the httpd service

Open the Windows terminal and go to the httpd server Installation bin directory.

httpd server bin installation directory

C:\Java\Apache24\bin>

Install the service with the following command:

Install httpd server service

C:\Java\Apache24\bin>httpd -k install

You should get an output similar to this:

install Tomcat output

Installing the 'Apache2.4' service
The 'Apache2.4' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.

8.4 Start the Tomcat service

Start the service with the following command:

Start tomcat output

C:\Java\Apache24\bin>sc start Apache2.4

You should get an output similar to the following:

console

SERVICE_NAME: Apache2.4
        TYPE               : 10  WIN32_OWN_PROCESS
        STATUS             : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_OUTPUT_CODE  : 0  (0x0)
        SERVICE_OUTPUT_CODE: 0  (0x0)
        CHECK-POINT        : 0x0
        START-INDICATOR    : 0x7d0
        PID                : 3268
        MARKS              :

8.5 Check that tomcat is running

Open the browser in the URL: localhost and you should see the httpd server Welcome screen.

7 Apache httpd server welcome screen

7 Apache httpd server welcome screen

9. mod_jk installation

Extract the mod_jk compressed file and copy the file mod_jk.so into the httpd server modules directory.

8 Install mod_jk

8 Install mod_jk

9.1 Add mod_jk to the httpd.conf

Edit the httpd.conf file.

9 Edit httpd

9 Edit httpd

Add the following line to load the connector.

Load mod_jk

LoadModule jk_module modules/mod_jk.so

9.2 Configure mod_jk in the Apache httpd server

We are going to configure the mod_jk to work with our Apache httpd server. Add the following lines to the httpd.conf

Configure mod_jk

JkWorkersFile C:\Java\Apache24\conf\workers.properties
JkShmFile     C:\Java\Apache24\log\mod_jk.shm
JkLogFile     C:\Java\Apache24\log\mod_jk.log
JkLogLevel    error
JkMount  /status  status
JkMount  /*  balancer

JkWorkersFile C:\Java\Apache24\conf\workers.properties. In this file we define the Tomcat workers.
JkShmFile C:\Java\Apache24\log\mod_jk.shm Defines where is the jk_mod shared memory file.

JkLogFile C:\Java\Apache24\log\mod_jk.log Defines where are the mod_jk log file.
JkLogLevel error Sets the error level of the mod_jk.

JkMount /status status Maps a worker to the status worker.
JkMount /* balancer Mounts the Tomcat balancer

9.3 Create the workers.properties file

In the conf directory of the Apache httpd server, create a new text file with the name workers.properties

10 Create workers.properties

10 Create workers.properties

Edit the worker.properties file and put the following lines:

Workers properties

worker.list=balancer,status
 
worker.tomcat1.type=ajp13
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
 
worker.tomcat2.type=ajp13
worker.tomcat2.port=8010
worker.tomcat2.host=localhost
 
worker.tomcat3.type=ajp13
worker.tomcat3.port=8011
worker.tomcat3.host=localhost
 
 
worker.balancer.type=lb
worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3
 
worker.status.type=status

worker.list=balancer,stat From outside we have 2 visible workers: balancer and stat. All the request that comes to the balancer are managed by the balancer worker.
worker.tomcat1.type=ajp13 Defines a native connector to use with the Tomcat instance, is the same for each worker.

worker.tomcat1.host=localhost Sets the host of the worker, this is done for each worker.
worker.tomcat1.port=8009 Sets the port of each worker.

worker.balancer.type=lb Load Balancing Workers: Do not show workers of type “lb”
worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3 Adds the workers to the load balancer

worker.status.type=status The status worker will log changes made to the configuration with log level “info” to the usual JK log file. Invalid requests will be logged with log level “warn”.

At this point the Apache httpd server its working as a front end of the Tomcat load balancer.

11 Tomcat load balancing

11 Tomcat load balancing

10. Conclusion

When you access the Httpd server from the browser, each request is going to be distributed to the three tomcat instances. If one of the tomcat instances fail then load balancer stops to forward requests to that failed instance.

If that failed instance becomes active then the load balancer adds it to the cluster and forward requests again. The key here is that you make each node of the load balancer a different Tomcat server from a separate host.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

1 Comment on "Apache Tomcat Load Balancing Tutorial"

avatar
  Subscribe  
newest oldest most voted
Notify of
Brian
Guest

In this example, those multiple Tomcat instances are installed on the same server host. What is the difference for multiple Tomcat instances installed on different server hosts?