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.
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
[...]
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!
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!
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!
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.