Over a million developers have joined DZone.

Spring-session Demonstration using docker-compose

DZone's Guide to

Spring-session Demonstration using docker-compose

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

I have  earlier written about an exciting new project called  Spring-session which provides a clean way to externalize user sessions for java based web applications. 

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

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:

  image: nginx
    - nginx:/etc/nginx:ro
    - shop1
    - shop2
   - "80:80"

  build: .
  hostname: shop1
    - redis
    - "8081:8080"

  build: .
  hostname: shop2
    - redis
    - "8082:8080"

  image: redis
  hostname: redis
    - "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.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}