TomCat Load Balancing-Windows

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The key takeaways are that a load balancer distributes requests across multiple servers to optimize performance and ensure high availability. Apache can be used as a load balancer to distribute requests between multiple Tomcat instances.

A load balancer is a virtual server that distributes requests to different nodes or servers in a cluster to optimize performance. If one server is unable to handle a request, another server can handle it transparently.

The components needed are Apache HTTP Server, Tomcat, mod_jk Tomcat connector, and a workers.properties file to define Tomcat workers.

LOAD BALANCING WITH APACHE

AND TWO INSTANCES OF TOMCAT


ON WINDOWS

By
Sam Mohan (wfj)
Tomcat is a popular application server used to host web applications. Apache
is a popular web server which provides services like https encryption and decryption,
URL rewriting etc. Apache can also be used a load balancer to balance load between
several Tomcat application servers. This article briefly discusses the procedure
followed for load balancing an application server, in the context of a web application.

Please note: The name ‘APACHE_INSTALL’ refers to the httpd install directory.

1. What is a Load-Balancer?
A Load balancer is a virtual worker that does not really communicate with
Tomcat workers; instead it is responsible for the management of several "real"
workers. For example, we can have a web server such as Apache forwarding servlet
requests to a Tomcat process (the worker) running behind it.
Simply, to balance server load, the system distributes requests to different nodes
within the server cluster, with the goal of optimizing system performance. If one
server cannot handle a request, can other servers in the cluster handle it? In a highly
available system, if a single Web server fails, then another server takes over, as
transparently as possible, to process the request. The user requests are processed by
more than one server with all servers sharing the load ‘equally’.

Fig: 1
Load balancing algorithms

• Round robin – requests are Reduced likelihood of version conflicts


• Weighted round robin – servers of different capacity are assigned requests in
proportion to their capacity (as defined by a load factor). Apache as 2 versions of
this:
o Request counting algorithm – requests are delegated in round robin
manner irrespective of the nature of the request.
o Weighted traffic counting algorithm – Apache delegates traffic to real
worker based on the number of bytes in the request.

What do you need for the set up (Round Robin)?

1. Apache HTTP server 2.2.11 from The Apache HTTP Server Project. I prefer
the zip over the Windows Installer. You are free to choose the Windows Installer if
you wish, but modify the service instructions below accordingly.
2. Apache Tomcat 5.5.20 from Apache Tomcat downloads
3. Mod JK Tomcat connector from here. Please note: You need to download the
binary- click on JK 1.2 Binary Releases --> win32 --> jk-1.2.28--> mod_jk-apache-
2.2.3.so, and rename it to mod_jk.so.
Apache configuration:

1. Edit the main Apache config file, APACHE_INSTALL/conf/httpd.conf and add the
following immediately after the other LoadModule directives:
• LoadModule jk_module modules/mod_jk.so

Fig: 2

2. You must inform mod_jk the location of your workers.properties file. Use mod_jk's
JkWorkersFile configuration directive. Similarly, you should specify a location where
mod_jk is going to place its log file and a log level to be used. Use the JkLogFile and
JkLogLevel configuration directives.
Add the following just before the end of the APACHE_INSTALL/conf/httpd.conf
file, at the end.

JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
Fig: 3
Tomcat worker is a Tomcat instance that is waiting to execute servlets on behalf
of some web server. For example, we can have a web server such as Apache
forwarding servlet requests to a Tomcat process (the worker) running behind it.
Defining workers to the Tomcat web server plug-in can be done using a properties
file, called worker.properties.
Find more details of Tomcat worker.properties please refer
http://tomcat.apache.org/tomcat-3.3-doc/Tomcat-Workers-HowTo.html.

3. Uncomment the “#ServerName localhost:80” to “ServerName localhost:80” in


APACHE_INSTALL/conf/httpd.conf file. Keep Listen Port the same.

Fig: 4

4. Copy the Tomcat installation folder from TOMCAT_HOME installation directory


and paste it to the same TOMCAT_HOME. Please find the screenshot below for
details. The home path is kept same for easiness, but you have two different path
directories for CATALINA_HOME, i.e., “D:\ApplicationServer\TomCat\5-5-
20\apache-tomcat-5.5.20” and “D:\ApplicationServer\TomCat\5-5-20\apache-tomcat-
5.5.20-1” (in this example).

Fig: 5
5. Copy the Web application (for eg: ematrix) into TOMCAT_HOME\webapps folder
for both apache-tomcat-5.5.20 and apache-tomcat-5.5.20-1, as well as in
APACHE_INSTALL\htdocs

Fig: 6 Web Application folder copied in APACHE_INSTALL\htdocs


6. Create a file “workers.properties” in APACHE_INSTALL\conf folder, and add the
following.
Workers.tomcat_home=D:/ApplicationServer/TomCat/5-5-20/apache-tomcat-
5.5.20
workers.java_home=E:/JAVA/jdk1.6.0
#worker.list=worker1, worker2
worker.list=loadbalancer

worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1

worker.tomcat2.port=8019
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
# Define the LB worker
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1, tomcat2
worker.loadbalancer.method=Busyness
worker.loadbalancer.sticky_sessions=true

Here, worker1 and worker2 represents two tomcat instances working on


AJP. Balance_workers is a comma separated list of workers that the load
balancer need to manage. These workers should not appear in the worker.list
property. This directive can be used multiple times for the same load balancer.
Sticky_session specifies whether requests with SESSION ID’s should be routed
back to the same Tomcat worker. Set sticky_session to False when Tomcat is using a
Session Manager which can persist session data across multiple instances of Tomcat.
By default sticky_session is set to True.
Now, we have a round robin load balancer with two Tomcats.
7. Modify server.xml file for Server Port, HTTP Connector Port and AJP Connector
Port on \apache-tomcat-5.5.20\conf folder.

Fig: 7

Fig: 8

Fig: 9

8. Modify server.xml file for Server Port, HTTP Connector Port and AJP Connector
Port on \apache-tomcat-5.5.20-1\conf folder.

Fig: 10

Fig: 11

Fig: 12

With the HTTP connector you can access the installed web applications directly.
8. Edit the server.xml for Tomcat and Tomcat's SecondInstance and add a jvmRoute
attribute to the Engine element:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
for the first instance and
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
for the second.
9. Open the APACHE_INSTALL/conf/httpd.conf file and comment out the previous
JkMount directives. Replace them with these:

JkMount /confluence loadbalancer


JkMount /confluence/* loadbalancer

<VirtualHost localhost:80>
#App server Load Balance
JkMount /*.jsp loadbalancer
JkMount /*.inc loadbalancer
JkMount /*.js loadbalancer
JkMount /*.properties loadbalancer
JkMount /*/*.inc loadbalancer
JkMount /*/*.jsp loadbalancer
JkMount /*/*.js loadbalancer
JkMount /*/*.properties loadbalancer
JkMount /*/*/.jsp loadbalancer
JkMount /*/*/.inc loadbalancer
JkMount /*/*/.js loadbalancer
JkMount /*/*/*.properties loadbalancer
JkMount /*/*/*/*.jsp loadbalancer
JkMount /*/*/*/*.inc loadbalancer
JkMount /*/*/*/*.js loadbalancer
JkMount /*/*/*/*.properties loadbalancer
JkMount /*/*/*/*/*.jsp loadbalancer
JkMount /*/*/*/*/*.inc loadbalancer
JkMount /*/*/*/*/*.js loadbalancer
JkMount /*/*/*/*/*.properties loadbalancer
JkMount /WebClient/*.* loadbalancer
JkMount /*/servlet/* loadbalancer
JkMount /*/com/* loadbalancer
JkMount /*/netscape/* loadbalancer
JkMount /workspace/* loadbalancer
JkMount /workspace/*/* loadbalancer
JkMount /services loadbalancer
JkMount /services/* loadbalancer
</VirtualHost>

Here, we've just pointed Apache to a single worker- the loadbalancer. The mod_jk's
JkMount directive is used to assign specific URLs to Tomcat.
10. Add "CATALINA_HOME = <base path>\apache-tomcat-5.5.20",
"CATALINA_HOME = <base path>\apache-tomcat-5.5.20-1", ... to "startup.bat",
"shutdown.bat", "catalina.bat", "digest.bat" and "service.bat". This is done since
Tomcat instances are running on the same server machine.

TO TEST THE SETUP:

1. Start the first tomcat instance. And check if it runs independently:


http://localhost: <tomcat1_port>/ematrix/emxLogin.jsp

2. Start the Apache from APACHE_INSTALL/bin/httpd.exe and check if the tomcat


runs with Apache:
http://localhost/ematrix/emxLogin.jsp

3. Start the Second tomcat and check if it runs independently.


http://localhost:<tomcat2_port>/ematrix/emxLogin.jsp

4. Stop first tomcat instance.

5. Check if Apache still runs with second tomcat up (Same url)


http://localhost/ematrix/emxLogin.jsp

References:
*********
1. Load Balancing Tomcat with Apache:
http://www.theserverside.com/tt/knowledgecenter/knowledgecenter.tss?
l=LoadBalancingTomcatApache

2. Tomcat with Apache Load Balancing:


http://confluence.atlassian.com/display/DOC/Apache+and+Tomcat+load+balancing

3. How to load balance Tomcat 5.5 with Apache on Windows-


http://thought-bytes.blogspot.com/2007/03/how-to-load-balance-tomcat-55-with.html

4. Clustering and Load Balancing in Tomcat 5, Part 1


http://onjava.com/pub/a/onjava/2004/03/31/clustering.html

5. Load Balancing Web Applications


http://onjava.com/pub/a/onjava/2001/09/26/load.html

6. LoadBalancer HowTo- Tomcat documentation on Load Balancing.


http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html

7. Professional Apache Tomcat 5- Chapter 12


Authors: Vivek Chopra, Amit Bakore, Jon Eaves, Ben Galbraith, Chanoch Wiggers,
Sing Li
************************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy