Implement a Dynamic Tomcat Cluster with Mod_Cluster

Apache Tomcat, the open source Web application server, hosts an estimated 70 percent of small Java Web applications. However Tomcat can become overburdened when faced with spikes in user requests. One way to ensure that your Tomcat servers are utilized to their maximum capacity is to connect and cluster them in a single environment and then load balance the servers in that environment. The mod_cluster module enables you to do just that: connect multiple servers and perform load balancing. Using mod_cluster, you can use a single IP address to connect multiple deployed Web servers.

Load balancing is basically defined as connectivity with one or more Web application servers for handling increasing user loads. Apart from checking the connected server load, load balancing also helps to route user requests to free servers, thereby lessening the burden on loaded servers. The two supported cluster modes are single-cluster mode and dynamic-cluster mode, with single-cluster mode being the default. In single-cluster mode, all server machines are used even when very few users access the Web application, which is a waste of network, power and server resources. If the number of users increases, then the response of the application will go down, because fewer servers are used to serve the request.

By setting up a dynamic Tomcat cluster environment where you can add or remove servers dynamically based on the user load, you can implement a sort of Tomcat platform as a service (PaaS) that ably addresses the problem of resource utilization. The following figure illustrates the Tomcat PaaS architecture.

This solution will improve resource utilization while load balancing the environment. In this tutorial, we explain how to implement a dynamic Tomcat cluster with mod_cluster.

The Case for Mod_cluster

Mod_cluster is an httpd-based load balancer that uses a communication channel to forward requests from httpd to a set of application server nodes. Unlike mod_jk and mod_proxy, mod_cluster utilizes an additional connection between the application server nodes and httpd. The application server nodes use this connection to send server-side load balance factors and lifecycle events back to httpd via the Mod-Cluster Management Protocol (MCMP), a custom set of HTTP methods. With this additional feedback channel, mod_cluster can provide more intelligence and granularity than many other load-balancing alternatives, as well as the following advantages.

  • Dynamic configuration of httpd workers
  • Server-side load balance factor calculation
  • Fine-grained Web app lifecycle control
  • AJP is optional

Mod_Cluster Configuration in Apache Web Server

Add the following modules in your Apache Web Server to enable clustering and load balancing:

  1. mod_proxy_ajp
  2. mod_proxy
  3. mod_proxy_cluster
  4. mod_manager
  5. mod_advertise
  6. mod_slotmem

The Apache Web Server has an httpd.conf file. This file contains loading module, cluster, load balancer, host, and log format configuration details. The configuration data below needs to be added for enabling clustering and load balancing in the Apache Web Server. Also below is the required module for enabling clustering and load balancing.

Configuration data for load module

LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_ajp_module modules/mod_proxy_ajp.soLoadModule slotmem_module modules/mod_slotmem.soLoadModule manager_module modules/mod_manager.soLoadModule proxy_cluster_module modules/mod_proxy_cluster.soLoadModule advertise_module modules/

Configuration for mod_cluster and load balancer

# MOD_CLUSTER configuration detailsListen *:9999ManagerBalancerName paascluster : : : : :Order deny,allow : : : : :Deny from none : : : : :Allow from allKeepAliveTimeout 300MaxKeepAliveRequests 0ServerAdvertise on http://localhost:9999AdvertiseFrequency 5SetHandler mod_cluster-managerOrder deny,allowDeny from noneAllow from all 

After adding the above configuration in the httpd.conf file, you have to restart Apache Web Server. To verify the clustering and load balancing, follow these steps:

  1. Open a Web browser.
  2. Type this address: http://localhost:9999/mod_cluster_paas

If the details below appear in the browser, then the cluster and load balancer are enabled.

Dynamic Clustering in Tomcat

To enable clustering in Tomcat, add the following JAR in the lib folder:

Jar name: Mod_cluster.1.1.3.jar : This jar is provided by jbossJar download URL :

Using the above JAR connects Apache Web Server with Tomcat Application Server via AJP.

Below is the Apache Application Server configuration for clustering and load balancing with Apache Web Server:

  1. Add the below listener entry on top of the server tag in Apache Application Server’s server.xml file.
  2. Enable AJP connector tag in the server.xml file.
  3. Add the jvmRoute name in the engine tag in server.xml. This name is useful for identifying the system in the cluster and gives a different name to each Tomcat application server.
  4. Add cluster and channel details to the engine tag in the server.xml file. This configuration is used for enabling different groups of clusters in Apache Web Server. The membership tag is used for setting the multicast IP for different groups of clusters based on multicast IP address.

Dynamic Clustering in Apache Application Server

To enable dynamic clustering in Apache Application Server, JMS is required. Java Messaging System (JMS) communicates with the server through messaging and is used for configuring server properties dynamically without restarting the server system. The following code snippet gives the option for enabling clustering with Apache Web Server:

Registry registry =Registry.getRegistry();MBeanServer mBeanServer = Registry.getRegistry().getServer()String beans[]=registry.findManagedBeans();ManagedBean mbeanProxy = registry.findManagedBean("org.jboss.modcluster.ModClusterListener");ClassNameMBean mbean= new ClassNameMBean();mbean.setManagedBean(mbeanProxy);OperationInfo info[] = mbeanProxy.getOperations() ;for(int i=0;i-1){  OperationInfo opinfo=info[i];  ParameterInfo param[]=opinfo.getSignature();  Object params[] = {"localhost",9999};  String sig[] = {"java.lang.String","int"};  mbeanProxy.getInvoke("addProxy",params,sig,mbean,null);   }}

Dynamically Deploying/Undeploying Apps in Tomcat

The following code snippet is for deploying Web applications in Tomcat:

DeployTask dt = new DeployTask();dt.setUrl("http://localhost:8080/manager");dt.setUsername("admin");dt.setPassword("admin");dt.setPath("/examples11");dt.setWar("d:/cas-cert/examples1/examples11.war");dt.execute();

The following code snippet is for undeploying Web applications in Tomcat.

UndeployTask udt = new UndeployTask();udt.setUrl("http://localhost:8080/manager");udt.setUsername("admin");udt.setPassword("admin");//http://localhost:8080/manager/undeploy?path=/examples11udt.setPath("/examples11");udt.execute(); : : : : :
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.