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

}
相关推荐
懒羊羊不懒@23 分钟前
Java基础语法—最小单位、及注释
java·c语言·开发语言·数据结构·学习·算法
ss27326 分钟前
手写Spring第4弹: Spring框架进化论:15年技术变迁:从XML配置到响应式编程的演进之路
xml·java·开发语言·后端·spring
DokiDoki之父38 分钟前
MyBatis—增删查改操作
java·spring boot·mybatis
兩尛1 小时前
Spring面试
java·spring·面试
Java中文社群1 小时前
服务器被攻击!原因竟然是他?真没想到...
java·后端
Full Stack Developme1 小时前
java.nio 包详解
java·python·nio
零千叶1 小时前
【面试】Java JVM 调优面试手册
java·开发语言·jvm
代码充电宝2 小时前
LeetCode 算法题【简单】290. 单词规律
java·算法·leetcode·职场和发展·哈希表
li3714908902 小时前
nginx报400bad request 请求头过大异常处理
java·运维·nginx
摇滚侠2 小时前
Spring Boot 项目, idea 控制台日志设置彩色
java·spring boot·intellij-idea