簡介
Spring框架提供了組件支持多應用共享Session。我採用了spring-session-data-redis組件,將session緩存到redis中。下面簡要說明製做Demo的過程和遇到的問題
Maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
配置application.yml
server:
session:
timeout: 1000
spring:
redis:
[ 忽略相關配置 ]
session:
store-type: redis
redis:
namespace: abc
第一坑:redis config
- redis異常
ERR unknow command ' CONFIG '
- 解決方法
參考https://docs.spring.io/spring-session/docs/current/reference/html5/#api-redisoperationssessionrepository-sessiondestroyedevent
redis服務配置config set notify-keyspace-events Egx
第二坑: @EnableRedisHttpSession和application.yml配置衝突
框架選擇@EnableRedisHttpSession的屬性,並忽略application.yml中的配置;不推薦使用@EnableRedisHttpSession