{"id":1229,"date":"2012-08-30T15:40:49","date_gmt":"2012-08-30T07:40:49","guid":{"rendered":"http:\/\/rmohan.com\/?p=1229"},"modified":"2012-08-30T15:48:33","modified_gmt":"2012-08-30T07:48:33","slug":"implement-a-dynamic-tomcat-cluster-with-mod_cluster","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1229","title":{"rendered":"Implement a Dynamic Tomcat Cluster with Mod_Cluster"},"content":{"rendered":"<p>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 <a href=\"http:\/\/www.jboss.org\/mod_cluster\" target=\"_blank\">mod_cluster<\/a> 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.<\/p>\n<p>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 <em>single-cluster mode<\/em> and <em>dynamic-cluster mode<\/em>, 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.<\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1230\" title=\"TomcatCluster_fig2\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2.png\" alt=\"\" width=\"742\" height=\"456\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2.png 742w, https:\/\/mohan.sg\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2-300x184.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2-150x92.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2012\/08\/TomcatCluster_fig2-400x245.png 400w\" sizes=\"(max-width: 742px) 100vw, 742px\" \/><\/a><\/p>\n<p>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.<\/p>\n<h2>The Case for Mod_cluster<\/h2>\n<p><em>Mod_cluster<\/em> is an httpd-based load balancer that uses a communication channel to forward requests from httpd to a set of application server nodes. Unlike <em>mod_jk<\/em> and <em>mod_proxy<\/em>, 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.<\/p>\n<ul>\n<li>Dynamic configuration of httpd workers<\/li>\n<li>Server-side load balance factor calculation<\/li>\n<li>Fine-grained Web app lifecycle control<\/li>\n<li>AJP is optional<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Mod_Cluster Configuration in Apache Web Server<\/h2>\n<p>Add the following modules in your Apache Web Server to enable clustering and load balancing:<\/p>\n<ol>\n<li><em>mod_proxy_ajp<\/em><\/li>\n<li><em>mod_proxy<\/em><\/li>\n<li><em>mod_proxy_cluster<\/em><\/li>\n<li><em>mod_manager<\/em><\/li>\n<li><em>mod_advertise<\/em><\/li>\n<li><em>mod_slotmem<\/em><\/li>\n<\/ol>\n<p>The Apache Web Server has an <em>httpd.conf<\/em> 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.<\/p>\n<p><strong>Configuration data for load module<\/strong><\/p>\n<pre><code>LoadModule proxy_module modules\/mod_proxy.so <br \/>LoadModule proxy_ajp_module modules\/mod_proxy_ajp.so <br \/>LoadModule slotmem_module modules\/mod_slotmem.so <br \/>LoadModule manager_module modules\/mod_manager.so <br \/>LoadModule proxy_cluster_module modules\/mod_proxy_cluster.so <br \/>LoadModule advertise_module modules\/mod_advertise.so<\/code><\/pre>\n<p><strong>Configuration for mod_cluster and load balancer<\/strong><\/p>\n<pre><code># MOD_CLUSTER configuration details <br \/>&lt;IfModule manager_module&gt; <br \/>Listen *:9999 <br \/>ManagerBalancerName paascluster <br \/>&lt;VirtualHost *:9999&gt; <br \/>&lt;Directory \/&gt; \u00a0<br \/> Order deny,allow \u00a0<br \/> Deny from none \u00a0<br \/>Allow from all <br \/>&lt;\/Directory&gt; <br \/>KeepAliveTimeout 300 <br \/>MaxKeepAliveRequests 0 <br \/>ServerAdvertise on <br \/>http:\/\/localhost:9999 <br \/>AdvertiseFrequency 5 <br \/>&lt;Location \/mod_cluster_paas&gt; <br \/>SetHandler mod_cluster-manager <br \/>Order deny,allow <br \/>Deny from none <br \/>Allow from all <br \/>&lt;\/Location&gt; <br \/>&lt;\/VirtualHost&gt; <br \/>&lt;\/IfModule&gt; <\/code><\/pre>\n<p>After adding the above configuration in the <em>httpd.conf<\/em> file, you have to restart Apache Web Server. To verify the clustering and load balancing, follow these steps:<\/p>\n<ol>\n<li>Open a Web browser.<\/li>\n<li>Type this address: <em>http:\/\/localhost:9999\/mod_cluster_paas<\/em><\/li>\n<\/ol>\n<p>If the details below appear in the browser, then the cluster and load balancer are enabled.<\/p>\n<p>&nbsp;<\/p>\n<h2>Dynamic Clustering in Tomcat<\/h2>\n<p>To enable clustering in Tomcat, add the following JAR in the <code>lib<\/code> folder:<\/p>\n<pre><code>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<\/code><\/pre>\n<p>Using the above JAR connects Apache Web Server with Tomcat Application Server via <a href=\"http:\/\/tomcat.apache.org\/connectors-doc\/ajp\/ajpv13a.html\" target=\"_blank\">AJP<\/a>.<\/p>\n<p>Below is the Apache Application Server configuration for clustering and load balancing with Apache Web Server:<\/p>\n<ol>\n<li>Add the below listener entry on top of the server tag in Apache Application Server&#8217;s <em>server.xml<\/em>file.\n<pre><code>&lt;Listener className=\"org.jboss.modcluster.catalina.ModClusterListener\" advertise=\"true\" proxyList=\"localhost:9999\"\/&gt;<\/code><\/pre>\n<\/li>\n<li>Enable AJP connector tag in the <em>server.xml<\/em>file.\n<pre><code>&lt;Connector protocol=\"AJP\/1.3\" port=\"8669\" redirectPort=\"8443\"\/&gt;<\/code><\/pre>\n<\/li>\n<li>Add the <code>jvmRoute<\/code> name in the <code>engine<\/code> tag in <em>server.xml<\/em>. This name is useful for identifying the system in the cluster and gives a different name to each Tomcat application server.\n<pre><code>&lt;Engine name=\"jboss.web\" defaultHost=\"localhost\" jvmRoute=\"node1\"&gt;<\/code><\/pre>\n<\/li>\n<li>Add cluster and channel details to the <code>engine<\/code> tag in the <em>server.xml<\/em> file. This configuration is used for enabling different groups of clusters in Apache Web Server. The <code>membership<\/code>tag is used for setting the multicast IP for different groups of clusters based on multicast IP address.\n<pre><code>&lt;Cluster className=\"org.apache.catalina.ha.tcp.SimpleTcpCluster\" channelSendOptions=\"6\"&gt;<br \/> &lt;Manager className=\"org.apache.catalina.ha.session.BackupManager\" expireSessionsOnShutdown=\"false\" notifyListenersOnReplication=\"true\" mapSendOptions=\"6\"\/&gt; <br \/> &lt;Channel className=\"org.apache.catalina.tribes.group.GroupChannel\"&gt; <br \/> &lt;Membership className=\"org.apache.catalina.tribes.membership.McastService\" address=\"228.0.0.4\" port=\"45564\" frequency=\"500\" dropTime=\"3000\"\/&gt; <br \/> &lt;Receiver className=\"org.apache.catalina.tribes.transport.nio.NioReceiver\" address=\"auto\" port=\"5000\" selectorTimeout=\"100\" maxThreads=\"6\"\/&gt; <br \/> &lt;Sender className=\"org.apache.catalina.tribes.transport.ReplicationTransmitter\"&gt; <br \/> &lt;Transport className=\"org.apache.catalina.tribes.transport.nio.PooledParallelSender\"\/&gt; <br \/> &lt;\/Sender&gt; <br \/> &lt;Interceptor className=\"org.apache.catalina.tribes.group.interceptors.TcpFailureDetector\"\/&gt; <br \/> &lt;Interceptor className=\"org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor\"\/&gt; <br \/> &lt;Interceptor className=\"org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor\"\/&gt; <br \/> &lt;\/Channel&gt; <br \/> &lt;Deployer className=\"org.apache.catalina.ha.deploy.FarmWarDeployer\" tempDir=\"\/tmp\/war-temp\/\" deployDir=\"\/tmp\/war-deploy\/\" watchDir=\"\/tmp\/war-listen\/\" watchEnabled=\"false\"\/&gt; <br \/> &lt;ClusterListener className=\"org.apache.catalina.ha.session.ClusterSessionListener\"\/&gt; <br \/> &lt;\/Cluster&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2>Dynamic Clustering in Apache Application Server<\/h2>\n<p>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:<\/p>\n<pre><code>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); } } <\/code><\/pre>\n<h2>Dynamically Deploying\/Undeploying Apps in Tomcat<\/h2>\n<p>The following code snippet is for deploying Web applications in Tomcat:<\/p>\n<pre><code>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(); <\/code><\/pre>\n<p>The following code snippet is for undeploying Web applications in Tomcat.<\/p>\n<pre><code>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();\u00a0:\u00a0:\u00a0:\u00a0:\u00a0: <\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1229"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1229"}],"version-history":[{"count":7,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1229\/revisions"}],"predecessor-version":[{"id":1233,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1229\/revisions\/1233"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}