SpringBoot結合Redis哨兵模式的實現示例
阿新 • • 發佈:2020-04-23
Redis哨兵模式
Redis Sentinel介紹
Redis Sentinel是Redis高可用的實現方案。Sentinel是一個管理多個Redis例項的工具,它可以實現對Redis的監控、通知、自動故障轉移。
Redis Sentinel主要功能
Redis 的 Sentinel 系統用於管理多個 Redis 伺服器(instance), 該系統執行以下三個任務:
- 監控(Monitoring):Sentinel 會不斷地檢查你的主伺服器和從伺服器是否運作正常。
- 提醒(Notification):當被監控的某個 Redis 伺服器出現問題時, Sentinel 可以通過 API 向管理員或者其他應用程式傳送通知。
- 自動故障遷移(Automatic failover):當一個主伺服器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主伺服器的其中一個從伺服器升級為新的主伺服器, 並讓失效主伺服器的其他從伺服器改為複製新的主伺服器; 當客戶端試圖連線失效的主伺服器時, 叢集也會向客戶端返回新主伺服器的地址, 使得叢集可以使用新主伺服器代替失效伺服器。
Redis Sentinel部署
Redis叢集配置
Redis叢集啟動
複製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服務,在啟動slave服務
master節點服務 ./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,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節點服務 ./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節點的埠 port 26379 dir "/home/redis/sentinel" daemonize yes logfile "sentinel-26379.log" #當前Sentinel節點監控 127.0.0.1:6379 這個主節點 #代表判斷主節點失敗至少需要2個Sentinel節點節點同意 #mymaster是主節點的別名 sentinel monitor mymaster 192.168.126.200 6380 2 #每個Sentinel節點都要定期PING命令來判斷Redis資料節點和其餘Sentinel節點是否可達,如果超過30000毫秒且沒有回覆,則判定不可達 sentinel down-after-milliseconds mymaster 30000 #當Sentinel節點集合對主節點故障判定達成一致時,Sentinel領導者節點會做故障轉移操作,選出新的主節點,原來的從節點會向新的主節點發起復制操作,限制每次向>新的主節點發起復制操作的從節點個數為1 sentinel leader-epoch mymaster 1 #故障轉移超時時間為180000毫秒 sentinel failover-timeout mymaster 180000 #同理建立修改sentinel2.conf、sentinel3.conf配置檔案
啟動3臺sentinel服務
./redis-sentinel /home/redis/sentinel1.conf ./redis-sentinel /home/redis/sentinel2.conf ./redis-sentinel /home/redis/sentinel3.conf
SpringBoot結合Redis哨兵模式
建立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: # 連線池最大連線數(使用負值表示沒有限制) 預設為8 max-active: 8 # 連線池中的最大空閒連線 預設為8 max-idle: 8 # 連線池最大阻塞等待時間(使用負值表示沒有限制) 預設為-1 max-wait: -1ms # 連線池中的最小空閒連線 預設為 0 min-idle: 0 sentinel: # 主節點的別名 master: mymaster # sentinel服務的ip和埠 nodes: 192.168.126.200:26379,192.168.126.200:26380,192.168.126.200:26381
程式呼叫
@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服務後,後臺會嘗試重連,當超過最大等待時間,無法連線後,sentinel會重新選舉出一個新的master,程式獲取到新的master節點,提供讀寫服務
到此這篇關於SpringBoot結合Redis哨兵模式的實現示例的文章就介紹到這了,更多相關SpringBoot結合Redis哨兵模式內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!