mirror of
https://github.com/iflytek/skillhub.git
synced 2026-08-28 11:25:00 +00:00
feat(server): 支持 Redis Sentinel 模式密码配置
- RedissonConfig 根据 spring.profiles.active 切换普通/Sentinel密码 - 新增 application-redis-sentinel.yml 专属 Spring profile - 新增 RedissonConfigTest 覆盖普通和 Sentinel 两种模式用例 Signed-off-by: jangrui <admin@jangrui.com>
This commit is contained in:
parent
906c7f9884
commit
07c97cf7cd
3 changed files with 44 additions and 1 deletions
|
|
@ -41,7 +41,8 @@ public class RedissonConfig {
|
|||
private static void configureSentinelServers(Config config, RedisProperties redisProperties) {
|
||||
SentinelServersConfig sentinelServersConfig = config.useSentinelServers()
|
||||
.setMasterName(redisProperties.getSentinel().getMaster())
|
||||
.setDatabase(redisProperties.getDatabase());
|
||||
.setDatabase(redisProperties.getDatabase())
|
||||
.setCheckSentinelsList(false);
|
||||
List<String> nodes = redisProperties.getSentinel().getNodes();
|
||||
nodes.stream()
|
||||
.map(String::trim)
|
||||
|
|
@ -50,6 +51,9 @@ public class RedissonConfig {
|
|||
.forEach(sentinelServersConfig::addSentinelAddress);
|
||||
|
||||
applySharedSettings(sentinelServersConfig, redisProperties);
|
||||
if (StringUtils.hasText(redisProperties.getSentinel().getPassword())) {
|
||||
sentinelServersConfig.setSentinelPassword(redisProperties.getSentinel().getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasSentinelConfiguration(RedisProperties redisProperties) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
spring:
|
||||
data:
|
||||
redis:
|
||||
password: ${SPRING_DATA_REDIS_SENTINEL_PASSWORD:${SPRING_DATA_REDIS_PASSWORD:${REDIS_PASSWORD:}}}
|
||||
sentinel:
|
||||
master: ${SPRING_DATA_REDIS_SENTINEL_MASTER:${REDIS_SENTINEL_MASTER:mymaster}}
|
||||
nodes: ${SPRING_DATA_REDIS_SENTINEL_NODES:${REDIS_SENTINEL_NODES:}}
|
||||
password: ${SPRING_DATA_REDIS_SENTINEL_PASSWORD:${REDIS_SENTINEL_PASSWORD:}}
|
||||
|
|
@ -105,6 +105,37 @@ class RedissonConfigTest {
|
|||
assertThat(sentinelConfig.getSentinelAddresses()).containsExactly("rediss://redis-sentinel-1:26379");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createConfig_appliesSentinelPasswordWhenSet() throws Exception {
|
||||
RedisProperties properties = new RedisProperties();
|
||||
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
|
||||
sentinel.setMaster("mymaster");
|
||||
sentinel.setNodes(List.of("redis-sentinel-1:26379"));
|
||||
sentinel.setPassword("sentinel-secret");
|
||||
properties.setSentinel(sentinel);
|
||||
properties.setPassword("master-secret");
|
||||
|
||||
Config config = RedissonConfig.createConfig(properties);
|
||||
SentinelServersConfig sentinelConfig = sentinelConfig(config);
|
||||
|
||||
assertThat(sentinelConfig.getSentinelPassword()).isEqualTo("sentinel-secret");
|
||||
assertThat(sentinelConfig.getPassword()).isEqualTo("master-secret");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createConfig_sentinelCheckSentinelsListDisabled() throws Exception {
|
||||
RedisProperties properties = new RedisProperties();
|
||||
RedisProperties.Sentinel sentinel = new RedisProperties.Sentinel();
|
||||
sentinel.setMaster("mymaster");
|
||||
sentinel.setNodes(List.of("redis-sentinel-1:26379"));
|
||||
properties.setSentinel(sentinel);
|
||||
|
||||
Config config = RedissonConfig.createConfig(properties);
|
||||
SentinelServersConfig sentinelConfig = sentinelConfig(config);
|
||||
|
||||
assertThat(sentinelConfig.isCheckSentinelsList()).isFalse();
|
||||
}
|
||||
|
||||
private SentinelServersConfig sentinelConfig(Config config) throws Exception {
|
||||
Method method = Config.class.getDeclaredMethod("getSentinelServersConfig");
|
||||
method.setAccessible(true);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue