为了说明问题,先来看个简单的示例代码:
package com.tom.zookeeper.book; import com.tom.Host; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.Watcher; import java.io.IOException; public class Master implements Watcher { ZooKeeper zk; String hostPort; Master(String hostPort) { this.hostPort = hostPort; } void startZK() throws IOException { System.out.println("Start to create the ZooKeeper instance"); zk = new ZooKeeper(hostPort, 15000, this); System.out.println("Finish to create the ZooKeeper instance"); } public void process(WatchedEvent e) { System.out.println(e); } public static void main(String args[]) throws Exception { Master m = new Master(Host.HOST); m.startZK(); Thread.sleep(60*1000); //sleep 60s } }
1.不启动Zookeeper服务器
运行上面的代码,输出是
Start to create the ZooKeeper instance
Finish to create the ZooKeeper instance
因为会话没有建立,所以,Watcher的回调方法process没有被调用
2.启动Zookeeper服务器
运行上面的代码,在运行结束前,然后关闭Zookeeper服务器,输出是
Finish to create the ZooKeeper instance
WatchedEvent state:SyncConnected type:None path:null
WatchedEvent state:Disconnected type:None path:null
3.启动Zookeeper服务器,运行代码,在运行结束前,关闭服务器和重启服务器,输出是
Finish to create the ZooKeeper instance
WatchedEvent state:SyncConnected type:None path:null
WatchedEvent state:Disconnected type:None path:null
WatchedEvent state:SyncConnected type:None path:null
对于第三种情况,客户端重新建立会话,原因是当Zookeeper服务器不可用时,客户端维持的是CONNECTING状态,当Zookeeper服务器又可用时,客户端会自动重建会话
假如Zookeeper Client的超时时间是5秒钟,而Zookeeper服务器挂了10分钟,按理说,Zookeeper Client的连接早该超时了,可为什么能会话重建呢,为什么会有如下的事件发生呢?
WatchedEvent state:Disconnected type:None path:null
WatchedEvent state:SyncConnected type:None path:null
原因可以参考Zookeeper的官方FAQ,它就这个给出了答案:
What happens to ZK sessions while the cluster is down?
Imagine that a client is connected to ZK with a 5 second session timeout, and the administrator brings the entire ZK cluster down for an upgrade. The cluster is down for several minutes, and then is restarted.
In this scenario, the client is able to reconnect and refresh its session. Because session timeouts are tracked by the leader, the session starts counting down again with a fresh timeout when the cluster is restarted. So, as long as the client connects within the first 5 seconds after a leader is elected, it will reconnect without an expiration, and any ephemeral nodes it had prior to the downtime will be maintained.
The same behavior is exhibited when the leader crashes and a new one is elected. In the limit, if the leader is flip-flopping back and forth quickly, sessions will never expire since their timers are getting constantly reset.
总结和问题:
1.在Zookeeper服务器不可用时,客户端等维持CONNECTING状态多长时间?
2.通过这个实例,我们在处理Disconnected事件时,不应该重新创建新的Zookeeper实例,试图创建新的会话,如果Zookeeper能够恢复,那么之前的Zookeeper实例在Zookeeper Client Library的帮助下可以完成会话重建,如果Zookeeper不能恢复,那么重新创建Zookeeper也是徒劳无功。
相关推荐
zookeeper学习笔记
Zookeeper学习笔记
自己整理的ZooKeeper学习笔记,适合刚刚接触ZooKeeper的人学习
java ZooKeeper学习笔记\ZooKeeper原理、运用
ZooKeeper是一种为分布式应用所设计的高可用、高性能且一致的开源协调服务,它提供了一项基本服务:**分布式锁服务**。由于ZooKeeper的开源特性,后来我们的开发者在分布式锁的基础上,摸索了出了其他的使用方法:**...
linux下编译zookeeper3.7.0出的头文件和库: proto.h recordio.h zookeeper.h zookeeper.jute.h zookeeper_log.h zookeeper_version.h libzookeeper_mt.a libzookeeper_mt.la libzookeeper_mt.so libzookeeper_mt....
本文适合但不限于软件开发人员阅读。本文档能够使阅读者对zookeeper有一个宏观且全面的了解,内容主要包含zookeeper架构、数据模型、读写及工作原理、典型应用场景、指令汇总等,
尚硅谷2021 zookeeper 笔记
hadoop,hbase,zookeeper安装笔记hadoop,hbase,zookeeper安装笔记hadoop,hbase,zookeeper安装笔记
zookeeper笔记
zookeeper笔记
zookeeper笔记.pdf
资源名称:zookeeper笔记和搭建 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
ZooKeeper会话超时以及重连机制
zookeeper client依赖jar包,核心依赖的jar包,生产者和消费者必不可少的
适合初学入门,知识巩固。涵盖安装配置、命令操作、Java API操作、事件监听、分布式锁、集群搭建等知识
zookeeper的客户端应用
- 概述 - 术语 - 分布式应用 - 介绍 - 架构 - 工作流 - Leader选举 - 安装服务 - CLI 操作 - java-api - 动态感知服务器上下线 - 实现Hadoop高可用(Hadoop-HA-High Availability)