这里通过SpringSession来实现Session的共享,Session数据存储在Redis中
SpringSession的操作指南:
https://docs.spring.io/spring-session/docs/2.5.6/reference/html5/guides/boot-redis.html
导入相关的依赖
<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency>
设置对应的配置(yaml文件或者其它)
spring:session:store-type: redisredis:namespace: spring:session
在各服务的启动类上添加注解
@EnableRedisHttpSession
默认存储在redis db0,若想自定义设置
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();connectionFactory.setDatabase(1); // 设置使用第 1 号数据库return connectionFactory;}@Beanpublic HttpSessionConfiguration httpSessionConfiguration() {HttpSessionConfiguration config = new HttpSessionConfiguration();config.setMaxInactiveIntervalInSeconds(1800); // 设置会话过期时间为 30 分钟return config;}
}