jedis-sentinel principle analysis

principle

Sentinel connected to the client through the cluster by sending Protocol.SENTINEL_GET_MASTER_ADDR_BY_NAME command from the machine sentinel

After asking for information master node, the master node to get the ip and port number, and then the client initiates a connection. After the connection, the client needs to build

Li sensing mechanism, when the master re-election, the clients need to connect to the new master node

Private HostAndPort initSentinels (the Set <String> sentinels, Final String masterName) { 
HostAndPort Master = null ;
 Boolean sentinelAvailable = to false ; 
log.info ( "Master from the Trying to Find Available Sentinels ..." );
 // plurality sentinels, these traversing a Sentinels 
for (String Sentinel: Sentinels) {
 // Host: Sentinel address into a port represented HostAndPort object. 
Final HostAndPort HAP = HostAndPort.parseString (Sentinel); 
log.fine ( "Connecting to the Sentinel" + HAP); 
Jedis jedis = null ;
 the try {
// 连接到sentinel
jedis = new Jedis(hap.getHost(), hap.getPort());
// 根据masterName得到master的地址,返回一个list,host= list[0], port =// list[1]
List<String> masterAddr = jedis.sentinelGetMasterAddrByName(masterName);
// connected to sentinel...
sentinelAvailable = true;if (masterAddr == null || masterAddr.size() != 2) {
log.warning("Can not get master addr, master name: " + masterName + ".
Sentinel: " + hap
+ ".");
continue;
}
// 如果在任何一个sentinel中找到了master,不再遍历sentinels
master = toHostAndPort(masterAddr);
log.fine("Found Redis master at " + master);
break;
} catch (JedisException e) {
// resolves #1036, it should handle JedisException there's another chance
// of raising JedisDataException
log.warning("Cannot get master address from sentinel running @ " + hap + ".
Reason: " + e
+ ". Trying next one.");
} finally {
if (jedis != null) {
jedis.close();
}
}
}
//Here, if the master is null, it indicates that there are two cases, one is all the sentinels nodes is down, one is the master section 
point is not viable sentinels to monitor
 IF (master == null ) {
 IF ( sentinelAvailable) {
 // CAN Connect to Sentinel, But Not to Seems name Master
 // Monitored 
the throw  new new JedisException ( "Can Connect to Sentinel, But" + masterName
 + "to BE Seems Not Monitored ..." ); 
} the else {
 the throw  new new JedisConnectionException ( "All Sentinels Down, CAN not the Determine the WHERE IS 
"
 + masterName + "Master IS running ..." ); 
} 
} 
//If you come here, the instructions to locate the address of the master of 
log.info ( "Redis master running AT" + master + ", in the Listeners Starting Sentinel ..." );
 // start listening to each sentinels of 
// for each sentinel We have launched a listener MasterListener. MasterListener itself is a thread, it will go to the subscription sentinel 
on the news about the master node address change. 
for (String Sentinel: Sentinels) {
 Final HostAndPort HAP = HostAndPort.parseString (Sentinel); 
MasterListener masterListener = new new MasterListener (masterName, hap.getHost (), 
hap.getPort ()); 
// Whether or Not MasterListener Threads are Alive, CAN BE stopped Process 
masterListener.setDaemon ( to true ); 
masterListeners.add (masterListener);
masterListener.start (); 
} 
return master; 
} 
node obtains information from the master methods Sentinel

public List<String> sentinelGetMasterAddrByName(String masterName) {

 
 

client.sentinel(Protocol.SENTINEL_GET_MASTER_ADDR_BY_NAME, masterName);

 
 

final List<Object> reply = client.getObjectMultiBulkReply();

 
 

return BuilderFactory.STRING_LIST.build(reply);

 
 

}

 

 

Guess you like

Origin www.cnblogs.com/yintingting/p/11701432.html