springboot集成redis哨兵集群的實現示例
前言
redis主從集群和redis sentinel集群都配置完畢了, 現在我們需要了解spring boot 如何連接上該集群
才能用上這兩個集群帶來的便利
本章內容
- 為什么需要關注這個問題?
- 怎么配置?
記住. 本章是針對redis已經配置了主從集群和哨兵集群的, 而非cluster集群模式
為什么需要關注這個問題?
沒有 Redis Sentinel 架構之前,如果主節(jié)點掛了,需要運維人員手動進行主從切換,然后更新所有用到的 Redis IP 地址參數再重新啟動系統,所有恢復操作都需要人為干預,如果半夜掛了,如果系統很多,如果某個操作搞錯了,等等,這對運維人員來說簡直就是惡夢。
有了 Redis Sentinel,主從節(jié)點故障都是自動化切換,應用程序參數什么也不用改,對于客戶端來說都是透明無縫切換的,運維人員再也不用擔驚受怕了。
怎么配置?
看看源碼分析分析
我們需要注意redis中springboot的這個接口

也就是RedisConnectionFactory這個接口

可以看到三個函數, 分別拿到
- redis集群的Connection
- 單機redis的Connection
- 哨兵方式的Connection
如果你寫過springboot整合redis的hello world 項目的話, 你會發(fā)現, springboot默認使用
LettuceConnectionFactory 類
同時我們通過在 application.yml 上的 spring.redis 字符串找到

org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration
可以看到下面的這個Bean配置

優(yōu)先級已經決定了, 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 就看到


配置
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
其他操作不需要修改
到此這篇關于springboot集成redis哨兵集群的實現示例的文章就介紹到這了,更多相關springboot redis哨兵集群內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在JDK和Eclipse下如何編寫和運行Java Applet
本文主要介紹了在JDK和Eclipse的環(huán)境下如何編寫和運行Java Applet,圖文方式,適合初學者學習。2015-09-09
SpringBoot中@ControllerAdvice注解的使用方法
這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個增強的?Controller,對controller層做異常處理、數據預處理、全局數據綁定,?springboot?會自動掃描到,不需要調用,這個注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下2024-01-01
Spring?Security權限管理實現接口動態(tài)權限控制
這篇文章主要為大家介紹了Spring?Security權限管理實現接口動態(tài)權限控制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06

