Spring-session + Redis solve Session Sharing

   1. Start ensure Redis
           2. Import dependent
                SpringBoot + Spring-Session + Redis
               

<-! The Spring redis the Boot and application environment Basic Configuration ->
                
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency> <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
 

 


            
            3. Configure big profile application.yml
               

server:
 #port: 8080 port:
8081 #redis Configuration spring: repeat: database: 0 host: 127.0.0.1 port: 6379 # password: redis

controller

@RestController
public class SessionController {
    //存放Session值
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        request.getSession().setAttribute("username","zhangsan");
        return "success";
    }

    //获取Session值
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        return (String)request.getSession().getAttribute("username");
    }
}

 

At the same time start the 8080 and 8081 ports in the current project

In 8080 the port data set

 

 Normally get data

 

 

At this time, set the session has been saved to redis

8081 port to detect when they have no access,

If not removed from redis

The same can be normal access

 

 

repeat

 

Guess you like

Origin www.cnblogs.com/chx9832/p/12298760.html