Spring boot + redis realizes session sharing

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:

Java code  
@Configuration  
@EnableRedisHttpSession  
public class RedisSessionConfig {  
}
 

The annotation @EnableRedisHttpSession is provided by spring-session-data-redis, so add it in 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 application.properties. Here, we use this machine:

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

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    :

server.port=8080
 

 

Next define a 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 starting the access test, first access tomcat on port 8080 and return   :

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

 Next, we access the sessions on port 8080 and return:

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

Finally, access the sessions on port 9090 and return:

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

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:

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

The sessions of both servers return:

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

 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529875&siteId=291194637