This is the first part of a series of posts on how to create a scalable Java
Application with Docker. TDD and Integration Tests helps us a lot to have some trust in our source
code. Amongst other things, Docker helps us to have more trust in our
applications as a whole, by running whole applications in a production-like
cluster setup on a single host. In this initial post I will prepare a Tomcat
Docker Image which uses Redis as a Session Store, which we can use in later
posts to achive high availability for our user sessions.
Now verify that the Redis server is working properly.
You should now be logged in to your Redis Server. When typing INFO keyspace,
no surprise, there is no keyspace yet.
Prepare the Tomcat Image
To use Redis as the Session Store, a Session Manager Implementation is
required. Luckily a project called
tomcat-redis-session-manager
exists, which provides an implementation. The latest official release of the
project is quite old, so I forked it and added a compiled jar of the master
branch on
Github. As
Tomcat base image I have used
tutum/tomcat:7.0. Next the
Session Manager and its dependencies needed to be placed in the image:
For convenience the two environment variables REDIS_HOST and REDIS_PORT
were added to the Dockerfile. They will be evalueted on runtime. Default values
are redis and 6379, which are sufficient when linking the containers
correctly.
Got to http://localhost:8080/manager/html and log in with the credentials you
see on the Tomcat container output. Finally login to the Redis Server again and
do the following:
Tomcat successfully stored the Session in Redis.
A minimal Fig setup with persistence for Redis enabled
might look like this:
In the next post I will show you, how to configure Redis and the Tomcat Image
to use Redis Sentinel, for achieving automatic failover.