Session Clustering with Terracotta
- Before you start, make sure you have installed terracotta 2.4. We'll refer to the installation directory as
- If you haven't already got a boot jar for terracotta for your jvm version, then make one now. On linux this is
- Check out jetty from svn, or download an unzip a src bundle. We'll refer to the installation directory as
- Build it if it isn't already (
- Go to the
$JETTY_HOME/contrib/terracottadirectory and build it with
- Copy the target/terracotta-sessions.jar you just built into
Configuring Jetty to use Terracotta consists of creating a single TerracottaSessionIdManager per jetty instance to generate unqiue session ids, and then setting up a special TerracottaSessionManager per webapp to be distributed.
We've provided sample config files to do both of these things, so if you don't want to read further, you can just use them. Here's how to start jetty with terracotta and deploy the sample webapps that come with the terracotta distribution:
- build, then copy the DepartmentTaskList.war, Cart.war and Townsend.war files from
- copy these jetty context deployer config files which match the sample webapps into $JETTY-HOME/contexts from
- skip down to the #Starting section to run jetty
Otherwise, let's continue with looking into the configuration files.
One TerracottaSessionIdManager is configured per jetty instance to generate unique session ids. These are the relevant lines from
workerName is a unqiue name for the jetty instance. In the example it is "fred" but you can use any naming scheme you'd like. We set the
tcIdMgr object as a Server attribute so that we can reference it in other config files.
Each webapp whose sessions we want clustered must use a TerracottaSessionManager instead of the default HashSessionManager.
The easiest way to do this is to create individual context deployer config files for each webapp, and include these lines:
These lines ensure that a TerracottaSessionManager is established for each webapp, and has access to the jetty instance's unique session id generator we configured above.
- Start a terracotta server with a jetty configuration file. On linux this is
$TC_HOME/bin/start-tc-server.sh -f $JETTY-HOME/contrib/terracotta/src/main/resources/tc-config-jetty.xml
- Set up the webapps that you want to use terracotta by following the instructions in the Configuration section above.
- Start jetty:
You'll now have a jetty instance that is talking to terracotta to distribute sessions from your webapps. Rinse and repeat to create other jetty instances in the cluser. Don't forget to select a unique
workerName for the other TerracottaSessionIdManagers. Also, if you just want to play around and run 2 jetty instances on the same machine, don't forget that you'll need to use port numbers for each!