Session一致性問題

NaShiYu發表於2021-01-02
  • Nginx的 IP_Hash 策略

同⼀個客戶端IP的請求都會被路由到同⼀個⽬標伺服器,也叫做會話粘滯

在nginx的  niginx.conf 中 新增ip_hash

  

  • Spring Session+ Redis的Session共享應⽤

以SpringBoot中部署為例

1. 編入依賴包

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

   2. application.properties中引入配置

spring.redis.host=localhost
spring.redis.database=0
spring.redis.port=6379
spring.redis.password=
spring.redis.connect-timeout=5000

3. 引入註解 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@SpringBootApplication
@EnableCaching
@EnableRedisHttpSession
public class LoginprojectApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginprojectApplication.class, args);
    }
    
}

4. nginx中轉發配置

  

  

  

 

相關文章