How to Cluster in Apache Tomcat (Linux)
See Apache: Tips and Tricks for similar articles.
In a production Tomcat environment, you might have hundreds of web requests per second on a routine basis. With so much volume assaulting your Tomcat server, you'll probably discover that memory resources in one Tomcat JVM (Java Virtual Machine) can become overwhelmed. To help prevent this situation from becoming reality you can place two or more Tomcat servers in a cluster. A cluster is a group of servers that communicate with one another. Now you have multiple JVMs to handle the work load coming in from the network. Session data can be shared among the cluster members to achieve high availability, i.e., recovery of a web session should a server crash occur. In this topic, I will walk you through the steps to turn on clustering in a Linux environment and to start the first member of the cluster. I will use Ubuntu as the Linux distribution.
To learn how to cluster in Apache Tomcat in Linux, follow these 15 steps:
- Stop the Tomcat server.
- Create a directory under the
/etcdirectory of your file system drive namedtomcat7A. - Copy the contents of
CATALINA_BASE/conf, e.g.,/var/lib/tomcat7/confto your new directory. NOTE:confis a symbolic link to/etc/tomcat7. - Open an edit session on
/etc/tomcat7A/server.xml. Locate the cluster entry:Note that the entry is commented out.<!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> - Remove the comment tags:
Note that the entry is commented out.
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> - Now locate the
Connectorelement for HTTP:<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> - Change the port number to "8181":
We want to change the HTTP port so that there will be no contention for the port when we start our customized Tomcat.
<Connector port="8181" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> - Locate the
Serverelement:<Server port="8005" shutdown="SHUTDOWN"> - Change the port number to "8001":
We also want to change the shutdown port so that there will be no contention for the port.
<Server port="8001" shutdown="SHUTDOWN"> - Save your changes.
- Start a terminal window and navigate to
CATALINA_HOME/bine.g.,/usr/share/tomcat7/bin. - You will need to unlink the current symbolic link:
sudo unlink /var/lib/tomcat7/conf - Next, you will create a symbolic link to your customized configuration:
Now when we start the server, Tomcat will pick up our new configuration, including the updated
sudo ln -s /etc/tomcat7A /var/lib/tomcat7/confserver.xml. - Start Tomcat:
sudo ./startup.sh - In your browser, enter the URL that includes the new HTTP port number,
http://localhost:8181:
The welcome page will display, indicating the new configuration that includes
clustering has been installed by Tomcat.
