refactor: make session store redis by default
This commit is contained in:
@@ -7,7 +7,7 @@ import java.time.Duration;
|
|||||||
@ConfigurationProperties(prefix = "lingniu.ingest.session")
|
@ConfigurationProperties(prefix = "lingniu.ingest.session")
|
||||||
public class SessionProperties {
|
public class SessionProperties {
|
||||||
|
|
||||||
private Store store = Store.MEMORY;
|
private Store store = Store.REDIS;
|
||||||
|
|
||||||
private Duration ttl = Duration.ofMinutes(30);
|
private Duration ttl = Duration.ofMinutes(30);
|
||||||
|
|
||||||
@@ -16,6 +16,9 @@ public class SessionProperties {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setStore(Store store) {
|
public void setStore(Store store) {
|
||||||
|
if (store == Store.MEMORY) {
|
||||||
|
throw new IllegalStateException("session.store supports only redis; configured value: memory");
|
||||||
|
}
|
||||||
this.store = store;
|
this.store = store;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,15 +17,10 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
|||||||
@EnableConfigurationProperties(SessionProperties.class)
|
@EnableConfigurationProperties(SessionProperties.class)
|
||||||
public class SessionCoreAutoConfiguration {
|
public class SessionCoreAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "memory")
|
|
||||||
public Object rejectedLegacyMemorySessionStore() {
|
|
||||||
throw new IllegalStateException("session.store=memory has been removed; configure session.store=redis");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnBean(StringRedisTemplate.class)
|
@ConditionalOnBean(StringRedisTemplate.class)
|
||||||
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "redis")
|
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "redis",
|
||||||
|
matchIfMissing = true)
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public SessionStore redisSessionStore(StringRedisTemplate redis, SessionProperties properties) {
|
public SessionStore redisSessionStore(StringRedisTemplate redis, SessionProperties properties) {
|
||||||
// Redis 只保存会话元数据索引;真实 Netty Channel 仍在各协议进程本地内存中。
|
// Redis 只保存会话元数据索引;真实 Netty Channel 仍在各协议进程本地内存中。
|
||||||
|
|||||||
@@ -43,12 +43,22 @@ class SessionCoreAutoConfigurationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void rejectsLegacyMemorySessionStoreMode() {
|
void usesRedisSessionStoreByDefaultWhenRedisTemplateExists() {
|
||||||
|
contextRunner
|
||||||
|
.withBean(StringRedisTemplate.class, () -> mock(StringRedisTemplate.class))
|
||||||
|
.run(context -> {
|
||||||
|
assertThat(context).hasSingleBean(SessionStore.class);
|
||||||
|
assertThat(context).hasSingleBean(RedisSessionStore.class);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsMemorySessionStoreModeWithRedisOnlyRule() {
|
||||||
contextRunner
|
contextRunner
|
||||||
.withPropertyValues("lingniu.ingest.session.store=memory")
|
.withPropertyValues("lingniu.ingest.session.store=memory")
|
||||||
.run(context -> assertThat(context.getStartupFailure())
|
.run(context -> assertThat(context.getStartupFailure())
|
||||||
.hasRootCauseInstanceOf(IllegalStateException.class)
|
.hasRootCauseInstanceOf(IllegalStateException.class)
|
||||||
.hasMessageContaining("session.store=memory has been removed")
|
.hasStackTraceContaining("session.store supports only redis")
|
||||||
.hasMessageContaining("session.store=redis"));
|
.hasStackTraceContaining("memory"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user