欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot集成redis哨兵集群的實現(xiàn)示例

 更新時間:2023年08月06日 11:16:39   作者:渣渣→_→  
本文主要介紹了springboot集成redis哨兵集群的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

redis主從集群和redis sentinel集群都配置完畢了, 現(xiàn)在我們需要了解spring boot 如何連接上該集群

才能用上這兩個集群帶來的便利

本章內(nèi)容

  • 為什么需要關(guān)注這個問題?
  • 怎么配置?

記住. 本章是針對redis已經(jīng)配置了主從集群和哨兵集群的, 而非cluster集群模式

為什么需要關(guān)注這個問題?

沒有 Redis Sentinel 架構(gòu)之前,如果主節(jié)點掛了,需要運維人員手動進(jìn)行主從切換,然后更新所有用到的 Redis IP 地址參數(shù)再重新啟動系統(tǒng),所有恢復(fù)操作都需要人為干預(yù),如果半夜掛了,如果系統(tǒng)很多,如果某個操作搞錯了,等等,這對運維人員來說簡直就是惡夢。

有了 Redis Sentinel,主從節(jié)點故障都是自動化切換,應(yīng)用程序參數(shù)什么也不用改,對于客戶端來說都是透明無縫切換的,運維人員再也不用擔(dān)驚受怕了。

怎么配置?

看看源碼分析分析

我們需要注意redis中springboot的這個接口

也就是RedisConnectionFactory這個接口

可以看到三個函數(shù), 分別拿到

  • redis集群的Connection
  • 單機redis的Connection
  • 哨兵方式的Connection

如果你寫過springboot整合redis的hello world 項目的話, 你會發(fā)現(xiàn), springboot默認(rèn)使用

LettuceConnectionFactory

同時我們通過在 application.yml 上的 spring.redis 字符串找到

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

可以看到下面的這個Bean配置

優(yōu)先級已經(jīng)決定了, sentinel > cluster > 單機 方式

進(jìn)入getSentinelConfig函數(shù)

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 就看到

配置

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:從主節(jié)點讀取
  • MASTER_PREFERRED:優(yōu)先從master節(jié)點讀取,master不可用才讀取replica
  • REPLICA:從slave(replica)節(jié)點讀取
  • REPLICA_PREFERRED:優(yōu)先從slave(replica)節(jié)點讀取,所有的slave都不可用才讀取master

其他操作不需要修改

到此這篇關(guān)于springboot集成redis哨兵集群的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot redis哨兵集群內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論