springboot如何集成redis哨兵集群?

14 篇文章 0 订阅
5 篇文章 0 订阅

前言

redis主从集群和redis sentinel集群都配置完毕了, 现在我们需要了解spring boot 如何连接上该集群

才能用上这两个集群带来的便利

本章内容

  1. 为什么需要关注这个问题?
  2. 怎么配置?

记住. 本章是针对redis已经配置了主从集群和哨兵集群的, 而非cluster集群模式

为什么需要关注这个问题?

没有 Redis Sentinel 架构之前,如果主节点挂了,需要运维人员手动进行主从切换,然后更新所有用到的 Redis IP 地址参数再重新启动系统,所有恢复操作都需要人为干预,如果半夜挂了,如果系统很多,如果某个操作搞错了,等等,这对运维人员来说简直就是恶梦。

有了 Redis Sentinel,主从节点故障都是自动化切换,应用程序参数什么也不用改,对于客户端来说都是透明无缝切换的,运维人员再也不用担惊受怕了。

怎么配置?

看看源码分析分析

我们需要注意redis中springboot的这个接口

image.png

也就是RedisConnectionFactory这个接口

image.png

可以看到三个函数, 分别拿到

  • redis集群的Connection
  • 单机redis的Connection
  • 哨兵方式的Connection

如果你写过springboot整合redis的hello world 项目的话, 你会发现, springboot默认使用

LettuceConnectionFactory

同时我们通过在 application.yml 上的 spring.redis 字符串找到

image.png

org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration

可以看到下面的这个Bean配置

image.png

优先级已经决定了, sentinel > cluster > 单机 方式

进入getSentinelConfig函数

protected final RedisSentinelConfiguration getSentinelConfig() {
   if (this.sentinelConfiguration != null) {
      return this.sentinelConfiguration;
   }
   RedisProperties.Sentinel sentinelProperties = this.properties.getSentinel();
   if (sentinelProperties != null) {
      RedisSentinelConfiguration config = new RedisSentinelConfiguration();
      config.master(sentinelProperties.getMaster());
      config.setSentinels(createSentinels(sentinelProperties));
      config.setUsername(this.properties.getUsername());
      if (this.properties.getPassword() != null) {
         config.setPassword(RedisPassword.of(this.properties.getPassword()));
      }
      config.setSentinelUsername(sentinelProperties.getUsername());
      if (sentinelProperties.getPassword() != null) {
         config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
      }
      config.setDatabase(this.properties.getDatabase());
      return config;
   }
   return null;
}

上面可以看到 password 和 sentinelPassword 是可选的

跳到RedisProperties.Sentinel 就看到

image.png

image.png

配置

sentinel:
  master: mymaster
  password: 123456
  nodes:
    - 192.168.7.5:26379
    - 192.168.7.6:26380
    - 192.168.7.7:26381

完整application.yml

server:
  port: 8081
spring:
  application:
    name: redis-data-demo
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/hmdp?useSSL=false&serverTimezone=UTC
    username: root
    password: 123456
  redis:
    host: 127.0.0.1
    port: 6379
    lettuce:
      pool:
        max-active: 10
        max-idle: 10
        min-idle: 1
        time-between-eviction-runs: 10s
        enabled: true
    sentinel:
      master: mymaster
      password: 123456
      nodes:
        - 192.168.7.5:26379
        - 192.168.7.6:26380
        - 192.168.7.7:26381
  jackson:
    default-property-inclusion: non_null
    date-format: yyyy-MM-dd HH:mm:ss
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
    deserialization:
      READ_DATE_TIMESTAMPS_AS_NANOSECONDS: false
  #  cache:
  #    type: redis
  #    redis:
  #      time-to-live: 1800 # 全局 key 过期时间
  #      cache-null-values: true
  main:
    allow-circular-references: true
  rabbitmq:
    host: 127.0.0.1
    username: zhazha
    password: 123456
    virtual-host: /
    listener:
      simple:
        acknowledge-mode: manual
        retry:
          enabled: true
          initial-interval: 300
          max-attempts: 3
          max-interval: 1000
    publisher-returns: true
    publisher-confirm-type: correlated
mybatis-plus:
  type-aliases-package: com.zhazha.entity
  global-config:
    banner: off
logging:
  level:
    com.zhazha: debug

在项目的启动类中,添加一个新的bean:

@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
    return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}

这个bean中配置的就是读写策略,包括四种:

  • MASTER:从主节点读取
  • MASTER_PREFERRED:优先从master节点读取,master不可用才读取replica
  • REPLICA:从slave(replica)节点读取
  • REPLICA_PREFERRED:优先从slave(replica)节点读取,所有的slave都不可用才读取master

其他操作不需要修改

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值