2024-10-08 09:38:54 +08:00
|
|
|
|
/**
|
2025-03-04 10:42:17 +08:00
|
|
|
|
* 开源代码,仅供学习和交流研究使用,商用请联系三丙
|
|
|
|
|
|
* 微信:mohan_88888
|
|
|
|
|
|
* 抖音:程序员三丙
|
|
|
|
|
|
* 付费课程知识星球:https://t.zsxq.com/aKtXo
|
2024-10-08 09:38:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
package sanbing.jcpp.infrastructure.cache;
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2025-06-11 16:43:26 +08:00
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
2024-10-08 09:38:54 +08:00
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
|
|
|
|
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
|
|
|
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
2025-06-11 16:43:26 +08:00
|
|
|
|
@ConditionalOnExpression("'${cache.type:null}'=='redis' && '${redis.connection.type:null}'=='sentinel'")
|
2024-10-08 09:38:54 +08:00
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class JCPPJCPPRedisSentinelConfiguration extends JCPPRedisCacheConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.sentinel.master:}")
|
|
|
|
|
|
private String master;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.sentinel.sentinels:}")
|
|
|
|
|
|
private String sentinels;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.sentinel.password:}")
|
|
|
|
|
|
private String sentinelPassword;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.sentinel.useDefaultPoolConfig:true}")
|
|
|
|
|
|
private boolean useDefaultPoolConfig;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.db:}")
|
|
|
|
|
|
private Integer database;
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${redis.password:}")
|
|
|
|
|
|
private String password;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public LettuceConnectionFactory loadFactory() {
|
|
|
|
|
|
log.info("Initializing Redis Sentinel on {}, sentinels: {}", master, sentinels);
|
|
|
|
|
|
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
|
|
|
|
|
|
redisSentinelConfiguration.setMaster(master);
|
|
|
|
|
|
redisSentinelConfiguration.setSentinels(getNodes(sentinels));
|
|
|
|
|
|
redisSentinelConfiguration.setSentinelPassword(sentinelPassword);
|
|
|
|
|
|
redisSentinelConfiguration.setPassword(password);
|
|
|
|
|
|
redisSentinelConfiguration.setDatabase(database);
|
|
|
|
|
|
return new LettuceConnectionFactory(redisSentinelConfiguration, buildClientConfig());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private LettucePoolingClientConfiguration buildClientConfig() {
|
|
|
|
|
|
var clientConfigurationBuilder = LettucePoolingClientConfiguration.builder();
|
|
|
|
|
|
if (!useDefaultPoolConfig) {
|
|
|
|
|
|
clientConfigurationBuilder.poolConfig(buildPoolConfig());
|
|
|
|
|
|
}
|
|
|
|
|
|
return clientConfigurationBuilder.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|