【Redis】多机部署Redis-sentinel

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("启动成功");
    }

}
相关推荐
Asurplus3 分钟前
Centos7安装Maven环境
java·centos·maven·apache·yum
想学后端的前端工程师11 分钟前
【Spring Boot微服务开发实战:从入门到企业级应用】
java·开发语言·python
徐老总20 分钟前
手机号脱敏处理(Python/Scala 双版本实现)
java
夏末47228 分钟前
面试必问!多线程操作集合避坑指南:用synchronized搞定线程安全
java
CC.GG31 分钟前
【C++】STL----封装红黑树实现map和set
android·java·c++
loosenivy35 分钟前
IP风险画像识别和IP风险预警接口
java·ip查询·ip风险画像识别·ip预警查询·ip画像
汤姆yu1 小时前
基于springboot的林业资源管理系统
java·spring boot·后端
软件管理系统1 小时前
基于Spring Boot的医疗服务系统的设计与实现
java·spring boot·后端
软件管理系统1 小时前
基于微信小程序的健身房管理系统
java·tomcat·maven