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网络开源框架volley(五岁以下儿童)——volley一些细节

    最近的一次volley整理出下一个.我以前没有再次遭遇了一些小问题,在该记录: 1.HttpUrlConnection DELETE 信息不能加入body问题:java.net.ProtocolExc ...

  2. C#中的DataGridView

    关键字:C# DataGridView作者:peterzb来源:http://www.cnblogs.com/peterzb 1.DataGridView实现课程表 testcontrol.rar 2 ...

  3. android延时处理任务范例

    今天要做一个任务,要求图片做button开关,点击出发对应事件.点击打开,图片左边显示几行字体,这几行字体是延时显示的.以下将主要代码附上.以下是main.xml <?xml version=& ...

  4. Swift 开源那点事儿

    最近各大网站上最喜大普奔的新闻莫过于 Swift 正式开源这条了.这无疑是一个里程碑式的前进,苹果也向开源社区更加进了一步.那么 Swift 开源后有什么具体的改变呢,我们一一道来. 成立开源社区 s ...

  5. SourceTree 免注册使用

    sourcetree安装的时候,需要注册.但是这个注册需要FQ,所以我们需要绕过注册 1. 找到目录:C:\Users\用户\AppData\Local\Atlassian\SourceTree 2. ...

  6. 毕业设计之感悟 —— UML 与 ER 图

    今天毕业设计答辩,虽然我第一个上场,但是不是特别紧张,因为整个系统都是我写的.我以为自己天衣无缝,能应付所有老师的所有问题.事实上,我被老师教育了一番. 老师说我,毕业论文中没有一个类.我一开始比较懵 ...

  7. 图像滤镜艺术---Wave滤镜

    原文:图像滤镜艺术---Wave滤镜 Wave Filter水波滤镜 水波滤镜是通过坐标变换来模拟水波效果,使图像呈现出水波的特效.这个滤镜有一个可调参数:水波的扭曲程度. 代码如下; //     ...

  8. 零元学Expression Blend 4 - Chapter 35 讨厌!!我不想一直重复设定!!『Template Binding』使用前後的差异

    原文:零元学Expression Blend 4 - Chapter 35 讨厌!!我不想一直重复设定!!『Template Binding』使用前後的差异 因为先前写到自制Button时需特别注意T ...

  9. MVC4升级MVC5导致原项目出错的解决方法

    原文:MVC4升级MVC5导致原项目出错的解决方法 出现安全透明方法"WebMatrix.WebData.PreApplicationStartCode.Start()"尝试访问安 ...

  10. 向github提交本地项目

    首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下载地址,下载后一路直接安装即可: https://gi ...