jedis src代码MasterListener方法运行:
j.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
log.fine("Sentinel " + host + ":" + port + " published: " + message + ".");
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
if (masterName.equals(switchMasterMsg[0])) {
initPool(toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
} else {
log.fine("Ignoring message on +switch-master for master name "
+ switchMasterMsg[0] + ", our master name is " + masterName);
}
} else {
log.severe("Invalid message received on Sentinel " + host + ":" + port
+ " on channel +switch-master: " + message);
}
}
}, "+switch-master");
如果有三个Sentinels,那么创建三个MasterListener。当发生故障转移时,jedis客户端将为每个MasterListener三次initPool。
问题是:为什么不只是initPool一次?当sentinel目标离线高手时,那么jedis客户端接收到重新initPool的消息?
答案 0 :(得分:0)
我认为此行需要同步" if(!master.equals(currentHostMaster)){"防止多次创建连接池。
public class JedisSentinelPool extends JedisPoolAbstract {
private volatile HostAndPort currentHostMaster;
private void initPool(HostAndPort master) {
if (!master.equals(currentHostMaster)) {
currentHostMaster = master;
if (factory == null) {
}