springboot配置 两个redis 哨兵集群
时间: 2023-12-03 22:36:27 浏览: 197
好的,针对您的问题,我可以给出以下步骤:
1. 在pom.xml中添加redis依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
2. 在application.properties中配置两个redis哨兵集群:
```
# 第一个redis哨兵集群
spring.redis.sentinel.master=myMaster1
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
# 第二个redis哨兵集群
spring.redis.sentinel.master=myMaster2
spring.redis.sentinel.nodes=127.0.0.1:26382,127.0.0.1:26383,127.0.0.1:26384
```
3. 在代码中使用两个RedisTemplate来操作两个redis哨兵集群:
```
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate1")
public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean(name = "redisTemplate2")
public RedisTemplate<String, Object> redisTemplate2(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig1 = new RedisSentinelConfiguration()
.master("myMaster1")
.sentinel("127.0.0.1", 26379)
.sentinel("127.0.0.1", 26380)
.sentinel("127.0.0.1", 26381);
RedisSentinelConfiguration sentinelConfig2 = new RedisSentinelConfiguration()
.master("myMaster2")
.sentinel("127.0.0.1", 26382)
.sentinel("127.0.0.1", 26383)
.sentinel("127.0.0.1", 26384);
return new LettuceConnectionFactory(sentinelConfig1, sentinelConfig2);
}
}
```
阅读全文