源代码:

http://svn.apache.org/repos/asf/zookeeper/trunk/

导入eclipse:

在包含build.xml目录下执行ant eclipse将产生.classpath文件

目录结构:

src/recipes:提供了各种Zookeeper应用例子

src/c:提供了c版客户端。zookeeper_st,zookeeper_mt两个library

src/contrib:别人贡献的代码?

src/generated:由jute生成的java实体类

客户端入口:org.apache.zookeeper.ZooKeeperMain

//读取命令行输入,用MyCommandOptions解析。

//内部类MyCommandOptions包含成员命令名command、参数列表cmdArgs

-option value –option value command cmdArgs

//根据以上解析的ip、端口,连接到ZooKeeper

zk = newZooKeeper(host,

Integer.parseInt(cl.getOption("timeout")),

newMyWatcher(), readOnly);

//执行命令,在ZooKeeperMain.run()

//ZooKeeperMain只是一个外壳,使用jline实现了命令提示功能。

//commandMapCli将提供的命令命令名与执行体CliCommand关联

//execute from commandMap

CliCommandcliCmd = commandMapCli.get(cmd);

if(cliCmd!=null) {

cliCmd.setZk(zk);

watch =cliCmd.parse(args).exec();

}

//最终转到调用ZooKeeper方法

//提供的命令:

quit:Zk.close()关闭zk连接,调用cnxn.close()

history:列出历史记录

redo index:重新执行历史记录

printwatches [on]:查看/设置watche开关状态

connect:connectToZK(host)连接zk

//ZooKeeper内部连接

cnxn = newClientCnxn(connectStringParser.getChrootPath(),

hostProvider,sessionTimeout,this,watchManager,

getClientCnxnSocket(),canBeReadOnly);

cnxn.start();

ClientCnxn包含SendThread和EventThread两个线程

SendThread将事件添加到waitEvents队列中,EventThread线程消费该队列。

//下面以ls命令为例

//调用zk.getChildren

public boolean exec() throwsKeeperException, InterruptedException {

String path= args[1];

boolean watch =cl.hasOption("w");

List<String> children = zk.getChildren(path, watch);

out.println(children);

return watch;

}

//getChildren生成request

RequestHeader h = newRequestHeader();

h.setType(ZooDefs.OpCode.getChildren);

GetChildrenRequest request = newGetChildrenRequest();

request.setPath(serverPath);

request.setWatch(watcher != null);

GetChildrenResponse response = newGetChildrenResponse();

ReplyHeader r = cnxn.submitRequest(h, request,response, wcb);

//submitRequest调用queuePacket

publicReplyHeadersubmitRequest(RequestHeaderh, Record request,

Recordresponse, WatchRegistration watchRegistration)

throwsInterruptedException {

ReplyHeaderr = new ReplyHeader();

Packetpacket = queuePacket(h,r, request, response,null,null,null,

null, watchRegistration);

synchronized(packet) {

while (!packet.finished) {

packet.wait();

}

}

return r;

}

//queuePacket将Packet添加到outgoingQueue队列中

packet= new Packet(h, r, request, response,watchRegistration);

packet.cb = cb;

packet.ctx = ctx;

packet.clientPath =clientPath;

packet.serverPath =serverPath;

outgoingQueue.add(packet);

//然后唤醒selector

sendThread.getClientCnxnSocket().wakeupCnxn();

//sendThread.run消费outgoingQueue

clientCnxnSocket.doTransport(to,pendingQueue,outgoingQueue,ClientCnxn.this);

//selector判断读/写事件

//doTransport调用doIO,doIO解析Response

//读事件

int rc =sock.read(incomingBuffer);

sendThread.readResponse(incomingBuffer);

//写事件

sock.write(p.bb);

//readResponse在finally块中调用finishPacket,finishPacket将设置packet.finish,

//此时submitRequest返回response。

try {

packet.replyHeader.setXid(replyHdr.getXid());

packet.replyHeader.setErr(replyHdr.getErr());

packet.replyHeader.setZxid(replyHdr.getZxid());

if(replyHdr.getZxid() > 0) {

lastZxid =replyHdr.getZxid();

}

if(packet.response !=null&& replyHdr.getErr() == 0) {

packet.response.deserialize(bbia,"response");

}

} finally {

finishPacket(packet);

}

以下图片转自:http://www.spnguru.com/2010/08/zookeeper%E5%85%A8%E8%A7%A3%E6%9E%90%E2%80%94%E2%80%94client%E7%AB%AF/

ZooKeeper源码阅读(二):客户端的更多相关文章

  1. ZooKeeper源码阅读——client(二)

    原创技术文章,转载请注明:转自http://newliferen.github.io/ 如何连接ZooKeeper集群   要想了解ZooKeeper客户端实现原理,首先需要关注一下客户端的使用方式, ...

  2. Zookeeper 源码(二)序列化组件 Jute

    Zookeeper 源码(二)序列化组件 Jute 一.序列化组件 Jute 对于一个网络通信,首先需要解决的就是对数据的序列化和反序列化处理,在 ZooKeeper 中,使用了Jute 这一序列化组 ...

  3. zookeeper源码分析之三客户端发送请求流程

    znode 可以被监控,包括这个目录节点中存储的数据的修改,子节点目录的变化等,一旦变化可以通知设置监控的客户端,这个功能是zookeeper对于应用最重要的特性,通过这个特性可以实现的功能包括配置的 ...

  4. xxl-job源码阅读二(服务端)

    1.源码入口 xxl-job-admin是一个简单的springboot工程,简单翻看源码,可以很快发现XxlJobAdminConfig入口. @Override public void after ...

  5. Spring 源码阅读 二

    程序入口: 接着上一篇博客中看完了在AnnotationConfigApplicationContext的构造函数中的register(annotatedClasses);将我们传递进来的主配置类添加 ...

  6. zookeeper源码分析二FASTLEADER选举算法

    如何在zookeeper集群中选举出一个leader,zookeeper使用了三种算法,具体使用哪种算法,在配置文件中是可以配置的,对应的配置项是"electionAlg",其中1 ...

  7. zookeeper 源码(二) session 和 处理事务请求

    问题 session 如何生成的?sessionId为什么不直接使用时间戳+单机名 sessionid 关闭的时候的逻辑,sessionid 的维护是由各节点还是leader ? 会话相关 sessi ...

  8. SparkConf加载与SparkContext创建(源码阅读二)

    紧接着昨天,我们继续开搞了啊.. 1.下面,开始创建BroadcastManager,就是传说中的广播变量管理器.BroadcastManager用于将配置信息和序列化后的RDD.Job以及Shuff ...

  9. JDK源码阅读(二) AbstractList

    package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> i ...

随机推荐

  1. Diet

    Dialogue 1   Healthy diet 关于健康饮食 F:Bob, look at this sentence. 'Healthy eating is not about strict n ...

  2. Generator & yield write in sync way

    Generator & yield write in sync way var p = new Promise(function(resolve, reject){ setTimeout(fu ...

  3. Promise原理 && 简单实现

    Promise原理 参考https://github.com/chunpu/promise/blob/master/promise.js 个人认为原博的实现有点问题 在next函数的实现上, 会导致无 ...

  4. MacBook USB Type-C接口很美?其实是缩水的!

    苹果终于推出了12寸的全新MacBook,拥有2304×1440的高分辨率.蝶式结构全尺寸键盘.新的触摸板.14nm Core M处理器和无风扇设计,以及新的USB 3.1 Type-C接口.可以预料 ...

  5. Unix/Linux环境C编程入门教程(20) 搭建基于Mac的 Xcode 与 QT 开发环境

    1.启动 Vmware,如果没有 VMware 的同学,请看前面我们搭建 VMware 的视频 2.打开虚拟机以后,出现虚拟机界面 3 新建一个虚拟机 4 选择自定义,单击下一步 5 选择默认的 VM ...

  6. poj 1065 简单的贪心算法

    题意大概是:有一组木头需要处理,每块木头有长度,重量两个属性,处理的前一块木头长len,重wei,当下一块木头的len1,与wei1满足:len1>len&&wei1>we ...

  7. ar解压deb包

    解压文件 ar -x libstdc++6_4.7.2-5_i386.deb tar -zxvf data.tar.gz

  8. PHP与MySQL交互

    <?php $con = mysql_connect("localhost","root","12345"); $dbcharset ...

  9. null类型的字段加1

    很高兴今天学到了一种新方法. 数据库中字段类型为Long ,值可能为null,也可能是某一数.因此对该字段数值进行 +1操作时需要判断该值是null还是数值. 同时实现更新操作.具体如下: updat ...

  10. 相见恨晚——MarkDown

    什么是MarkDown MarkDown是一种轻量级的标记语言 MarkDown使你更加关注文章的内容 MarkDown使文章的排版变得简单直接 什么情景下使用MarkDown 在我们熟悉的githu ...