有人会说这有什么难的,已经知道了主节点的ip地址和端口,用对应编程语言的客户端连接主节点不就可以了吗?但试想一下,如果这样使用客户端,客户端连接Redis Sentinel和主从复制的Redis又有什么区别呢,如果主节点挂掉了,虽然Redis Sentinel可以完成故障转移,但是客户端无法获取这个变化,那么使用Redis Sentinel的意义就不大了,所以各个语言的客户端需要对Redis Sentinel进行显式的支持。
实际上最了解主节点信息的就是Sentinel节点集合,而各个主节点可以通过<master-name>进行标识的,所以,无论是哪种编程语言的客户端,如果需要正确地连接Redis Sentinel,必须有Sentinel节点集合和masterName两个参数。
Redis Sentinel客户端基本实现原理
1)遍历Sentinel节点集合获取一个可用的Sentinel节点,后面会介绍Sentinel节点之间可以共享数据,所以从任意一个Sentinel节点获取主节点信息都是可以的
2)通过sentinel get-master-addr-by-name master-name这个API来获取对应主节点的相关信息
3)验证当前获取的“主节点”是真正的主节点,这样做的目的是为了防止故障转移期间主节点的变化
4)保持和Sentinel节点集合的“联系”,时刻获取关于主节点的相关“信息”
从上面的模型可以看出,Redis Sentinel客户端只有在初始化和切换主节点时需要和Sentinel节点集合进行交互来获取主节点信息,所以在设计客户端时需要将Sentinel节点集合考虑成配置(相关节点信息和变化)发现服务。
Java操作Redis Sentinel
需要有masterName和Sentinel节点集合两个参数。我们介绍了Jedis的连接池JedisPool,为了不与之相混淆,Jedis针对Redis Sentinel给出了一个JedisSentinelPool,很显然这个连接池保存的连接还是针对主节点的。Jedis给出很多构造方法
public JedisSentinelPool(String masterName, Set<String> sentinels,
final GenericObjectPoolConfig poolConfig, final int connectionTimeout,
final int soTimeout,
final String password, final int database,
final String clientName)
masterName——主节点名
sentinels——Sentinel节点集合
poolConfig——common-pool连接池配置
connectTimeout——连接超时
soTimeout——读写超时
password——主节点密码
database——当前数据库索引
clientName——客户端名
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName,
sentinelSet, poolConfig, timeout);
此时timeout既代表连接超时又代表读写超时,password为空,database默认使用0,clientName为空
private HostAndPort initSentinels(Set<String> sentinels, final String masterName) {
// 主节点
HostAndPort master = null;
// 遍历所有sentinel节点
for (String sentinel : sentinels) {
// 连接sentinel节点
HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
Jedis jedis = new Jedis(hap.getHost(), hap.getPort());
// 使用sentinel get-master-addr-by-name masterName获取主节点信息
List<String> masterAddr = jedis.sentinelGetMasterAddrByName(masterName);
// 命令返回列表为空或者长度不为2,继续从下一个sentinel节点查询
if (masterAddr == null || masterAddr.size() != 2) {
continue;
}
// 解析masterAddr获取主节点信息
master = toHostAndPort(masterAddr);
// 找到后直接跳出for循环
break;
}
if (master == null) {
// 直接抛出异常,
throw new Exception();
}
// 为每个sentinel节点开启主节点switch的监控线程
for (String sentinel : sentinels) {
final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":")));
MasterListener masterListener = new MasterListener(masterName, hap.getHost(),
hap.getPort());
masterListener.start();
}
// 返回结果
return master;
}
下面代码就是MasterListener的核心监听代码,代码中比较重要的部分就是订阅Sentinel节点的+switch-master频道,它就是Redis Sentinel在结束对主节点故障转移后会发布切换主节点的消息,Sentinel节点基本将故障转移的各个阶段发生的行为都通过这种发布订阅的形式对外提供,开发者只需订阅感兴趣的频道即可
Jedis sentinelJedis = new Jedis(sentinelHost, sentinelPort);
// 客户端订阅Sentinel节点上"+switch-master"(切换主节点)频道
sentinelJedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
// 判断是否为当前masterName
if (masterName.equals(switchMasterMsg[0])) {
// 发现当前masterName发生switch,使用initPool重新初始化连接池
initPool(toHostAndPort(switchMasterMsg[3], switchMasterMsg[4]));
}
}
}
}, "+switch-master");