跳到主要内容

响应式 Redis 索引配置

QWen Max 中英对照 Redis Indexed Web Session Reactive Redis Indexed Configurations

要开始使用 Redis Indexed Web Session 支持,你需要在项目中添加以下依赖项:

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

并在配置类中添加 @EnableRedisIndexedWebSession 注解:

@Configuration
@EnableRedisIndexedWebSession
public class SessionConfig {
// ...
}
java

就是这样。现在你的应用程序已经有了基于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;
}

}
java

上述代码片段使用了 Spring Security,因此我们创建了一个自定义的 ObjectMapper,它使用了 Spring Security 的 Jackson 模块。如果您不需要 Spring Security 的 Jackson 模块,您可以注入您的应用程序的 ObjectMapper bean 并像这样使用:

@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
java
备注

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 {
// ...
}
java

了解 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 文档中的 Redis 如何使键过期。为了规避过期事件不能保证发生的问题,我们可以在键预期过期时确保每个键都被访问。这意味着,如果键的 TTL 已过期,在我们尝试访问该键时,Redis 会移除该键并触发过期事件。因此,每个会话的过期情况也会通过将会话 ID 存储在按其过期时间排序的有序集合中进行跟踪。这允许后台任务访问可能已过期的会话,以确保 Redis 的过期事件以更确定的方式触发。例如:

ZADD spring:session:sessions:expirations "1.702402961162E12" "648377f7-c76f-4f45-b847-c0268bb48381"

我们不会显式地删除键,因为在某些情况下,可能会出现竞态条件,导致错误地将未过期的键识别为已过期。除了使用分布式锁(这会严重影响我们的性能)之外,没有其他方法可以确保过期映射的一致性。通过简单地访问键,我们可以确保只有在该键的TTL到期时才会被移除。

默认情况下,Spring Session 每 60 秒最多会检索 100 个过期的会话。如果您想配置清理任务的运行频率,请参阅更改会话清理的频率部分。

配置 Redis 发送键空间事件

默认情况下,Spring Session 会尝试使用 ConfigureNotifyKeyspaceEventsReactiveAction 来配置 Redis 以发送键空间事件,这反过来可能会将 notify-keyspace-events 配置属性设置为 Egx。但是,如果 Redis 实例已经正确地进行了安全设置,这种策略将不起作用。在这种情况下,应该对外部配置 Redis 实例,并暴露一个类型为 ConfigureReactiveRedisAction.NO_OP 的 Bean 以禁用自动配置。

@Bean
public ConfigureReactiveRedisAction configureReactiveRedisAction() {
return ConfigureReactiveRedisAction.NO_OP;
}
java

更改会话清理的频率

根据你的应用程序的需求,你可能需要更改会话清理的频率。为此,你可以暴露一个 ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> bean 并设置 cleanupInterval 属性:

@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setCleanupInterval(Duration.ofSeconds(30));
}
java

您还可以调用 disableCleanupTask() 来禁用清理任务。

@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.disableCleanupTask();
}
java

控制清理任务

有时,默认的清理任务可能无法满足应用程序的需求。你可能希望采用不同的策略来清理过期的会话。由于你知道会话 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();
}

}
java

监听会话事件

很多时候,对会话事件做出反应是有价值的,例如,你可能希望根据会话生命周期进行某种处理。

你配置应用程序以监听 SessionCreatedEventSessionDeletedEventSessionExpiredEvent 事件。在 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
}

}
java