I managed to get a good demonstration set-up for spring-session using docker-compose which shows off the strengths of this project and I wanted to write about this here. In short, this is the set-up that running docker-compose will bring up:
Two instances of the application which makes use of Spring-session are started up, these instances use the same redis container for storing the session state and are in turn fronted by an nginx server.
All that needs to be done to bring up this topology is to:
- clone my repo available here
- install docker-compose
- build the app - "mvn package -DskipTests" - skipping tests as the tests depend on a local redis-server, which may or may not be available
- run "docker-compose up" in the cloned folder
That is it, if everything has been set-up cleanly nginx should be available at http://docker-ip url - in my mac, it is typically http://192.168.59.103
Details and Demonstration:
Docker-compose is a tool providing a means to put together a set of docker containers into a coherent stack. The stack can be defined declaratively, and the following is a sample stack used here:
nginx: image: nginx volumes: - nginx:/etc/nginx:ro links: - shop1 - shop2 ports: - "80:80" shop1: build: . hostname: shop1 links: - redis ports: - "8081:8080" shop2: build: . hostname: shop2 links: - redis ports: - "8082:8080" redis: image: redis hostname: redis ports: - "6379:6379"
This application itself makes use of the user session to maintain the state of a "shopping cart", since this application is configured to use spring-session the session will be maintained in redis database. There are two instances of the application behind nginx, either one of the servers would end up getting the request, but the externalized session state would continue to work seamlessly irrespective of the application instance handling the request.
The following is the view of the shopping cart:
The session id and the details of the instance handling the request are printed at the bottom of the page.
As can be seen in the following screenshot, even if a different instance handles the request, the session state continues to be cleanly maintained.