SpringSession+Redis realizes [Distributed Session]

1. What is Spring Session

SpringBoot's automatic configuration of Spring-Session integration can be described as out-of-the-box, extremely concise and convenient. This article introduces SpringBoot's integration of Spring-Session. Here we only introduce the actual combat based on RedisSession.

Spring Session is a sub-project of the Spring family. Spring Session provides APIs and implementations for managing user session information. It replaces the httpSession implemented by the servlet container with spring-session, focusing on solving session management problems. The default session information is stored in Redis, which can be easily, quickly and seamlessly integrated into our applications

Spring session official website address: Spring Session

Features of Spring Session:

  • Provide API and implementation of user session management
  • Provide HttpSession to replace the session of the web container in a neutral way, such as the session in tomcat
  • Support cluster session processing, without binding to a specific web container to solve the session sharing problem under the cluster   

 2. Why use Spring Session

 

SpringCloud microservice disassembles a complete monolithic application into independent sub-services, and each independent micro-service sub-module will be deployed to a different server, and services are independently isolated from each other. At this time, to implement session session sharing between services, you need to use the spring-session framework to solve the problem of distributed session management and sharing.


3. Configure spring-session

Note: The public module is used as the dependency support of all microservice submodules. If redis support is not configured in each service module, an error will occur when starting other microservices.

<!--redis-->
<dependency>    
   <groupId>org.springframework.boot</groupId>    
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring session-->
<dependency>   
   <groupId>org.springframework.session</groupId>    
   <artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--commons-pool2-->
<dependency>    
   <groupId>org.apache.commons</groupId>  
   <artifactId>commons-pool2</artifactId>
</dependency>

 Configure application.yml in commodity service and user service respectively

spring:
  session:
    redis:
      flush-mode: on_save
      namespace: session.zmall
      cleanup-cron: 0 * * * * *
    store-type: redis
    timeout: 1800
  redis:
    host: localhost #redis ip
    port: 6379 #redis  端口
    password: 123456 #redis  密码
    jedis:
      pool:
        max-active: 100
        max-wait: 10
        max-idle: 10
        min-idle: 10
    database: 0

Guess you like

Origin blog.csdn.net/m0_63300795/article/details/128337690