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

也就是RedisConnectionFactory這個(gè)接口

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

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

優(yōu)先級(jí)已經(jīng)決定了, sentinel > cluster > 單機(jī) 方式
進(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 過期時(shí)間
# 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在項(xiàng)目的啟動(dòng)類中,添加一個(gè)新的bean:
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}這個(gè)bean中配置的就是讀寫策略,包括四種:
- MASTER:從主節(jié)點(diǎn)讀取
- MASTER_PREFERRED:優(yōu)先從master節(jié)點(diǎn)讀取,master不可用才讀取replica
- REPLICA:從slave(replica)節(jié)點(diǎn)讀取
- REPLICA_PREFERRED:優(yōu)先從slave(replica)節(jié)點(diǎn)讀取,所有的slave都不可用才讀取master
其他操作不需要修改
到此這篇關(guān)于springboot集成redis哨兵集群的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot redis哨兵集群內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Random隨機(jī)數(shù)生成雙色球號(hào)碼
使用Random類是Java中用于生成隨機(jī)數(shù)的標(biāo)準(zhǔn)類,本文主要介紹了Java實(shí)現(xiàn)Random隨機(jī)數(shù)生成雙色球號(hào)碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例
這篇文章主要介紹了java實(shí)現(xiàn)socket從服務(wù)器連續(xù)獲取消息的示例,需要的朋友可以參考下2014-04-04
Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Activiti進(jìn)階之組任務(wù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作
這篇文章主要介紹了在IDEA中 實(shí)現(xiàn)給main方法附帶參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼
本文以Java代碼為例介紹如何實(shí)現(xiàn)將彩色PDF文件轉(zhuǎn)為灰度(黑白)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-03-03
如何解決java.util.zip.ZipFile解壓后被java占用問題
這篇文章主要介紹了如何解決java.util.zip.ZipFile解壓后被java占用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringBoot內(nèi)部調(diào)用事務(wù)不起作用問題的解決方案
這篇文章主要介紹了SpringBoot事務(wù)不起作用問題的解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
詳解如何在SpringBoot中實(shí)現(xiàn)優(yōu)雅關(guān)閉
這篇文章主要介紹了如何在SpringBoot中實(shí)現(xiàn)優(yōu)雅關(guān)閉,SpringBoot應(yīng)用程序的關(guān)閉可以是崩潰,也可以是手動(dòng)關(guān)閉的,Shutdown、Crash 和 Graceful 之間的區(qū)別在于,它控制決定了我們可以用這個(gè)事件做什么,本文中,一起研究下Spring Boot提供的開箱即用功能之一:優(yōu)雅關(guān)閉2024-09-09

