YOU ARE HERE: Home > Tech > Internet > Article

Create Load Balance For Tomcat
By Jack Xu This article was rated:
 
Printer Version Printer Friendly | Add As Favorite | Link to Article

About the Author

Jack Xu has worked in IT for 12 years. He is a senior system architect for large distributed web portals. He has also developed Java based forums and search engines.

In this article, we provide steps of creating a cluster of Tomcat web application servers. We use tomcat 5.5.x that can be downloaded from http://jakarta.apache.org/tomcat/index.html.

1. Other software requirement

We use Apache web server to work with tomcat servers. Apache web server can be downloaded from http://www.apache.org

Besides, JK 1.2 library (mod_jk) is used and it can be downloaded from http://jakarta.apache.org/site/downloads/downloads_tomcat.html

For more information about how to install Apache and JK 1.2 library, please refer to my previous tutorials ?Install and Use Apache Web Server? and ?Use Apache with Web Application Server?.

In this article, we use two tomcat servers. In our machine, we created two tomcat installations.

2. Create Load Balance For Tomcat

In previous tutorial ?Use Apache with Web Application Server?, we have demonstrated that how Apache and Tomcat integrated.

We summarize the steps:

1. Create a workers.properties file to describe tomcat servers.

2. Add entries in Tomcat configuration file.

3. Add entries in Apache configuration (httpd.conf) file.

4. Start/restart Tomcat and Apache.


Now we will use the same steps to create load balance for tomcat server.

2.1 Create a workers.properties file

In the workers.properties file, we added a loadbalancer worker, and the worker.list now point to loadbalancer

List 1. workers.properties file


workers.tomcat_home=C:\jakarta-tomcat-5

workers.java_home=D:\jdk1.5.0

ps=\

worker.list=loadbalancer

worker.first.port=8007
worker.first.host=localhost
worker.first.type=ajp13
worker.first.lbfactor=1

worker.second.port=8009
worker.second.host=localhost
worker.second.type=ajp13
worker.second.lbfactor=1
worker.second.cachesize=5

worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=first, second

Explanation

worker.list=loadbalancer

Apache uses loadbalancer to handle JSPs. Then loadbalancer delegates this job to registered workers (first and second).

2.2 Add entries in Tomcat configuration file

In C:\ jakarta-tomcat-5.5.9\conf\server.xml, we change port ?8080? to ?8081?, change ?8443? to ?8463?, so the two Tomcat server would not have conflicts in ports.

Also we change ?8009? to ?8007?, so load balance work can send servlet request to this port.


<!-- Define an AJP 1.3 Connector on port 8007 -->
<Connector port="8007"
enableLookups="false" redirectPort="8463" protocol="AJP/1.3" />

and also

<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="first">

should replace the original Engine definition.


We only need change the other Tomcat settings at C:\ jakarta-tomcat-5.5.9\conf\server.xml the following line


<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="second">

should replace original Engine xml node.

2.3 Add entries in Apache configuration file

In httpd.conf file, we need add following lines:

List 2. Alias


Alias /jsp-examples/ "C:/jakarta-tomcat-5/webapps/jsp-examples/"

<Directory "C:/jakarta-tomcat-5/webapps/jsp-examples/">
Options +Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

We add alias definition in httpd.conf. This can ensure Apache server take care of the static files such as html and text files in C:/jakarta-tomcat-5/webapps/jsp-examples, where tomcat was installed.

Note that the load balance did not affect Apache server reading static contents, i.e., Apache web server reads html and text files.

We add alias definition in httpd.conf. This can ensure Apache server take care of the static files such as html and text files in C:/jakarta-tomcat-5/webapps/jsp-examples, where tomcat was installed.

List 3. Added to httpd.conf


<Location /*/WEB-INF/*>
AllowOverride None
Deny from all
</Location>

Above we don?t allow WEB-INF folder accessed from web server, because this folder has configuration files and other files that need to be hidden from web users.

List 4. Add to httpd.conf


# Load mod_jk
LoadModule jk_module "C:/Program Files/Apache Group/Apache2/modules/mod_jk-1.2.13-apache-2.0.54.so"

# Configure mod_jk
JkWorkersFile "C:\jakarta-tomcat-5\conf\workers.properties"
JkLogFile "C:\jakarta-tomcat-5\logs\mod_jk.log"
JkLogLevel info

JkMount /jsp-examples/*.jsp loadbalancer
JkMount /jsp-examples/servlet/* loadbalancer


List 4 has been added to the end of httpd.conf file. List 4 glues Apache with Tomcat using worker.properties file (in List 1).

JkMount /jsp-examples/*.jsp loadbalancer
JkMount /jsp-examples/servlet/* loadbalancer

Above we tell Apache use connector worker loadbalancer to handle JSP and java servlets.

Save httpd.conf in Apache.

Explanation

If you compare the changes in httpd.conf with what we did in ?Use Apache with Web Application Server? article, we can see JSP/servlets are directed to loadbalancer worker, instead of individual workers. And List 1 workers.properties has loadbalancer worker assigning the job of processing jsp/servlets to individual workers (Tomcat application servers).


2.4 Start/restart Tomcat and Apache

Now we start the two Tomcat servers, and the Apache Server,

Type: http://localhost/jsp-examples/

We see Apache presents this static page. Click a JSP link, the two Tomcats server would be assigned JSP page by loadbalancer that we have set up.

To test that Tomcat servers process JSP/servlets in turn. We wrote the following pages:

List 5. load.jsp saved to ?C:\jakarta-tomcat-5.5.9\webapps\jsp-examples?


<html>
<title>From Tomcat Server 1</title>
<font color=blue size=+1>Hello from Tomcat Server 1</font>
</html>

List 6. load.jsp saved to ?C:\jakarta-tomcat-5\webapps\jsp-examples?


<html>
<title>From Tomcat Server 2</title>
<font color=red size=+1>Hello from Tomcat Server 2</font>
</html>

We save List 5 as load.jsp to the folder ?C:\jakarta-tomcat-5.5.9\webapps\jsp-examples?.
And we save List 6 as load.jsp to the folder ?C:\jakarta-tomcat-5\webapps\jsp-examples?.

Note: for the purpose of demonstration, we use different load.jsp files in two Tomcat server. In true production of load balanced Tomcat servers, we should use exact files in load balancing directories.

If we open two IE 6 windows, each type: http://localhost/jsp-examples/load.jsp

We see two windows are different?the first window comes from Tomcat server 1, and the second comes from another Tomcat server.


Was this article helpful to you?yesno
2 of 2 people found this article helpful.



Related Publications
 
Bruce Eckel's Free Electronic Books Downloads (Mirror Links)
Create Load Balance For Tomcat
Study: Shoppers naive about retail prices online
'Tagging' helps unclutter data
Shopping.com Sees Fertile Ground in Europe
Yahoo Buying Photo-Sharing Service Flickr
Swap home, see world
Online Banking Growing Rapidly
Ask Jeeves Buys Blog Search Provider Bloglines
Some EBay Sellers Say Fee Cut Falls Short
China Internet Firms Seek New Sources of Revenue

(Registered users can post questions/comments)

 
 TLINKS SEARCH
Advanced Search
Help
 Recommended Links
Red Cross
Responding to hurricane katrina relieve. Donate today. It's a Great Feeling to Help.
http://www.redcross.org
Getusjobs.com
Getusjobs.com is the job site focused on American jobs. See the results that put us on top.
http://www.getusjobs.com
Database Tool
TLinkSoft® tools empowers developers, integrators and DBAs to be more productive.
http://www.cppunit.org/download.jsp
USAnalyst.com
USAnalyst.com provide a community for database analysts, business analysts, developer analysts and managers.
http://www.cppunit.org/article

Powered by Tlinks Systems