springboot如何集成redis哨兵集群?

您所在的位置:网站首页 redis哨兵模式哨兵挂了 springboot如何集成redis哨兵集群?

springboot如何集成redis哨兵集群?

2023-05-12 09:42| 来源: 网络整理| 查看: 265

前言

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

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

本章内容 为什么需要关注这个问题? 怎么配置?

记住. 本章是针对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

其他操作不需要修改



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3