Springboot Redis?哨兵模式的實(shí)現(xiàn)示例
Redis配置
redis.conf配置
由于服務(wù)器資源有限,我這里通過(guò)配置不同端口,模擬Redis集群,redis-server占用6379、6380、6381端口,redis-sentinel占用26379、26380、26381端口。
首先準(zhǔn)備3份redis配置文件,分別取名redis6379.conf,redis6380.conf,redis6381.conf
redis6379.conf,6379端口作為master,需要修改的配置如下
protected-mode no #設(shè)置為NO,其他IP才可以訪(fǎng)問(wèn) port 6379 #端口 daemonize yes pidfile "/var/run/redis_6379.pid" logfile "6379.log" #日志,若redis分布在不同服務(wù)器,不用修改 dbfilename "dump6379.rdb" #數(shù)據(jù),若redis分布在不同服務(wù)器,不用修改 masterauth "admin123/*-" #從節(jié)點(diǎn)訪(fǎng)問(wèn)主節(jié)點(diǎn)時(shí)需要的密碼 requirepass "admin123/*-" #redis密碼,應(yīng)用訪(fǎng)問(wèn)redis時(shí)需要
redis6380.conf,6380端口作為slave,需要設(shè)置主節(jié)點(diǎn)ip和port,需要修改的配置如下:
protected-mode no #設(shè)置為NO,其他IP才可以訪(fǎng)問(wèn) port 6380 #端口 daemonize yes pidfile "/var/run/redis_6380.pid" logfile "6380.log" #日志,若redis分布在不同服務(wù)器,不用修改 dbfilename "dump6380.rdb" #數(shù)據(jù),若redis分布在不同服務(wù)器,不用修改 replicaof 192.168.1.1 6379 #標(biāo)記主節(jié)點(diǎn)ip+端口,IP設(shè)置為自己服務(wù)器IP地址即可 masterauth "admin123/*-" #從節(jié)點(diǎn)訪(fǎng)問(wèn)主節(jié)點(diǎn)時(shí)需要的密碼 requirepass "admin123/*-" #redis密碼,應(yīng)用訪(fǎng)問(wèn)redis時(shí)需要
備注:slaveof 192.168.1.1 6379,這里需要設(shè)置成你自己的IP,可實(shí)現(xiàn)Redis 復(fù)制功能
redis6381.conf,6381端口作為slave,同上,需要修改的配置如下:
protected-mode no #設(shè)置為NO,其他IP才可以訪(fǎng)問(wèn) port 6381 #端口 daemonize yes pidfile "/var/run/redis_6381.pid" logfile "6381.log" #日志,若redis分布在不同服務(wù)器,不用修改 dbfilename "dump6381.rdb" #數(shù)據(jù),若redis分布在不同服務(wù)器,不用修改 replicaof 192.168.1.1 6379 #標(biāo)記主節(jié)點(diǎn)ip+端口,IP設(shè)置為自己服務(wù)器IP地址即可 masterauth "admin123/*-" #從節(jié)點(diǎn)訪(fǎng)問(wèn)主節(jié)點(diǎn)時(shí)需要的密碼 requirepass "admin123/*-" #redis密碼,應(yīng)用訪(fǎng)問(wèn)redis時(shí)需要
sentinel.conf配置
準(zhǔn)備3分sentinel.conf配置文件,分別為sentinel26379.conf、sentinel26380.conf、sentinel26381.conf。
sentinel26379.conf,26379端口作為哨兵1,需要修改配置如下
port 26379 #端口 daemonize yes pidfile "/var/run/redis-sentinel26379.pid" logfile "/usr/local/bin/sentinelLogs/sentinel_26379.log" #配置指示 Sentinel 去監(jiān)視一個(gè)名為 mymaster 的主節(jié)點(diǎn), 主節(jié)點(diǎn)的 IP 地址為 127.0.0.1 , 端口號(hào)為 6379 , #而將這個(gè)主服務(wù)器判斷為失效至少需要 2 個(gè) Sentinel 同意 (只要同意 Sentinel 的數(shù)量不達(dá)標(biāo),自動(dòng)故障遷移就不會(huì)執(zhí)行) sentinel monitor mymaster 192.168.1.1 6379 2 sentinel auth-pass mymaster admin123/*- #哨兵訪(fǎng)問(wèn)主節(jié)點(diǎn)密碼 protected-mode no
sentinel26380.conf,26380端口作為哨兵2,需要修改配置如下
port 26380 #端口 daemonize yes pidfile "/var/run/redis-sentinel26380.pid" logfile "/usr/local/bin/sentinelLogs/sentinel_26380.log" #配置指示 Sentinel 去監(jiān)視一個(gè)名為 mymaster 的主節(jié)點(diǎn), 主節(jié)點(diǎn)的 IP 地址為 127.0.0.1 , 端口號(hào)為 6379 , #而將這個(gè)主服務(wù)器判斷為失效至少需要 2 個(gè) Sentinel 同意 (只要同意 Sentinel 的數(shù)量不達(dá)標(biāo),自動(dòng)故障遷移就不會(huì)執(zhí)行) sentinel monitor mymaster 192.168.1.1 6379 2 sentinel auth-pass mymaster admin123/*- #哨兵訪(fǎng)問(wèn)主節(jié)點(diǎn)密碼 protected-mode no
sentinel26381.conf,26381端口作為哨兵3,需要修改配置如下
port 26381 #端口 daemonize yes pidfile "/var/run/redis-sentinel26381.pid" logfile "/usr/local/bin/sentinelLogs/sentinel_26381.log" #配置指示 Sentinel 去監(jiān)視一個(gè)名為 mymaster 的主節(jié)點(diǎn), 主節(jié)點(diǎn)的 IP 地址為 127.0.0.1 , 端口號(hào)為 6379 , #而將這個(gè)主服務(wù)器判斷為失效至少需要 2 個(gè) Sentinel 同意 (只要同意 Sentinel 的數(shù)量不達(dá)標(biāo),自動(dòng)故障遷移就不會(huì)執(zhí)行) sentinel monitor mymaster 192.168.1.1 6379 2 sentinel auth-pass mymaster admin123/*- #哨兵訪(fǎng)問(wèn)主節(jié)點(diǎn)密碼 protected-mode no
啟動(dòng)redis-server,redis-sentinel
[root@VM-20-5-centos bin]# redis-server redis6379.conf [root@VM-20-5-centos bin]# redis-server redis6380.conf [root@VM-20-5-centos bin]# redis-server redis6381.conf [root@VM-20-5-centos bin]# redis-sentinel sentinel_26379.conf [root@VM-20-5-centos bin]# redis-sentinel sentinel_26380.conf [root@VM-20-5-centos bin]# redis-sentinel sentinel_26381.conf
查看狀態(tài),如下圖所示,即為啟動(dòng)成功
[root@VM-20-5-centos mconfig]# redis-cli -p 26379
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.1.1"
5) "port"
6) "6379"
7) "runid"
127.0.0.1:26379> sentinel slaves mymaster
1) 1) "name"
2) "192.168.1.1:6381"
3) "ip"
4) "192.168.1.1"
5) "port"
6) "6381"
7) "runid"
8) ""
2) 1) "name"
2) "192.75.218.240:6380"
3) "ip"
4) "192.75.218.240"
5) "port"
127.0.0.1:26379>
Springboot整合
導(dǎo)入包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
properties配置
spring.redis.host=192.168.1.1 spring.redis.port=6379 spring.redis.password=admin123/*- spring.redis.sentinel.master=mymaster spring.redis.sentinel.password=admin123/*- spring.redis.sentinel.nodes=192.168.1.1:26379,192.168.1.1:26380,192.168.1.1:26381
測(cè)試
代碼示例
package com.mx.learnredis.controller;
?
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
?
import java.util.Date;
?
@SpringBootTest
class UserControllerTest {
?
? ? @Autowired
? ? @Qualifier("redisTemplate")
? ? private RedisTemplate redisTemplate;
?
? ? @Test
? ? public void test() throws InterruptedException {
? ? ? ? redisTemplate.opsForValue().set("user", "xx");
? ? ? ? while (true)
? ? ? ? {
? ? ? ? ? ? Thread.sleep(2000);
? ? ? ? ? ? System.out.println("time "+new Date()+":" +redisTemplate.opsForValue().get("user"));
? ? ? ? }
? ? }
}模擬redis宕機(jī)
idea控制臺(tái)輸出:
time Sat Jan 08 20:09:12 CST 2022:xx
time Sat Jan 08 20:09:14 CST 2022:xx
time Sat Jan 08 20:09:16 CST 2022:xx
time Sat Jan 08 20:09:18 CST 2022:xx
time Sat Jan 08 20:09:20 CST 2022:xx
time Sat Jan 08 20:09:22 CST 2022:xx
關(guān)閉掉主節(jié)點(diǎn),登錄linux服務(wù)器
[root@VM-20-5-centos mconfig]# ps -ef|grep redis root ? ? 27881 ? ? 1 ?0 17:19 ? ? ? ? ?00:00:12 redis-server *:6380 root ? ? 27997 ? ? 1 ?0 17:20 ? ? ? ? ?00:00:17 redis-sentinel *:26379 [sentinel] root ? ? 28017 ? ? 1 ?0 17:20 ? ? ? ? ?00:00:17 redis-sentinel *:26380 [sentinel] root ? ? 28030 ? ? 1 ?0 17:20 ? ? ? ? ?00:00:17 redis-sentinel *:26381 [sentinel] root ? ? 28471 ? ? 1 ?0 20:13 ? ? ? ? ?00:00:00 redis-server *:6381 root ? ? 28664 ? ? 1 ?0 17:23 ? ? ? ? ?00:00:12 redis-server *:6379 root ? ? 28753 11813 ?0 20:14 pts/1 ? ?00:00:00 grep --color=auto redis [root@VM-20-5-centos mconfig]# kill -9 28664 [root@VM-20-5-centos mconfig]#
idea控制臺(tái)輸出,可看到重連日志,Success
time Sat Jan 08 20:15:02 CST 2022:xx
time Sat Jan 08 20:15:04 CST 2022:xx
2022-01-08 20:15:05.010 INFO 7216 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was /192.168.1.1:6379
2022-01-08 20:15:07.211 WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:11.920 INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:14.081 WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:18.617 INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:20.767 WARN 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:26.119 INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:28.278 WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:33.720 INFO 7216 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:35.880 WARN 7216 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect to [192.168.1.1:6379]: Connection refused: no further information: /192.168.1.1:6379
2022-01-08 20:15:40.020 INFO 7216 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.1:6379
2022-01-08 20:15:40.160 INFO 7216 --- [ioEventLoop-4-4] i.l.core.protocol.ReconnectionHandler : Reconnected to 192.168.1.1:6381
time Sat Jan 08 20:15:06 CST 2022:xx
到此這篇關(guān)于Springboot Redis 哨兵模式的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Springboot Redis 哨兵模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Gateway網(wǎng)關(guān)中配置跨域的三種方案
文章總結(jié):介紹了三種處理跨域請(qǐng)求的方法:在Controller類(lèi)上添加注解、通過(guò)配置類(lèi)實(shí)現(xiàn)重寫(xiě)WebMvcConfigurer接口和在配置文件中統(tǒng)一設(shè)置,希望這些方法能幫助讀者解決跨域問(wèn)題2024-11-11
詳解Spring Boot 使用slf4j+logback記錄日志配置
本篇文章主要介紹了Spring Boot 使用slf4j+logback記錄日志配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Java并發(fā)編程中的volatile關(guān)鍵字詳解
這篇文章主要介紹了Java并發(fā)編程中的volatile關(guān)鍵字詳解,volatile?用于保證我們某個(gè)變量的可見(jiàn)性,使其一直存放在主存中,不被移動(dòng)到某個(gè)線(xiàn)程的私有工作內(nèi)存中,需要的朋友可以參考下2023-08-08
Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹(shù)形(Tree)結(jié)構(gòu)
本文主要介紹了Java實(shí)現(xiàn)平鋪列表(List)互轉(zhuǎn)樹(shù)形(Tree)結(jié)構(gòu),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
SpringBoot 監(jiān)聽(tīng)Redis鍵過(guò)期事件(過(guò)期監(jiān)聽(tīng))
Redis鍵過(guò)期事件是SpringBoot中常用的緩存失效通知方式,通過(guò)配置可以監(jiān)聽(tīng)特定鍵的過(guò)期事件,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
Java編寫(xiě)超時(shí)工具類(lèi)實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于Java編寫(xiě)超時(shí)工具類(lèi)實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02
解決Aop @AfterReturning因返回類(lèi)型不一致導(dǎo)致無(wú)法執(zhí)行切面代碼
這篇文章主要介紹了解決Aop @AfterReturning因返回類(lèi)型不一致導(dǎo)致無(wú)法執(zhí)行切面代碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

