此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Session 3.3.1! |
要开始使用 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 {
// ...
}
就是这样。您的应用程序现在具有响应式 Redis 支持的索引 Web 会话支持。 现在,您已经配置了应用程序,您可能希望开始自定义内容:
使用 JSON 序列化会话
默认情况下,Spring Session Data Redis 使用 Java 序列化来序列化会话属性。
有时可能会出现问题,尤其是当您有多个应用程序使用相同的 Redis 实例但具有同一类的不同版本时。
您可以提供一个 Bean 来自定义会话序列化到 Redis 的方式。
Spring Data Redis 提供了使用 Jackson 的 .RedisSerializer
GenericJackson2JsonRedisSerializer
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 模块的自定义。
如果你不需要 Spring Security Jackson 模块,你可以注入应用程序的 bean 并像这样使用它:ObjectMapper
ObjectMapper
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
Bean 名称必须不会与 Spring Data Redis 使用的其他 Bean 冲突。
如果提供了不同的名称,则 Spring Session 不会选择它。 |
Bean 名称必须不会与 Spring Data Redis 使用的其他 Bean 冲突。
如果提供了不同的名称,则 Spring Session 不会选择它。 |
指定其他命名空间
有多个应用程序使用同一个 Redis 实例,或者希望将会话数据与存储在 Redis 中的其他数据分开,这种情况并不少见。
出于这个原因,Spring Session 使用(默认为 )来保持会话数据的分离(如果需要)。namespace
spring:session
您可以通过在注释中设置属性来指定:namespace
redisNamespace
@EnableRedisIndexedWebSession
@Configuration
@EnableRedisIndexedWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
了解 Spring Session 如何清理过期的会话
Spring Session 依靠 Redis Keyspace Events 来清理过期的会话。
更具体地说,它侦听发送到 and 通道的事件,并根据已销毁的密钥解析会话 ID。__keyevent@*__:expired
__keyevent@*__:del
举个例子,假设我们有一个 id 的会话,并且该会话设置为在 30 分钟后过期。
当达到过期时间时,Redis 会向通道发出一个事件,其中包含消息,该消息是过期的密钥。
然后,Spring Session 将从密钥中解析会话 id(),并从 Redis 中删除所有相关的会话密钥。1234
__keyevent@*__:expired
spring:session:sessions:expires:1234
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 以发送密钥空间事件
默认情况下,Spring Session 会尝试将 Redis 配置为使用 发送密钥空间事件,而 这反过来又可能会将配置属性设置为 。
但是,如果 Redis 实例已正确保护,则此策略将不起作用。
在这种情况下,应该在外部配置 Redis 实例,并且应该公开 Bean 类型以禁用自动配置。ConfigureNotifyKeyspaceEventsReactiveAction
notify-keyspace-events
Egx
ConfigureReactiveRedisAction.NO_OP
@Bean
public ConfigureReactiveRedisAction configureReactiveRedisAction() {
return ConfigureReactiveRedisAction.NO_OP;
}
更改会话清理的频率
根据应用程序的需求,可能需要更改会话清理的频率。
为此,您可以公开一个 Bean 并设置属性:ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository>
cleanupInterval
@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setCleanupInterval(Duration.ofSeconds(30));
}
您还可以设置 invoke 来禁用清理任务。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();
}
}
收听会话事件
通常,对会话事件做出反应很有价值,例如,您可能希望根据会话生命周期执行某种处理。
将应用程序配置为侦听 和 事件。
有几种方法可以在 Spring 中侦听应用程序事件,在这个例子中,我们将使用注解。SessionCreatedEvent
SessionDeletedEvent
SessionExpiredEvent
@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
}
}