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:
- mod_proxy_ajp
- mod_proxy
- mod_proxy_cluster
- mod_manager
- mod_advertise
- 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.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
Configuration for mod_cluster and load balancer
# MOD_CLUSTER configuration details
<IfModule manager_module>
Listen *:9999
ManagerBalancerName paascluster
<VirtualHost *:9999>
<Directory />
Order deny,allow
Deny from none
Allow from all
</Directory>
KeepAliveTimeout 300
MaxKeepAliveRequests 0
ServerAdvertise on
http://localhost:9999
AdvertiseFrequency 5
<Location /mod_cluster_paas>
SetHandler mod_cluster-manager
Order deny,allow
Deny from none
Allow from all
</Location>
</VirtualHost>
</IfModule>
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:
- Open a Web browser.
- 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 jboss Jar download URL : http://www.jboss.org/mod_cluster/downloads/1-1-3
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:
- Add the below listener entry on top of the server tag in Apache Application Server’s server.xmlfile.
<Listener className="org.jboss.modcluster.catalina.ModClusterListener" advertise="true" proxyList="localhost:9999"/>
- Enable AJP connector tag in the server.xmlfile.
<Connector protocol="AJP/1.3" port="8669" redirectPort="8443"/>
- Add the
jvmRoute
name in theengine
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.<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">
- 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. Themembership
tag is used for setting the multicast IP for different groups of clusters based on multicast IP address.<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/>
<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"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="5000" selectorTimeout="100" maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
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=/examples11 udt.setPath("/examples11"); udt.execute(); : : : : :
Recent Comments