Session Clustering with Terracotta
- Before you start, make sure you have installed release terracotta 2.4 or greater. 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:
- Download and unzip a src bundle (6.1.4 onwards) or check out trunk or any tag from
jetty-6.1.4onwards. Here's how to do the checkout: 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:
- 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:
- Set up the webapps that you want to use terracotta by following the instructions in the #Configuration section above.
- Run the <TC_HOME>/bin/dso-env.sh command to determine the appropriate JVM arguments to start jetty with. (The dso-env script will also create a Terracotta boot jar for you if you haven't already created one for your platform as described earlier.)
- Copy the output of the
dso-envscript and use it to 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!
A simple way to test if Terracotta session clustering is working is to restart the jetty server and verify that your session data is intact. You can also use the Terracotta console (<TC_HOME>/bin/admin.sh) to inspect the live session data.