SpringBoot結(jié)合Redis哨兵模式的實現(xiàn)示例
Redis哨兵模式
Redis Sentinel介紹
Redis Sentinel是Redis高可用的實現(xiàn)方案。Sentinel是一個管理多個Redis實例的工具,它可以實現(xiàn)對Redis的監(jiān)控、通知、自動故障轉(zhuǎn)移。
Redis Sentinel主要功能
Redis 的 Sentinel 系統(tǒng)用于管理多個 Redis 服務(wù)器(instance), 該系統(tǒng)執(zhí)行以下三個任務(wù):
- 監(jiān)控(Monitoring):Sentinel 會不斷地檢查你的主服務(wù)器和從服務(wù)器是否運作正常。
- 提醒(Notification):當(dāng)被監(jiān)控的某個 Redis 服務(wù)器出現(xiàn)問題時, Sentinel 可以通過 API 向管理員或者其他應(yīng)用程序發(fā)送通知。
- 自動故障遷移(Automatic failover):當(dāng)一個主服務(wù)器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主服務(wù)器的其中一個從服務(wù)器升級為新的主服務(wù)器, 并讓失效主服務(wù)器的其他從服務(wù)器改為復(fù)制新的主服務(wù)器; 當(dāng)客戶端試圖連接失效的主服務(wù)器時, 集群也會向客戶端返回新主服務(wù)器的地址, 使得集群可以使用新主服務(wù)器代替失效服務(wù)器。
Redis Sentinel部署
Redis集群配置
Redis集群啟動
復(fù)制3個reids.conf配置文件
cp redis.conf /home/redis/redis6379.conf cp redis.conf /home/redis/redis6380.conf cp redis.conf /home/redis/redis6381.conf
修改reids.conf配置文件,以6379配置為例
vim redis6379.conf #啟用后臺啟動 daemonize yes #pidfile位置 pidfile "/home/redis/6379/redis6379.pid" #端口 port 6379 #日志文件位置 logfile "/home/redis/6379/log6379.log" #rdb備份文件名稱 dbfilename "dump6379.rdb" #rdb備份文件路徑 dir "/home/redis/rdb/"
修改redis-slave配置文件,修改和master如上配置,6380、6381配置文件,新增如下
slaveof 192.168.126.200 6379
先啟動master服務(wù),在啟動slave服務(wù)
master節(jié)點服務(wù) ./redis-cli -p 6379 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.126.200,port=6380,state=online,offset=975350,lag=1 slave1:ip=192.168.126.200,port=6381,state=online,offset=975350,lag=1 master_repl_offset:975495 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:975494 slave節(jié)點服務(wù) ./redis-cli -p 6380 # Replication role:slave master_host:192.168.126.200 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:989499 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
sentinel集群配置
編寫sentinel配置文件,master配置
touch sentinel1.conf vim sentinel1.conf #Sentinel節(jié)點的端口 port 26379 dir "/home/redis/sentinel" daemonize yes logfile "sentinel-26379.log" #當(dāng)前Sentinel節(jié)點監(jiān)控 127.0.0.1:6379 這個主節(jié)點 #代表判斷主節(jié)點失敗至少需要2個Sentinel節(jié)點節(jié)點同意 #mymaster是主節(jié)點的別名 sentinel monitor mymaster 192.168.126.200 6380 2 #每個Sentinel節(jié)點都要定期PING命令來判斷Redis數(shù)據(jù)節(jié)點和其余Sentinel節(jié)點是否可達(dá),如果超過30000毫秒且沒有回復(fù),則判定不可達(dá) sentinel down-after-milliseconds mymaster 30000 #當(dāng)Sentinel節(jié)點集合對主節(jié)點故障判定達(dá)成一致時,Sentinel領(lǐng)導(dǎo)者節(jié)點會做故障轉(zhuǎn)移操作,選出新的主節(jié)點,原來的從節(jié)點會向新的主節(jié)點發(fā)起復(fù)制操作,限制每次向>新的主節(jié)點發(fā)起復(fù)制操作的從節(jié)點個數(shù)為1 sentinel leader-epoch mymaster 1 #故障轉(zhuǎn)移超時時間為180000毫秒 sentinel failover-timeout mymaster 180000 #同理創(chuàng)建修改sentinel2.conf、sentinel3.conf配置文件
啟動3臺sentinel服務(wù)
./redis-sentinel /home/redis/sentinel1.conf ./redis-sentinel /home/redis/sentinel2.conf ./redis-sentinel /home/redis/sentinel3.conf
SpringBoot結(jié)合Redis哨兵模式
創(chuàng)建SpringBoot項目,添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--redis連接池--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
核心配置文件application.yml
server: port: 80 spring: redis: lettuce: pool: # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn)為8 max-active: 8 # 連接池中的最大空閑連接 默認(rèn)為8 max-idle: 8 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) 默認(rèn)為-1 max-wait: -1ms # 連接池中的最小空閑連接 默認(rèn)為 0 min-idle: 0 sentinel: # 主節(jié)點的別名 master: mymaster # sentinel服務(wù)的ip和端口 nodes: 192.168.126.200:26379,192.168.126.200:26380,192.168.126.200:26381
程序調(diào)用
@RestController @RequestMapping("/redis") public class RedisController { // 使用SpringBoot封裝的RestTemplate對象 @Autowired RedisTemplate<String, String> redisTemplate; @RequestMapping("/get") public String get(String key) { String value = redisTemplate.opsForValue().get(key); return value; } @RequestMapping("/set") public String set(String key, String value) { redisTemplate.opsForValue().set(key, value); return "success"; } }
模擬故障
手動shutdown redis的master服務(wù)后,后臺會嘗試重連,當(dāng)超過最大等待時間,無法連接后,sentinel會重新選舉出一個新的master,程序獲取到新的master節(jié)點,提供讀寫服務(wù)
到此這篇關(guān)于SpringBoot結(jié)合Redis哨兵模式的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot結(jié)合Redis哨兵模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法示例
這篇文章主要介紹了Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法,結(jié)合具體實例形式分析了java的遍歷、遞歸、回溯等算法實現(xiàn)八皇后問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Mybatis-plus實現(xiàn)主鍵自增和自動注入時間的示例代碼
這篇文章主要介紹了Mybatis-plus實現(xiàn)主鍵自增和自動注入時間的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java8新特性之重復(fù)注解(repeating annotations)淺析
這篇文章主要介紹了Java8新特性之重復(fù)注解(repeating annotations)淺析,這個新特性只是修改了程序的可讀性,是比較小的一個改動,需要的朋友可以參考下2014-06-06mybatis plus開發(fā)過程中遇到的問題記錄及解決
這篇文章主要介紹了mybatis plus開發(fā)過程中遇到的問題記錄及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07SpringCloud中的Feign遠(yuǎn)程調(diào)用接口傳參失敗問題
這篇文章主要介紹了SpringCloud中的Feign遠(yuǎn)程調(diào)用接口傳參失敗問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03springboot druid數(shù)據(jù)庫配置密碼加密的實現(xiàn)
Druid是阿里開發(fā)的數(shù)據(jù)庫連接池,本文主要介紹了springboot druid數(shù)據(jù)庫配置密碼加密的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06