Вы находитесь на странице: 1из 11

10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

News Knowledge Base Deals About      è Sea

ANDROID CORE JAVA DESKTOP JAVA ENTERPRISE JAVA JAVA BASICS JVM LANGUAGES SOFTWARE DEVELOPMENT DEVOPS

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

ABOUT 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


 Posted by: Jesus Boadas  in Tomcat  November 30th, 2016  5 Comments  2524 Views

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.

Table Of Contents
1. The tools
2. Introduction
3. Prerequisites
4. Download Tomcat
5. Download Apache httpd server
NEWSLETTER
6. Download mod_jk
7. Tomcat Installation
7.1 Uncompress Apache Tomcat 165,000 insiders are alre
7.2 Install the Tomcat service weekly updates and complime
7.3 Start the Tomcat service whitepapers!
7.4 Check that tomcat is running Join them now to ga
8. Apache httpd Installation access to the latest news in
8.1 Uncompress Apache httpd server as well as insights about Andr
8.2 Edit the server root Groovy and other related tech
8.3 Install the httpd service
8.4 Start the Tomcat service
Email address:
8.5 Check that tomcat is running Your email address
9. mod_jk installation
9.1 Add mod_jk to the httpd.conf
Receive Java & Developer
9.2 Configure mod_jk in the Apache httpd server your Area
9.3 Create the workers.properties file
10. Conclusion
Sign up
Want to master Tomcat Server ?
Subscribe to our newsletter and download the Apache Tomcat
Cookbook right now! JOIN US
With 1,
In order to get you prepared for your Tomcat development needs, we have compiled unique v
numerous recipes to help you kick-start your projects. Besides reading them online you may 500 au
download the eBook in PDF format! placed a
related s
Constan
lookout
encoura

So If yo
Download NOW!
https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 1/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019
unique and interesting content t
check out our JCG partners prog

be a guest writer for Java Cod
your writing skills!

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

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.

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 2/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

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

7. Tomcat Installation
7.1 Uncompress Apache Tomcat
Choose an installation directory and uncompress the Tomcat server in its own directory.

4 Uncompress Tomcat


7.2 Install the Tomcat service
https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 3/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019
Open the Windows terminal and go to the Tomcat Installation bin directory.

Tomcat installation directory
1 C:\Java\Apache Tomcat 8.0.15\bin>

Install the service with the following command:

Install Tomcat service


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

You should get an output similar to this:

install Tomcat output


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

7.3 Start the Tomcat service


Start the service with the following command:

Start tomcat output


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

You should get an output similar to the following:

console
01 SERVICE_NAME: Tomcat8
02 TYPE : 10 WIN32_OWN_PROCESS
03 STATUS : 2 START_PENDING
04 (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
05 WIN32_OUTPUT_CODE : 0 (0x0)
06 SERVICE_OUTPUT_CODE: 0 (0x0)
07 CHECK-POINT : 0x0
08 START-INDICATOR : 0x7d0
09 PID : 5552
10 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

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 4/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

8. Apache httpd Installation


8.1 Uncompress Apache httpd server
Choose a directory and uncompress the 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
1 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


1 C:\Java\Apache24\bin>

Install the service with the following command:

Install httpd server service


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

You should get an output similar to this:

install Tomcat output


1 Installing the 'Apache2.4' service
2 The 'Apache2.4' service is successfully installed.
3 Testing httpd.conf....
4 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


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

You should get an output similar to the following:

console
01 SERVICE_NAME: Apache2.4
02 TYPE : 10 WIN32_OWN_PROCESS
03 STATUS : 2 START_PENDING 
04 (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 5/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019
05 WIN32_OUTPUT_CODE : 0 (0x0)
06 SERVICE_OUTPUT_CODE: 0 (0x0) 
07 CHECK-POINT : 0x0
08 START-INDICATOR : 0x7d0
09 PID : 3268
10 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

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

9.1 Add mod_jk to the httpd.conf


Edit the
httpd.conf

file.

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 6/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

9 Edit httpd

Add the following line to load the connector.

Load mod_jk
1 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
1 JkWorkersFile C:\Java\Apache24\conf\workers.properties
2 JkShmFile C:\Java\Apache24\log\mod_jk.shm
3 JkLogFile C:\Java\Apache24\log\mod_jk.log
4 JkLogLevel error
5 JkMount /status status
6 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

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 7/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

10 Create workers.properties

Edit the
worker.properties file

and put the following lines:

Workers properties
01 worker.list=balancer,status
02
03 worker.tomcat1.type=ajp13
04 worker.tomcat1.port=8009
05 worker.tomcat1.host=localhost
06
07 worker.tomcat2.type=ajp13
08 worker.tomcat2.port=8010
09 worker.tomcat2.host=localhost
10
11 worker.tomcat3.type=ajp13
12 worker.tomcat3.port=8011
13 worker.tomcat3.host=localhost
14
15
16 worker.balancer.type=lb
17 worker.balancer.balance_workers=tomcat1,tomcat2,tomcat3
18
19 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.

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 8/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

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.

(No Ratings Yet)  5 Comments  2524 Views  Tweet it!

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 ....

Email address:
Your email address

Receive Java & Developer job alerts in your Area

Sign up

LIKE THIS ARTICLE? READ MORE FROM JAVA CODE GEEKS

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 9/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019

Complete End-to-End Spring Cloud Eureka JAX-WS Web Services Apache Tomcat Mod_jk
Testing - Setup, Write, Server Tutorial On Tomcat Tutorial
Run, & Record

Ad go.cypress.io javacodegeeks.com javacodegeeks.com javacodegeeks.com

Weblogic Server How to Install Jetty JDBC Connection Pool Apache ActiveMQ Load
Administration Tutorial Application Server Example Balancing Example

javacodegeeks.com javacodegeeks.com javacodegeeks.com javacodegeeks.com

5 Leave a Reply

Join the discussion...

3 2 0    5

 Subscribe   newest  oldest  most voted

Brian 

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?
Guest

 1   Reply  1 year ago 

Amit Arora 

Then you have update below entries hostname should be ip


Guest worker.tomcat1.host=IP Address
worker.tomcat2.host=IP Address2

 1  Reply  1 year ago

Biswajeet Rout 

Is there a way to maintain stickiness in LB?

Guest
 0   Reply  4 months ago

Tabitarh 

Great tutorial.
Just to find out.
Guest
If I have a folder created in the webapps directory to store some uploaded file in say tomcat instance 1, how will
the other tomcat instances access this folder.

 0   Reply  3 months ago 

Joel Patrick Llosa 


https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 10/11
10/10/2019 Apache Tomcat Load Balancing Tutorial | Examples Java Code Geeks - 2019
Member Don’t use the webapps directory. Store the uploaded file under the Apache httpd server. You’ll need to

configure httpd.conf or vhosts.conf to point to the directory of the uploaded files. See “JKMount”.

 0  Reply  2 months ago

KNOWLEDGE BASE HALL OF FAME ABOUT JAVA CODE GEEKS


JCGs (Java Code Geeks) is an independent online community focused o
Courses Android Alert Dialog Example
ultimate Java to Java developers resource center; targeted at the techn
technical team lead (senior developer), project manager and junior dev
Minibooks Android OnClickListener Example
JCGs serve the Java, SOA, Agile and Telecom communities with daily ne
domain experts, articles, tutorials, reviews, announcements, code snipp
News How to convert Character to String and a
source projects.
String to Character Array in Java
Resources
Java Inheritance example DISCLAIMER
Tutorials
Java write to File Example
All trademarks and registered trademarks appearing on Java Code Geek
THE CODE GEEKS NETWORK java.io.FileNotFoundException – How to property of their respective owners. Java is a trademark or registered tr
Oracle Corporation in the United States and other countries. Examples J
solve File Not Found Exception
is not connected to Oracle Corporation and is not sponsored by Oracle C
.NET Code Geeks java.lang.arrayindexoutofboundsexception
– How to handle Array Index Out Of
Java Code Geeks Bounds Exception

System Code Geeks java.lang.NoClassDefFoundError – How to


solve No Class Def Found Error
Web Code Geeks
JSON Example With Jersey + Jackson

Spring JdbcTemplate Example

Examples Java Code Geeks and all content copyright © 2010-2019, Exelixis Media P.C. | Terms of Use | Privacy Policy | Contact  

https://examples.javacodegeeks.com/enterprise-java/tomcat/apache-tomcat-load-balancing-tutorial/ 11/11

Вам также может понравиться