spring boot and redis implement session Shared tutorial

  • 2020-06-23 00:14:04
  • OfStack

If you are not familiar with spring boot, you can refer to the following two articles.

Spring Boot Quick start tutorial

Spring Boot Quick start Guide

This time comes the spring boot + redis tutorial for session sharing.

In the documentation for spring boot, tell us to add @EnableRedisHttpSession to enable spring session support, configured as follows:


@Configuration 
@EnableRedisHttpSession 
public class RedisSessionConfig { 
} 

The @EnableRedisHttpSession annotation is provided by spring-ES29en-ES30en-ES31en, so add it to the pom.xml file:


<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 ES37en.properties, where we will use native:


spring.redis.host=localhost 
spring.redis.port=6379 

Thus, the simplest spring boot + redis implementation of session sharing is completed, the next test.

First, we open two tomcat services, ports 8080 and 9090 respectively, and set them in ES50en.ES51en [Download address] :

server.port=8080

Next, define 1 Controller:


@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 startup, access test is conducted. First, tomcat of port 8080 is accessed and [download address] is returned:


{"request Url":"http://localhost:8080/admin/v1/first"} 

Next, we visit sessions on port 8080 and return:


{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":http://localhost:8080/admin/v1/first}

Finally, access sessions on port 9090 again and return:


{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":http://localhost:8080/admin/v1/first}

It can be seen that the 8080 and 9090 servers return a similar result and realize the sharing of session

If you access first on port 9090 at this point, first return:


{"request Url":"http://localhost:9090/admin/v1/first"} 

Both servers return sessions:


{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:9090/admin/v1/first"} 

Sharing of session via spring boot + redis is very simple and very useful. With nginx for load balancing, distributed applications can be realized.

The redis did not configure master-slave separation, speaking, reading and writing, and so on (_ (: з "<) _ is lazy blogger, haven't tried to...)

Moreover, the single point of failure of nginx is also an obstacle to our application... Hopefully, there will be improved versions of this blog, such as load balancing using zookeeper.


Related articles: