Coder Social home page Coder Social logo

spring-session-example's Introduction

Spring Session Example

This is a small sample project used to demonstrate Spring Session. It consists of an Embedded Tomcat that has a single HelloServlet servlet. When issuing a GET request, the servlet will respond with either the default Hello World! or if the name session attribute has been set with Hello [name]. The name session attribute can be changed by issuing a POST request with a name parameter. For more information, please read my blog post Scaling out with Spring Session.

Redis

Download and install Redis. For example if you are using OS X you can use Brew, e.g.

$ brew install redis  

(If you use Linux, you can use yum or apt-get. Alternatively, read the Quick Start Guide.)

Start Redis by calling the redis-server command, e.g. if you use Brew on OS X:

$ redis-server /usr/local/etc/redis.conf
[...]
Port: 6379
[...]
Server started, Redis version 3.0.1
[...]

2. Starting a Server

Build this project in another terminal:

$ mvn package

Start the first Tomcat instance on port 8080 (the default)

$ target/bin/main
[...]
INFO: Sarting ProtocolHandler ["http-nio-8080"]

Check the default, non session, response by issuing a GET request:

$ curl http://localhost:8080
Hello World!

3. Change the Session State

Change the value of the session attribute name by issuing a POST request with the name set as a parameter:

$ curl -i -d "name=Mattias" http://localhost:8080
[...]
Set-Cookie: SESSION=12b70435-9e6a-4e67-b544-01394dd59da0; Path=/; HttpOnly
[...]

Verify that the session attribute has been changed by issuing a GET request:

$ curl -H "Cookie: SESSION=12b70435-9e6a-4e67-b544-01394dd59da0" localhost:8080
Hello Mattias!

4. Session Replication to a Second Server

Start the second Tomcat instance on port 8081:

$ target/bin/main 8081    
[...]    
INFO: Starting ProtocolHandler ["http-nio-8081"]

Verify that the session attribute is available by issuing a GET request to the other Tomcat:

$ curl -H "Cookie: SESSION=12b70435-9e6a-4e67-b544-01394dd59da0" localhost:8081
Hello Mattias!

5. Failover to New Server

You can simulate server outage and failover by first shutting down your running servers and then restart a third server:

$ target/bin/main 8082    
[...]    
INFO: Starting ProtocolHandler ["http-nio-8082"]
$ curl -H "Cookie: SESSION=12b70435-9e6a-4e67-b544-01394dd59da0" localhost:8082
Hello Mattias!

Conclusion, the session state is preserved in all these cases.

spring-session-example's People

Contributors

matsev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.