1. 程式人生 > >企業級Redis開發運維從入門到實踐 (25)— Redis Sentinel(哨兵)的客戶端連線

企業級Redis開發運維從入門到實踐 (25)— Redis Sentinel(哨兵)的客戶端連線

客戶端連線

請求響應流程

既然已經實現高可用為什麼不直接直連? 高可用涉及的是服務高可用、完成自動的故障轉移;故障轉移後客戶端無法感知將無法保證正常的使用。 需要保證的是服務高可用客戶端高可用

客戶端實現基本原理
  1. 客戶端實現基本原理 - step1:獲取所有的Sentinel的節點和MasterName,遍歷Sentinel集合得到一個可用(即可以ping通)的Sentinel節點。 在這裡插入圖片描述

  2. 客戶端實現基本原理 - step2:向可用的Sentinel節點發送sentinel的get-master-addr-by-name的請求,引數masterName;獲取master節點資訊。 在這裡插入圖片描述

  3. 客戶端實現基本原理 - step3:客戶端獲取得到master節點後會執行一次role或者role replication來驗證是否是master節點。 在這裡插入圖片描述

  4. 客戶端實現基本原理 - step4:master節點發生變化,sentinel是感知的(所有的故障發現、轉移是由sentinel做的); sentinel怎麼通知client的呢? 內部是一個釋出訂閱的模式,client訂閱sentinel的某一個頻道,該頻道里由誰是master的資訊,假如由變化sentinel就在頻道里publish一條訊息,client訂閱就可以獲取到資訊,通過新的master資訊進行連線。 在這裡插入圖片描述

下圖就是客戶端實現基本原理: 在這裡插入圖片描述

客戶端接入流程
  1. Sentinel 地址集合
  2. masterName
  3. 不是代理模式
jedis
  • JedisSentinelPool不是連線Sentinel節點集合的連線池
  • 本質上還是連線master
  • 只是跟JedisPool進行區分

JedisSentinelPool sentinelPool = new JedisSentinelPool(masterName, sentinelSet, poolConfig, timeout);
Jedis jedis = null;
try {
	jedis = redisSentinelPool.getResource();
	//jedis command
} catch(Exception e) { logger.error(e.getMessage(), e); }finally { if(jedis != null) { jedis.close(); } }