1. Redis-sentinel配置文件
注意不同服务器上的Redis版本需要相同,否则可能因为RDB文件不同而导致Redis主从同步失败。
java
bind 0.0.0.0 // 任何ip都可以连接
port 27001 // 本机的端口
daemonize yes // 后台运行
sentinel announce-ip "xxx.xxx.xxx.xxx" // 本机的ip
sentinel monitor mymaster xxx.xxxx.xxxx.xxx 6379 2 // masterRedis的ip 端口 要有多少个sentinel票可以选为master
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster xxxx // 密码
dir "/root/sentinel/sentinel"
logfile "a.log"
# Generated by CONFIG REWRITE
protected-mode no
2. SpringBoot配置Redis-sentinel
application.yaml:
java
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
java
@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("启动成功");
}
}