This time I bring a tutorial on spring boot + redis to achieve session sharing.
In the spring boot documentation, we are told to add @EnableRedisHttpSession to enable spring session support. The configuration is as follows:
The annotation @EnableRedisHttpSession is provided by spring-session-data-redis, so add it in the pom.xml file:
Next, you need to configure the location of the redis server in application.properties. Here, we use this machine:
In this way, the simplest spring boot + redis implementation of session sharing is completed, and the next test is carried out.
First, we open two tomcat services, the ports are 8080 and 9090, and set them in application.properties :
Next define a Controller:
After starting the access test, first access tomcat on port 8080 and return :
Next, we access the sessions on port 8080 and return:
Finally, access the sessions on port 9090 and return:
It can be seen that the two servers of 8080 and 9090 return the same results, realizing the sharing of sessions
If you access the first of port 9090 at this time, first return:
The sessions of both servers return:
It is very simple and useful to realize session sharing through spring boot + redis. With nginx for load balancing, distributed applications can be realized.
This time redis has not been configured for master-slave, read-write separation, etc. (_(:з"∠)_ is actually a lazy blogger, I haven't tried it yet....)
Moreover, the single point of failure of nginx is also an obstacle to our application... There may be an improved version of this blog in the future, such as using zookeeper for load balancing, so stay tuned.