1. Home
  2. m-Power Administration
  3. Clustering/Load Balancing Tomcat with Apache

Clustering/Load Balancing Tomcat with Apache

Overview

Clustering/Load Balancing Tomcat has many performance implications within a production environment. For instance, developers and m-Power administrators can safely restart individual Tomcats without bringing down an entire production instance, as well as memory and resources are better balanced. There are two main steps in setting up a load balancing Tomcat environment– configuring m-Power, and configuring Apache HTTP. This document will describe how to setup a load balancing and clustered m-Power environment on a Windows server.

Note that Tomcat clustering/load balancing is a custom configuration. If needing mrc’s assistance with the configuration, this may be done via a consulting engagement.

Load Balancing

A load balancing environment will automatically route traffic to the separate Tomcat servers to balance the workload. Additionally, if one Tomcat server goes down, the traffic is routed elsewhere. With a simple re-login the user is still able to run their applications as normal.

Starting with m-Power

For each Tomcat instance you have, navigate to /m-power/tomcat/conf/, and open the server.xml file in a text editor. Look at line 91 (or do a CTRL + F search for ‘Define an AJP’). This should show you the AJP connector port. This is the port number the clustered server will call. Make sure that nothing else is using this port, and feel free to change this port if necessary. You may want to write down these port numbers– you will need them to configure Apache.

Configuring Apache HTTP Server

Now you will need to download and install the Apache HTTP Server. Click here to navigate to the Apache website to download the installer. After you have installed Apache, make sure that the service can start. By default, Apache will attempt to use port 80, the same default m-Power port for production. You may need to change either Apache or your production Tomcat to use a different port.

Once you have verified that Apache can start, you will need to download and install Apache’s load balancing module. You can do so here. After downloading the zip file, extract the contents, and copy the mod_jk.so file into /Apache Group/Apache2/modules.

Next, open a text editor like notepad. You will need to create a workers.properties file, that will be saved in /Apache Group/Apache2/conf/. Click here to see an example. Note that you can use as many Tomcat instances as you like. For more information on how to customize this file, please see this page. Keep track of your naming system for each individual worker, as these names (i.e. worker1) will be used to connect back to your Tomcat installation.

Next, open \Apache Group\Apache2\conf\httpd.conf in a text editor. Add the following lines to the Dynamic Shared Object section of the configuration file:

LoadModule jk_module modules/mod_jk.so
JkWorkersFile "C:/Program Files (x86)/Apache Group/Apache2/conf/workers.properties"
JkLogFile logs/jk.log
JkLogLevel debug
JkMount /* balancer 

Make sure to customize the path to the workers file based on your specific installation of Apache.

Finishing with m-Power

The last step in setting up your clustered environment is to make each Tomcat aware of its worker number. To do so, open /m-power/tomcat/conf/server.xml in a text editor, and look for line 103 (or do a CTRL + F search for ‘<engine‘). Directly above this, you should see directions on how to support load balancing. Copy jvmRoute="worker1" into your uncommented engine tag on line 103, and be sure to customize your worker number based on how you configured your workers.properties file. You will need to repeat this step for each Tomcat instance you are utilizing.

You may want to point all of your Tomcat instances to work over a single set of application files. For instance, you could have multiple production installations, but direct all installations to use the same applications, so you don’t need to promote the applications to each individual installation.

To do so, navigate to the bottom of the server.xml file, and locate the <!– mrcjava Context –> section. Within this you should see the docBase path. This path states which application files to use for this Tomcat instance. You can change this to whichever path you desire.

Finally, be sure to restart all of your Tomcat instances, as well as Apache.

Clustering

A clustered Tomcat environment builds on the load balancing setup. The difference is that session data is passed between the different Tomcat servers. This allows the user to continue working seamlessly even if the Tomcat server they’re currently using goes down.

To set up a clustered environment, all steps from the above load balancing section must be performed first.

Once you are able to successfully run an application through your Apache port, you simply need to configure the Tomcat server.xml file (found inside /tomcat/conf). Uncomment the following entry:

<cluster classname="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

Change the line and add the following logic beneath this entry:

<Cluster className=" org.apache.catalina.ha.tcp.simpletcpcluster"="" channelsendoptions="8">
<channel classname="org.apache.catalina.tribes.group.GroupChannel">
<membership classname="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" droptime="3000"> <!-- 228.0.0.4 -->
<sender classname="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<transport classname="org.apache.catalina.tribes.transport.nio.PooledParallelSender">
</transport></sender>
<receiver classname="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autobind="100" selectortimeout="5000" maxthreads="6">
<interceptor classname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector">
<interceptor classname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor">
</interceptor></interceptor></receiver></membership></channel>
<valve classname="org.apache.catalina.ha.tcp.ReplicationValve" filter="">
<valve classname="org.apache.catalina.ha.session.JvmRouteBinderValve">
<clusterlistener classname="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener">
<clusterlistener classname="org.apache.catalina.ha.session.ClusterSessionListener">
</clusterlistener></clusterlistener></valve></valve>

Next, open the tomcat/conf/context.xml file. Find uncomment the line. Replace with the following:

<manager classname="org.apache.catalina.ha.session.DeltaManager" expiresessionsonshutdown="false" notifylistenersonreplication="true"> </manager>

Repeat the above steps for all Tomcat instances.

Finally, be sure to restart all of your Tomcat instances, as well as Apache.

More Assistance

If you have more questions about how to implement this feature, please see the sites below:

Otherwise, please feel free to contact mrc by emailing support@mrc-productivity.com.

Updated on June 22, 2023

Was this article helpful?

Related Articles