【Redis】多机部署Redis-sentinel-2. SpringBoot配置Redis-sentinel

时间:2024-04-04 11:38:08

application.yaml:

spring:
  redis:
    password: "xxx" // Redis密码
    sentinel:
      master: mymaster
      nodes:
        - xxx.xxxx.xxx.xxx:27001
        - xxx.xxxx.xxx.xxx:27001
        - xxx.xxxx.xxx.xxx:27001

在启动类中注册配置类Bean

@SpringBootApplication
public class RedisDemoApplication {

    @Bean
    public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer() {
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }

    public static void main(String[] args) {
        SpringApplication.run(RedisDemoApplication.class, args);
        System.out.println("启动成功");
    }

}