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

}
相关推荐
哭哭啼6 分钟前
JAVA服务问题诊断
java·开发语言·jvm
Sayuanni%332 分钟前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
坚定信念,勇往无前1 小时前
Maven 私有仓库-nexus
java
Minner-Scrapy1 小时前
Scrapy 2.17 源码解析:Scheduler 调度器与磁盘/内存双队列
java·爬虫·python·scrapy·网络爬虫·twisted
晚风醉蝶1 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
夏天拐跑了西瓜2 小时前
Spring Cloud 微服务实战(三):一个服务挂了,凭什么拖垮整个系统?Sentinel 限流熔断实战
java·spring cloud·微服务
2601_967264282 小时前
Jetpack Compose 实践指南:从入门到进阶
java·学习
m0_587383003 小时前
社区家政系统开发实战:从需求分析到上线部署全指南
java·spring boot·spring·需求分析
凤山老林3 小时前
动态 i18n 体系落地:Spring Boot 多租户热加载与前后端协同实践
java·spring boot·后端·i18n
白露与泡影3 小时前
解密 Pi 的 Harness 工程:Agent 会话如何实现持久化与恢复
java·人工智能·算法