How to Cluster in Apache Tomcat (Linux)

See Apache: Tips and Tricks for similar articles.
Looking to improve your Apache Web Server skills? Check out Webucator’s Apache Web Server classes.

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:

  1. Stop the Tomcat server.
  2. Create a directory under the /etc directory of your file system drive named tomcat7A.
  3. Copy the contents of CATALINA_BASE/conf, e.g., /var/lib/tomcat7/conf to your new directory. NOTE: conf is a symbolic link to /etc/tomcat7.
  4. Open an edit session on /etc/tomcat7A/server.xml. Locate the cluster entry:
    <!--
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    -->
    	
    Note that the entry is commented out.
  5. Remove the comment tags:
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
           
    	
    Note that the entry is commented out.
  6. Now locate the Connector element for HTTP:
    <Connector port="8080" protocol="HTTP/1.1"	
    	connectionTimeout="20000"
    	redirectPort="8443" />
    	
  7. Change the port number to "8181":
    <Connector port="8181" protocol="HTTP/1.1"
    	connectionTimeout="20000"
    	redirectPort="8443" />
    	
    We want to change the HTTP port so that there will be no contention for the port when we start our customized Tomcat.
  8. Locate the Server element:
    <Server port="8005" shutdown="SHUTDOWN">
    	
  9. Change the port number to "8001":
    <Server port="8001" shutdown="SHUTDOWN">
    	
    We also want to change the shutdown port so that there will be no contention for the port.
  10. Save your changes.
  11. Start a terminal window and navigate to CATALINA_HOME/bin e.g., /usr/share/tomcat7/bin.
  12. You will need to unlink the current symbolic link:
    sudo unlink /var/lib/tomcat7/conf
    	
  13. Next, you will create a symbolic link to your customized configuration:
    sudo ln -s /etc/tomcat7A /var/lib/tomcat7/conf
    	
    Now when we start the server, Tomcat will pick up our new configuration, including the updated server.xml.
  14. Start Tomcat:
    sudo ./startup.sh
    	
  15. In your browser, enter the URL that includes the new HTTP port number, http://localhost:8181: Tomcat Welcome PageThe welcome page will display, indicating the new configuration that includes clustering has been installed by Tomcat.

Related Articles

  1. How to Start and Stop Apache Tomcat from the Command Line (Windows)
  2. How to Deploy a WAR File to Apache Tomcat (Windows)
  3. How to Start and Stop Apache Tomcat from the Command Line (Linux)
  4. How to Deploy a WAR File to Apache Tomcat (Linux)
  5. How to Run a JSP Program in Apache Tomcat (Windows)
  6. How to Check the Status of the Apache Tomcat Server (Windows)
  7. How to Deploy a Web Application Using the Apache Tomcat Manager (Windows)
  8. How to Check the Status of the Apache Tomcat Server (Linux)
  9. How to Set Default Context Path in Apache Tomcat (Windows)
  10. How to Use the autoDeploy Attribute in Apache Tomcat (Windows)
  11. How to Deploy a Web Application Using the Apache Tomcat Manager (Linux)
  12. How to List Deployed Applications Using the Apache Tomcat Manager (Windows)
  13. How to Run Multiple Instances of Apache Tomcat on One Server (Windows)
  14. How to Run a JSP Program in Apache Tomcat (Linux)
  15. How to Establish a JDBC Connection in Apache Tomcat (Windows)
  16. How to Verify Apache Tomcat Server Operation (Windows)
  17. How to Cluster in Apache Tomcat (Linux) (this article)
  18. How to Undeploy Web Applications Using the Apache Tomcat Manager (Windows)
  19. How to Set Default Context Path in Apache Tomcat (Linux)
  20. How to Use the Java Logging API in Apache Tomcat (Windows)
  21. How to Cluster in Apache Tomcat (Windows)
  22. How to Use the Java Logging API in Apache Tomcat (Linux)
  23. How to Use the autoDeploy Attribute in Apache Tomcat (Linux)
  24. How to Configure Apache Tomcat to Use MBeans (Windows)
  25. How to List Deployed Applications Using the Apache Tomcat Manager (Linux)
  26. How to Install and Configure Apache Tomcat (Windows)
  27. How to Configure Apache Tomcat to Use MBeans (Linux)