ZooKeeper 类是ZooKeeper 客户端库的主要类。要使用ZooKeeper服务,应用程序必须首先实例化ZooKeeper类的对象。所有的迭代都将通过调用ZooKeeper类的方法来完成。除非另有说明,否则此类的方法是线程安全的。

一旦建立到服务器的连接,会话ID就分配给客户机。客户机将定期向服务器发送心跳以保持会话有效。

只要客户端的会话ID保持有效,应用程序就可以通过客户端调用ZooKeeper API。

如果由于某种原因,客户机在很长一段时间内(例如超过sessionTimeout值)无法向服务器发送心跳,服务器将使会话过期,会话ID将变为无效。客户端对象将不再可用。要进行ZooKeeper API调用,应用程序必须创建一个新的客户机对象。

如果客户机当前连接到的ZooKeeper服务器出现故障或没有响应,客户机将在其会话ID过期之前自动尝试连接到其他服务器。如果成功,应用程序可以继续使用客户端。

ZooKeeper API方法要么是同步的,要么是异步的。同步方法会一直阻塞,直到服务器响应为止。异步方法只是将发送请求排队,然后立即返回。它们接受一个回调对象,该对象将在成功执行请求或出错时执行,返回代码(rc)指示错误。

一些成功的ZooKeeper API调用可以将监视留在ZooKeeper服务器的“数据节点”上。其他成功的ZooKeeper API调用可以触发这些监视。一旦一个手表被触发,一个事件将被传递给客户机,客户机首先离开了手表。每个表只能触发一次。因此,每离开一只手表,最多会向客户发送一个事件。

客户机需要一个实现观察程序接口的类的对象来处理传递给客户机的事件。当客户机断开当前连接并重新连接到服务器时,所有现有的监视都被认为是被触发的,但未传递的事件将丢失。为了模拟这一点,客户机将生成一个特殊的事件来告诉事件处理程序连接已经断开。此特殊事件具有类型eventnone和状态skeeperstatedisconnected。

This is the main class of ZooKeeper client library. To use a ZooKeeper service, an application must first instantiate an object of ZooKeeper class. All the iterations will be done by calling the methods of ZooKeeper class. The methods of this class are thread-safe unless otherwise noted.

Once a connection to a server is established, a session ID is assigned to the client. The client will send heart beats to the server periodically to keep the session valid.

The application can call ZooKeeper APIs through a client as long as the session ID of the client remains valid.

If for some reason, the client fails to send heart beats to the server for a prolonged period of time (exceeding the sessionTimeout value, for instance), the server will expire the session, and the session ID will become invalid. The client object will no longer be usable. To make ZooKeeper API calls, the application must create a new client object.

If the ZooKeeper server the client currently connects to fails or otherwise does not respond, the client will automatically try to connect to another server before its session ID expires. If successful, the application can continue to use the client.

The ZooKeeper API methods are either synchronous or asynchronous. Synchronous methods blocks until the server has responded. Asynchronous methods just queue the request for sending and return immediately. They take a callback object that will be executed either on successful execution of the request or on error with an appropriate return code (rc) indicating the error.

Some successful ZooKeeper API calls can leave watches on the "data nodes" in the ZooKeeper server. Other successful ZooKeeper API calls can trigger those watches. Once a watch is triggered, an event will be delivered to the client which left the watch at the first place. Each watch can be triggered only once. Thus, up to one event will be delivered to a client for every watch it leaves.

A client needs an object of a class implementing Watcher interface for processing the events delivered to the client. When a client drops current connection and re-connects to a server, all the existing watches are considered as being triggered but the undelivered events are lost. To emulate this, the client will generate a special event to tell the event handler a connection has been dropped. This special event has type EventNone and state sKeeperStateDisconnected.

ZooKeeper类说明的更多相关文章

  1. rabbitMq实现与zookeeper类似的watcher功能

    场景:A.B.C.D(可以是一个机器的不同进程,也可以是不同机器的进程)启动了相同的项目,使用同一个数据库.但是,如果A修改了数据库的数据,需要B.C.D在很短的时间能够知道数据库发生了修改.当然可以 ...

  2. Zookeeper开源客户端框架Curator简介

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

  3. Zookeeper Api(java)入门与应用(转)

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  4. 转载:ZooKeeper Programmer's Guide(中文翻译)

    本文是为想要创建使用ZooKeeper协调服务优势的分布式应用的开发者准备的.本文包含理论信息和实践信息. 本指南的前四节对各种ZooKeeper概念进行较高层次的讨论.这些概念对于理解ZooKeep ...

  5. 分布式服务框架Zookeeper

    协议介绍 zookeeper协议分为两种模式 崩溃恢复模式和消息广播模式 崩溃恢复协议是在集群中所选举的leader 宕机或者关闭 等现象出现 follower重新进行选举出新的leader 同时集群 ...

  6. Zookeeper集群的安装和使用

    Apache Zookeeper 由 Apache Hadoop 的 Zookeeper 子项目发展而来,现已经成为 Apache 的顶级项目,它是一个开放源码的分布式应用程序协调服务,是Google ...

  7. 分布式服务框架 Zookeeper -- 管理分布式环境中的数据

    转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html Zookeeper 分布式服务框架是 Apa ...

  8. ZooKeeper程序员指南(转)

    译自http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html 1 简介 本文是为想要创建使用ZooKeeper协调服务优势的分布式 ...

  9. Zookeeper开源客户端框架Curator简介[转]

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

随机推荐

  1. Android 事件分发机制具体解释

    很多其它内容请參照我的个人网站: http://stackvoid.com/ 网上非常多关于Android事件分发机制的解释,大多数描写叙述的都不够清晰,没有吧来龙去脉搞清晰,本文将带你从Touch事 ...

  2. OpenCV实现马赛克和毛玻璃滤镜效果

    一.马赛克效果 马赛克的实现原理是把图像上某个像素点一定范围邻域内的所有点用邻域内随机选取的一个像素点的颜色代替,这样可以模糊细节,但是可以保留大体的轮廓. 以下OpenCV程序实现马赛克效果,通过鼠 ...

  3. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

  4. freemarker写select包(四)

    freemarker写select包 1.宏定义 <#macro select id datas value="" key="" text="& ...

  5. OpenWrt 主的发展版本号trunk MT7620N 无线驱动程序bug

    周边环境: OpenWrt Development Trunk: svn co svn://svn.openwrt.org/openwrt/trunk/ BUG: 1. 无线无法建立连接. 2. 无线 ...

  6. 获取 UIElement 相对于屏幕原点所占用的矩形区域

    原文:获取 UIElement 相对于屏幕原点所占用的矩形区域 <Grid Background="Transparent"> <StackPanel Margi ...

  7. 模拟请求(模拟header gzip解压 泛型)

    WebClient HeaderData是自定义类对象,存储header信息 private static T GetDataCommonMethod<T>(string url, str ...

  8. jvm常用参数设置 专题

    在jdk8中 -Xms2g不合法,能通过的:-Xms2G #!/bin/bash JAVA_OPTS="-Xms4G -Xmx4G -XX:+HeapDumpOnOutOfMemoryErr ...

  9. C#:foreach语句,yield语句

    原文:C#:foreach语句,yield语句 1. foreach语句 C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in pe ...

  10. 【Linux】scp“免密” 远程copy较多文件

    一.linux脚本实现自动输入密码 使用Linux的程序员对输入密码这个举动一定不陌生,在Linux下对用户有严格的权限限制,干很多事情越过了权限就得输入密码,比如使用超级用户执行命令,又比如ftp. ...