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

}
相关推荐
凤山老林12 分钟前
04-Java JDK, JRE和JVM
java·开发语言·jvm
camellias_6 小时前
【无标题】
java·tomcat
咸鱼2.07 小时前
【java入门到放弃】需要背诵
java·开发语言
椰猫子7 小时前
Java:异常(exception)
java·开发语言
win x8 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海8 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言
阿维的博客日记8 小时前
什么是逃逸分析
java·juc
Ricky_Theseus9 小时前
C++右值引用
java·开发语言·c++
Rick19939 小时前
Java内存参数解析
java·开发语言·jvm
我是大猴子9 小时前
Spring代理类为何依赖注入失效?
java·后端·spring