This is a tutorial to implement session sharing with spring boot + redis.
In the spring boot documentation, tell us to add @enableredishttpsession to enable spring session support, which is configured as follows:
Java code
- @Configuration
- @EnableRedisHttpSession
- Public class Redissessionconfig {
- }
@enableredishttpsession This annotation is provided by Spring-session-data-redis, so add it in the Pom.xml file:
Java code
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.session</groupId>
- <artifactId>spring-session-data-redis</artifactId>
- </dependency>
Next, you need to configure the location of the Redis server in Application.properties, where we use this machine:
Java code
- Spring.redis.host=localhost
- spring.redis.port=6379
In this way, the simplest spring boot + REDIS Implementation session sharing is complete, below to test.
First we open two Tomcat services, with ports 8080 and 9090, set in Application.properties " " :
Java code
- server.port=8080
Next, define a controller:
Java code
- @RestController
- @RequestMapping (value = "/admin/v1")
- Public class Quickrun {
- @RequestMapping (value = "/first", method = Requestmethod.get)
- Public map<string, object> firstresp (HttpServletRequest request) {
- map<string, object> map = new hashmap<> ();
- Request.getsession (). SetAttribute ("Request Url", Request.getrequesturl ());
- Map.put ("Request Url", Request.getrequesturl ());
- return map;
- }
- @RequestMapping (value = "/sessions", method = Requestmethod.get)
- Public Object Sessions (HttpServletRequest request) {
- map<string, object> map = new hashmap<> ();
- Map.put ("SessionId", Request.getsession (). GetId ());
- Map.put ("message", Request.getsession (). getattribute ("map"));
- return map;
- }
- }
After starting the access test, first access 8080-port Tomcat, return to get "" :
Java code
- {"request Url":"Http://localhost:8080/admin/v1/first"}
Next, we visit the 8080 port of sessions and return:
Java code
- {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/ First "}
Finally, visit the sessions of Port 9090 and return:
Java code
- {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8080/admin/v1/ First "}
As can be seen, 8080 and 90,902 servers return the results, the realization of the session sharing
If you are accessing the first of Port 9090 at this point, return:
Java code
- {"request Url":"Http://localhost:9090/admin/v1/first"}
The sessions of two servers are returned:
Java code
- {"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/ First "}
Using spring boot + Redis to achieve session sharing is very simple and useful, with nginx load balancing, you can achieve distributed applications.
This time Redis does not carry out the master-slave, read-write separation and so on configuration (_ (: З"∠) _ is actually Bo Master lazy, has not tried ...)
Moreover, Nginx single point of failure is also a barrier to our application ... There may be an improved version of this blog later, such as load balancing with zookeeper, so please look forward to it.
Spring Boot + Redis for session sharing