SpringBoot結合Redis哨兵模式的實現

Redis哨兵模式

Redis Sentinel介紹

Redis Sentinel是Redis高可用的實現方案。Sentinel是一個管理多個Redis實例的工具,它能夠實現對Redis的監控、通知、自動故障轉移。java

Redis Sentinel主要功能

Redis 的 Sentinel 系統用於管理多個 Redis 服務器(instance), 該系統執行如下三個任務:node

  • 監控(Monitoring):Sentinel 會不斷地檢查你的主服務器和從服務器是否運做正常。web

  • 提醒(Notification):當被監控的某個 Redis 服務器出現問題時, Sentinel 能夠經過 API 向管理員或者其餘應用程序發送通知。redis

  • 自動故障遷移(Automatic failover):當一個主服務器不能正常工做時, Sentinel 會開始一次自動故障遷移操做, 它會將失效主服務器的其中一個從服務器升級爲新的主服務器, 並讓失效主服務器的其餘從服務器改成複製新的主服務器; 當客戶端試圖鏈接失效的主服務器時, 集羣也會向客戶端返回新主服務器的地址, 使得集羣可使用新主服務器代替失效服務器。spring

Redis Sentinel部署

Redis集羣配置

Redis集羣啓動

複製3個reids.conf配置文件shell

cp redis.conf /home/redis/redis6379.conf
cp redis.conf /home/redis/redis6380.conf
cp redis.conf /home/redis/redis6381.conf
複製代碼

修改reids.conf配置文件,以6379配置爲例apache

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配置文件,新增以下vim

slaveof 192.168.126.200 6379
複製代碼

先啓動master服務,在啓動slave服務bash

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,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節點服務
./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節點,提供讀寫服務

相關文章
相關標籤/搜索