Konstantin Ryadov JVM engineer turned AI engineer. Things I have built.

Tomcat Clustering + Multicast + Firewall

How do you build a Tomcat 7 or 8 cluster, with an example, and make it work with the firewall that is on by default?

It's simple:

  1. Install, configure and start Tomcat (say, two instances)

1.1) Add a Cluster element under Host in server.xml.

  1. Create two pages in the Context you need:

2.1) index.jsp

<%= session.getAttribute("cluster_attr") %>

2.2) set.jsp

<% session.setAttribute("cluster_attr", "blablabla"); %>
  1. Set up a shared Context (if you didn't put the pages into the ROOT context)

  2. Start Tomcat:

tomcat/bin/startup.bat
  1. Open "/" on both in a browser: you get null

  2. Open /set.jsp on one of them

  3. Repeat step 5: you get "blablabla" on both

...

  1. Not working? Check the firewall + multicast settings

  2. Add a firewall rule and reload:

10.1) RHEL / CentOS 6

10.1.1) Add to /etc/sysconfig/iptables:

-A INPUT -m pkttype -j ACCEPT --pkt-type multicast

10.1.2) Run

# service iptables restart

10.2) RHEL / CentOS 7: run

# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -m pkttype --pkt-type multicast -j ACCEPT
success

# firewall-cmd --reload
success
  1. Check (step 5) that everything works without restarting Tomcat.

Originally published in Russian on LiveJournal.

All posts