Reactive Redis 索引式配置
要开始使用 Redis 索引式 Web 会话支持,你需要将以下依赖项添加到你的项目
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
implementation 'org.springframework.session:spring-session-data-redis'
并将 `@EnableRedisIndexedWebSession` 注解添加到配置类中
@Configuration
@EnableRedisIndexedWebSession
public class SessionConfig {
// ...
}
就这样。你的应用程序现在拥有了基于 Reactive Redis 的索引式 Web 会话支持。现在你的应用程序已经配置好了,你可能想要开始自定义一些东西
使用 JSON 序列化会话
默认情况下,Spring Session Data Redis 使用 Java 序列化来序列化会话属性。有时这可能会带来问题,尤其是在你有多个应用程序使用同一个 Redis 实例但具有相同类的不同版本时。你可以提供一个 `RedisSerializer` bean 来定制会话如何被序列化到 Redis 中。Spring Data Redis 提供了 `GenericJackson2JsonRedisSerializer`,它使用 Jackson 的 `ObjectMapper` 来序列化和反序列化对象。
@Configuration
public class SessionConfig implements BeanClassLoaderAware {
private ClassLoader loader;
/**
* Note that the bean name for this bean is intentionally
* {@code springSessionDefaultRedisSerializer}. It must be named this way to override
* the default {@link RedisSerializer} used by Spring Session.
*/
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer(objectMapper());
}
/**
* Customized {@link ObjectMapper} to add mix-in for class that doesn't have default
* constructors
* @return the {@link ObjectMapper} to use
*/
private ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
return mapper;
}
/*
* @see
* org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang
* .ClassLoader)
*/
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.loader = classLoader;
}
}
上面的代码片段使用了 Spring Security,因此我们创建了一个使用 Spring Security 的 Jackson 模块的自定义 `ObjectMapper`。如果你不需要 Spring Security 的 Jackson 模块,你可以注入你应用程序的 `ObjectMapper` bean 并像这样使用它
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
`RedisSerializer` bean 的名称必须是 `springSessionDefaultRedisSerializer`,这样它才不会与 Spring Data Redis 使用的其他 `RedisSerializer` bean 冲突。如果提供了不同的名称,Spring Session 将不会选取它。 |
指定不同的命名空间
多个应用程序使用同一个 Redis 实例,或者希望将会话数据与其他存储在 Redis 中的数据分开,这并不少见。因此,Spring Session 使用 `namespace`(默认为 `spring:session`)来在需要时将会话数据分开。
你可以通过在 `@EnableRedisIndexedWebSession` 注解中设置 `redisNamespace` 属性来指定 `namespace`
@Configuration
@EnableRedisIndexedWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
理解 Spring Session 如何清理过期会话
Spring Session 依赖于 Redis Keyspace Events 来清理过期会话。更具体地说,它监听发送到 `__keyevent@*__:expired` 和 `__keyevent@*__:del` 通道的事件,并根据被销毁的键解析出会话 ID。
例如,假设我们有一个会话,其 ID 为 `1234`,并且该会话设置为 30 分钟后过期。当达到过期时间时,Redis 会向 `__keyevent@*__:expired` 通道发送一个事件,消息为 `spring:session:sessions:expires:1234`,这是已过期的键。然后 Spring Session 将从该键解析出会话 ID(`1234`),并从 Redis 中删除所有相关的会话键。
完全依赖 Redis 过期机制的一个问题是,如果键未被访问,Redis 不保证何时会触发过期事件。更多详细信息请参见 Redis 文档中的键如何过期。为了规避过期事件不一定触发的事实,我们可以确保在每个键预期过期时被访问。这意味着,如果键的 TTL 已过期,当我们尝试访问该键时,Redis 将移除该键并触发过期事件。因此,每个会话的过期也被跟踪,通过将会话 ID 存储在一个按过期时间排序的有序集合中。这允许后台任务访问潜在的过期会话,以更确定的方式确保 Redis 过期事件被触发。例如
ZADD spring:session:sessions:expirations "1.702402961162E12" "648377f7-c76f-4f45-b847-c0268bb48381"
我们不会显式删除键,因为在某些情况下可能存在竞态条件,错误地将一个尚未过期的键标识为已过期。除了使用分布式锁(这会极大地影响我们的性能)之外,没有办法确保过期映射的一致性。通过简单地访问键,我们确保只有当该键的 TTL 过期时,键才会被移除。
默认情况下,Spring Session 每 60 秒检索最多 100 个过期会话。如果你想配置清理任务的运行频率,请参阅更改会话清理的频率部分。
配置 Redis 发送 Keyspace Events
默认情况下,Spring Session 尝试使用 `ConfigureNotifyKeyspaceEventsReactiveAction` 配置 Redis 发送 keyspace events,这可能会将 `notify-keyspace-events` 配置属性设置为 `Egx`。然而,如果 Redis 实例已正确加固,此策略将不起作用。在这种情况下,应在外部配置 Redis 实例,并暴露一个类型为 `ConfigureReactiveRedisAction.NO_OP` 的 Bean 来禁用自动配置。
@Bean
public ConfigureReactiveRedisAction configureReactiveRedisAction() {
return ConfigureReactiveRedisAction.NO_OP;
}
更改会话清理的频率
根据你应用程序的需求,你可能需要更改会话清理的频率。为此,你可以暴露一个 `ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository>` Bean 并设置 `cleanupInterval` 属性
@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setCleanupInterval(Duration.ofSeconds(30));
}
你也可以调用 `disableCleanupTask()` 来禁用清理任务。
@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.disableCleanupTask();
}
控制清理任务
有时,默认的清理任务可能无法满足你应用程序的需求。你可能希望采用不同的策略来清理过期会话。既然你知道会话 ID 存储在键为 `spring:session:sessions:expirations` 的有序集合中,并按其过期时间排序,你可以禁用默认清理任务并提供你自己的策略。例如
@Component
public class SessionEvicter {
private ReactiveRedisOperations<String, String> redisOperations;
@Scheduled
public Mono<Void> cleanup() {
Instant now = Instant.now();
Instant oneMinuteAgo = now.minus(Duration.ofMinutes(1));
Range<Double> range = Range.closed((double) oneMinuteAgo.toEpochMilli(), (double) now.toEpochMilli());
Limit limit = Limit.limit().count(1000);
return this.redisOperations.opsForZSet().reverseRangeByScore("spring:session:sessions:expirations", range, limit)
// do something with the session ids
.then();
}
}
监听会话事件
通常,对会话事件作出反应很有价值,例如,你可能想要根据会话的生命周期进行一些处理。
你可以配置你的应用程序来监听 `SessionCreatedEvent`、`SessionDeletedEvent` 和 `SessionExpiredEvent` 事件。Spring 中有几种方式监听应用程序事件,本例中我们将使用 `@EventListener` 注解。
@Component
public class SessionEventListener {
@EventListener
public Mono<Void> processSessionCreatedEvent(SessionCreatedEvent event) {
// do the necessary work
}
@EventListener
public Mono<Void> processSessionDeletedEvent(SessionDeletedEvent event) {
// do the necessary work
}
@EventListener
public Mono<Void> processSessionExpiredEvent(SessionExpiredEvent event) {
// do the necessary work
}
}