Image Blog An In Depth Look at Tomcats Clustering Mechanism part 1
January 29, 2020

Apache Tomcat 8 Overview: Clustering Tutorial & More

Tomcat
Java

Apache Tomcat is one of the most popular open source Java technologies used today. In this blog, we compare Tomcat 8 vs. Tomcat 7 and share tips for clustering with Apache Tomcat 8. Plus, you'll learn about a better way to get Apache Tomcat 8 support.

Back to top

What Is Apache Tomcat 8?

Apache Tomcat 8 is an EOL version of Tomcat released in 2014. A more recent version of Tomcat, Tomcat 10, is now available.

Apache Tomcat 8 is the 8th major release of Apache Tomcat, which is a set of open source specifications that include select specifications from the Jakarta EE platform. Tomcat 8 reached end of life on June 30, 2018. Currently maintained versions include Tomcat 8.5, 9, and the latest version, Tomcat 10.

Back to top

Apache Tomcat 8 vs. Tomcat 7

Tomcat 8 requires Java 7 or later, while you can run Tomcat 7 on Java 6. This is the biggest difference between Tomcat 7 and Tomcat 8.

Other differences include: 

  • Tomcat 8 can use Apache Portable Runtime, which provides better scalability and performance.
  • Tomcat 8 internal API is broadly compatible with Tomcat 7 — but they are not binary compatible.
  • There are some differences in web application resources.

If you're currently using Tomcat 7, it might be time to migrate to a new version. Tomcat 7 is no longer supported as of March 31, 2021.

Now Available: The Enterprise Guide to Apache Tomcat

Whitepaper Apache Tomcat Best Practices PDF Mockup 1

In our Enterprise Guide to Apache Tomcat, our experts provide best practices for enterprise Tomcat deployments, including best practices for performance, security, clustering, resilience, and more.

Grab Your Free Copy HERE

Back to top

Apache Tomcat 8 Tutorial For Clustering

Here's a brief tutorial for Apache Tomcat 8 clustering.

To limit the liability of your attempt at creating a cluster, you can set up a machine, virtual or physical, just for this task.

1. Get Server Configurations

You can run multiple Tomcat instances on a single virtual/physical machine by tweaking just a few settings, mainly port numbers so the instances don’t interfere with each other. The configuration of these Tomcat instances is well outside the scope of this document, although it is not difficult to accomplish. Settings for the connectors in your server configuration files can be found at the Apache Tomcat 8 website.

Below are two server configurations that you can use to run a simple cluster. Just start with two instances of Apache Tomcat 8. And replace the corresponding server.xml file with the .xml information provided below.

Server.xml 1

<?xml version=’1.0’ encoding=’utf-8’?>
<Server port=”50005” shutdown=”SHUTDOWN”>
 <Listener className=”org.apache.catalina.startup.VersionLoggerListener” />
 <Listener className=”org.apache.catalina.core.AprLifecycleListener” SSLEngine=”on” />
 <Listener className=”org.apache.catalina.core.JreMemoryLeakPreventionListener” />
 <Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />
 <Listener className=”org.apache.catalina.core.ThreadLocalLeakPreventionListener” />
 <GlobalNamingResources>
 <Resource name=”UserDatabase” auth=”Container”
 type=”org.apache.catalina.UserDatabase”
 description=”User database that can be updated and saved”
 factory=”org.apache.catalina.users.MemoryUserDatabaseFactory”
 pathname=”conf/tomcat-users.xml” />
 </GlobalNamingResources>
 <Service name=”Catalina”>
 <Connector port=”51111” protocol=”HTTP/1.1”
 connectionTimeout=”20000”
 redirectPort=”51113” />
 <Engine name=”Catalina” defaultHost=”localhost”>
 <Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”/>
 <Realm className=”org.apache.catalina.realm.LockOutRealm”>
 <Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
 resourceName=”UserDatabase”/>
 </Realm>
 <Host name=”localhost” appBase=”webapps”
 unpackWARs=”true” autoDeploy=”true”>
 <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
 prefix=”localhost_access_log” suffix=”.txt”
 pattern=”%h %l %u %t &quot;%r&quot; %s %b” />
 </Host>
 </Engine>
 </Service>
</Server>

Server.xml 2

<?xml version=’1.0’ encoding=’utf-8’?>
<Server port=”50006” shutdown=”SHUTDOWN”>
 <Listener className=”org.apache.catalina.startup.VersionLoggerListener” />
 <Listener className=”org.apache.catalina.core.AprLifecycleListener” SSLEngine=”on” />
 <Listener className=”org.apache.catalina.core.JreMemoryLeakPreventionListener” />
 <Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />
 <Listener className=”org.apache.catalina.core.ThreadLocalLeakPreventionListener” />
 <GlobalNamingResources>
 <Resource name=”UserDatabase” auth=”Container”
 type=”org.apache.catalina.UserDatabase”
 description=”User database that can be updated and saved”
 factory=”org.apache.catalina.users.MemoryUserDatabaseFactory”
 pathname=”conf/tomcat-users.xml” />
 </GlobalNamingResources>
 <Service name=”Catalina”>
 <Connector port=”51112” protocol=”HTTP/1.1”
 connectionTimeout=”20000”
 redirectPort=“51114 “ />
 <Engine name=”Catalina” defaultHost=”localhost”>
 <Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”/>
 <Realm className=”org.apache.catalina.realm.LockOutRealm”>
 <Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
 resourceName=”UserDatabase”/>
 </Realm>
 <Host name=”localhost” appBase=”webapps”
 unpackWARs=”true” autoDeploy=”true”>
 <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”
 prefix=”localhost_access_log” suffix=”.txt”
 pattern=”%h %l %u %t &quot;%r&quot; %s %b” />
 </Host>
 </Engine>
 </Service>
</Server>

2. Create Your Apache Tomcat 8 Cluster

Clustering is very simple to setup in Tomcat 8. However, if you wish to leverage clustering in your enterprise environment the default configuration is not going to be the best route for you. To turn on clustering in your Tomcat 8 server all you have to do is add one line of code to your server.xml.

<Cluster className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”/>

You have created a clustered instance, but you only have one instance, so it is not a very big cluster. Before we create the next instance we should install the application we want to test on this cluster.

3. Make Your Web Application Distributable

With your cluster running, placing a normal application on one server will not trigger propagation to other servers. The idea behind propagation is: an application is placed on one node in the cluster, it is migrated (copied) automatically to other nodes in the cluster. To achieve this we add the following code to the web.xml:

<distributable/>

 

This tells Tomcat that this application is designed to run on multiple nodes in this cluster.

4. Set Up Session Replication

The default session replication mode is “All to All,” meaning any session data created on a server will be duplicated to all other servers in the cluster. If your application creates session data for a user, and you have a heterogeneous cluster, the session data will still be replicated across the other nodes.

A heterogeneous configuration is one that does not have all of the same applications on every node. Therefore, if application A stores session data for a user, and application A is running on server A, but not server B, session data will replicate to server B, even though there is no use for it there.

5. Configure Multicast Setup

The cluster is discovered and maintained via multicast heartbeats. The server will be set up with a default multicast IP address of 228.0.0.4 and a multicast port of 45564. This means that any other nodes that are using the same multicast address and port will see this cluster/node. It is important to ensure your network supports multicast. This is commonly blocked for security reasons.

6. Configure the Manager Object

After creating the cluster object and making your web applications distributable, we need to move on to configuring other settings. The Manager object controls session replication.

<Manager
 className=”org.apache.catalina.ha.session.DeltaManager”.../>

The DeltaManager replicates all changed session data to all nodes of the cluster. The BackupManager backs up session data to a specific backup node. For large clusters the BackupManager is the option to go with, for smaller clusters it is common to just use the default DeltaManager.

In Tomcat 8, you can define a manager in the cluster configuration, as you could in earlier versions, but you can also define a manager in a web application’s context.

Defining the Manager in your clustering configuration provides a default setting for applications that do not provide their own Manager configuration. For instance, the following code will set all applications in your cluster to use the BackupManager for session replication.

<Manager
 className=”org.apache.catalina.ha.session.BackupManager”.../>

7. Set Channel Send Options

After setting the Manager, you might need to apply a non-default channel send options value. Channel send options is a setting specified on the cluster object. For example:

<Cluster
 className=”org.apache.catalina.ha.tcp.SimpleTcpCluster”
 channelSendOptions=”6”>

Channel send options controls how messages are sent between cluster nodes. Are these message sent synchronously, or, in layman’s terms, does the thread that sends the message have to wait until the message has sent before continuing to work, in turn, potentially making the users request wait on this message to be sent? Sending the messages asynchronously is when the thread generates and sends the message, but does not stop and wait for this to happen. It does this by spawning a worker thread.

As you can see, this is just one aspect of the channel send options, and it is a lot of information. To go over channel send options in detail will require a whole default channel send mode is asynchronous.

Back to top

Get Support For Your Tomcat Deployments

Apache Tomcat is a great open source web application server. But there can be security issues and it can be tricky to configure Tomcat properly and get maximum performance out of it.

OpenLogic has Tomcat experts who can assist you with configurations (including clustering), migrations, and so much more. Get in touch with an expert today to learn more.

Talk to a Tomcat Expert

 

This blog was originally published in 2015 and has been updated for accuracy and comprehensiveness.

Related Content

Back to top