`

zookeeper学习记录

阅读更多

背景

前段时间看了S4流计算引擎,里面使用到了zookeeper进行集群管理,所以也就花了点时间研究了下zookeeper,不求看懂所有源码,但求了解其实现机制和原理,清楚其基本使用。这也是为后续hadoop,gridgain的分布式计算的产品。

学习

首先就是收集一些前人的一些学习资料和总结内容,方便自己快速入门。

这里罗列了几篇不错的文章:

看了这两篇文章,基本可以对zookeeper有了一个感性的认识,它是一个什么?

zookeeper功能点:
  • 统一命名空间(Name Service)
  • 配置推送 (Watch)
  • 集群管理(Group membership)

统一命名空间

在zookeeper中实现了一个类似file system系统的数据结构,比如/zookeeper/status。 每个节点都对应于一个znode节点。

znode节点的数据结构模型:


znode的数据结构内容:

  • czxid

    The zxid of the change that caused this znode to be created.

  • mzxid

    The zxid of the change that last modified this znode.

  • ctime

    The time in milliseconds from epoch when this znode was created.

  • mtime

    The time in milliseconds from epoch when this znode was last modified.

  • version

    The number of changes to the data of this znode.

  • cversion

    The number of changes to the children of this znode.

  • aversion

    The number of changes to the ACL of this znode.

  • ephemeralOwner

    The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.

  • dataLength

    The length of the data field of this znode.

  • numChildren

    The number of children of this znode.

说明: zxid (ZooKeeper Transaction Id,每次请求对应一个唯一的zxid,如果zxid a < zxid b ,则可以保证a一定发生在b之前)。

针对树状结构的处理,来看一下客户端使用的api :

Java代码 收藏代码
  1. Stringcreate(Stringpath,bytedata[],List<ACL>acl,CreateModecreateMode)
  2. voidcreate(Stringpath,bytedata[],List<ACL>acl,CreateModecreateMode,StringCallbackcb,Objectctx)
  3. voiddelete(Stringpath,intversion)
  4. voiddelete(Stringpath,intversion,VoidCallbackcb,Objectctx)
  5. StatsetData(Stringpath,bytedata[],intversion)
  6. voidsetData(Stringpath,bytedata[],intversion,StatCallbackcb,Objectctx)
  7. StatsetACL(Stringpath,List<ACL>acl,intversion)
  8. voidsetACL(Stringpath,List<ACL>acl,intversion,StatCallbackcb,Objectctx)
  9. Statexists(Stringpath,Watcherwatcher)
  10. Statexists(Stringpath,booleanwatch)
  11. voidexists(Stringpath,Watcherwatcher,StatCallbackcb,Objectctx)
  12. voidexists(Stringpath,booleanwatch,StatCallbackcb,Objectctx)
  13. byte[]getData(Stringpath,Watcherwatcher,Statstat)
  14. byte[]getData(Stringpath,booleanwatch,Statstat)
  15. voidgetData(Stringpath,Watcherwatcher,DataCallbackcb,Objectctx)
  16. voidgetData(Stringpath,booleanwatch,DataCallbackcb,Objectctx)
  17. List<String>getChildren(Stringpath,Watcherwatcher)
  18. List<String>getChildren(Stringpath,booleanwatch)
  19. voidgetChildren(Stringpath,Watcherwatcher,ChildrenCallbackcb,Objectctx)
  20. voidgetChildren(Stringpath,booleanwatch,ChildrenCallbackcb,Objectctx)
  21. List<String>getChildren(Stringpath,Watcherwatcher,Statstat)
  22. List<String>getChildren(Stringpath,booleanwatch,Statstat)
  23. voidgetChildren(Stringpath,Watcherwatcher,Children2Callbackcb,Objectctx)
  24. voidgetChildren(Stringpath,booleanwatch,Children2Callbackcb,Objectctx)

说明:每一种按同步还是异步,添加指定watcher还是默认watcher又分为4种。默认watcher可以在ZooKeeper zk = new ZooKeeper(serverList, sessionTimeout, watcher)中进行指定。如果包含boolean watch的读方法传入true则将默认watcher注册为所关注事件的watch。如果传入false则不注册任何watch

CreateMode主要有几种:

  • PERSISTENT (持续的,相比于EPHEMERAL,不会随着client session的close/expire而消失)
  • PERSISTENT_SEQUENTIAL
  • EPHEMERAL (短暂的,生命周期依赖于client session,对应session close/expire后其znode也会消失)
  • EPHEMERAL_SEQUENTIAL (SEQUENTIAL意为顺序的)
AsyncCallback异步callback,根据操作类型的不同,也分几类:
  • StringCallback
  • VoidCallback
  • StatCallback
  • DataCallback (getData请求)
  • ChildrenCallback
  • Children2Callback
对应的ACL这里有篇不错的文章介绍,http://rdc.taobao.com/team/jm/archives/947

配置推送(Watcher)

zookeeper为解决数据的一致性,使用了Watcher的异步回调接口,将服务端znode的变化以事件的形式通知给客户端,主要是一种反向推送的机制,让客户端可以做出及时响应。比如及时更新后端的可用集群服务列表。

这里有篇文章介绍Watcher/Callback比较详细,可以参考下:

如果想更好的理解Watcher的使用场景,可以了解下使用Watcher机制实现分布式的Barrier , Queue , Lock同步。

Barrier例子:

Java代码 收藏代码
  1. publicclassBarrierimplementsWatcher{
  2. privatestaticfinalStringaddr="10.20.156.49:2181";
  3. privateZooKeeperzk=null;
  4. privateIntegermutex;
  5. privateintsize=0;
  6. privateStringroot;
  7. publicBarrier(Stringroot,intsize){
  8. this.root=root;
  9. this.size=size;
  10. try{
  11. zk=newZooKeeper(addr,10*1000,this);
  12. mutex=newInteger(-1);
  13. Stats=zk.exists(root,false);
  14. if(s==null){
  15. zk.create(root,newbyte[0],Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
  16. }
  17. }catch(Exceptione){
  18. e.printStackTrace();
  19. }
  20. }
  21. publicsynchronizedvoidprocess(WatchedEventevent){
  22. synchronized(mutex){
  23. mutex.notify();
  24. }
  25. }
  26. publicbooleanenter(Stringname)throwsException{
  27. zk.create(root+"/"+name,newbyte[0],Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
  28. while(true){
  29. synchronized(mutex){
  30. List<String>list=zk.getChildren(root,true);
  31. if(list.size()<size){
  32. mutex.wait();
  33. }else{
  34. returntrue;
  35. }
  36. }
  37. }
  38. }
  39. publicbooleanleave(Stringname)throwsKeeperException,InterruptedException{
  40. zk.delete(root+"/"+name,0);
  41. while(true){
  42. synchronized(mutex){
  43. List<String>list=zk.getChildren(root,true);
  44. if(list.size()>0){
  45. mutex.wait();
  46. }else{
  47. returntrue;
  48. }
  49. }
  50. }
  51. }
  52. }

测试代码:

Java代码 收藏代码
  1. publicclassBarrierTest{
  2. publicstaticvoidmain(Stringargs[])throwsException{
  3. for(inti=0;i<3;i++){
  4. Processp=newProcess("Thread-"+i,newBarrier("/test/barrier",3));
  5. p.start();
  6. }
  7. }
  8. }
  9. classProcessextendsThread{
  10. privateStringname;
  11. privateBarrierbarrier;
  12. publicProcess(Stringname,Barrierbarrier){
  13. this.name=name;
  14. this.barrier=barrier;
  15. }
  16. @Override
  17. publicvoidrun(){
  18. try{
  19. barrier.enter(name);
  20. System.out.println(name+"enter");
  21. Thread.sleep(1000+newRandom().nextInt(2000));
  22. barrier.leave(name);
  23. System.out.println(name+"leave");
  24. }catch(Exceptione){
  25. e.printStackTrace();
  26. }
  27. }
  28. }

通过该Barrier,可以协调不同任务之间的同步处理,这里主要还是利用了Watcher机制的反向推送,避免客户端的循环polling动作,只要针对有事件的变化做一次响应。

集群管理

我不罗嗦,taobao有几篇文章已经介绍的很详细。

zookeeper集群对server进行了归类,可分为:
  • Leader
  • Follower
  • Obserer
说明:
1. Leader/Follower会通过选举算法进行选择,可以看一下http://zookeeper.apache.org/doc/r3.3.2/recipes.html里的Leader Election章节。
2. Observer主要是为提升zookeeper的性能,observer和follower的主要区别就是observer不参与Leader agreement vote处理。只提供读节点的处理,类似于master/slave的读请求。(http://zookeeper.apache.org/doc/r3.3.2/zookeeperObservers.html)
Java代码 收藏代码
  1. server.1:localhost:2181:3181:observer

3. 可通过命令行,查看当前server所处的状态

Java代码 收藏代码
  1. [ljh@ccbu-156-49bin]$echostat|nclocalhost2181
  2. Zookeeperversion:3.3.3--1,builton06/24/201113:12GMT
  3. Clients:
  4. /10.16.4.30:34760[1](queued=0,recved=632,sent=632)
  5. /127.0.0.1:43626[0](queued=0,recved=1,sent=0)
  6. /10.16.4.30:34797[1](queued=0,recved=2917,sent=2917)
  7. Latencymin/avg/max:0/0/33
  8. Received:3552
  9. Sent:3551
  10. Outstanding:0
  11. Zxid:0x200000003
  12. Mode:follower##当前模式
  13. Nodecount:8

使用zookeeper,我们能干些什么?

官方文档中,有举了几个应用场景,就是使用zookeeper提供分布式锁机制,从而实现分布式的一致性处理。

典型的几个场景:

  • Barrier
  • Queue
  • Lock
  • 2PC

其他

zookeeper基本是基于API和console进行znode的操作,并没有一个比较方便的操作界面,这里也发现了taobao 伯岩写的一个工具,可以比较方便的查询zookeeper信息。

工具的开发语言主要是node.js(最近比较火),其标榜的是无阻塞的api使用。其原理主要是基于google的V8(chrome的javascript的解析器,C语言编写),node.js本身是基于js语法进行开发,通过V8解析为C语言的执行代码

其标榜的无阻塞I/O实现,那可想而知就是linux系统下的select/poll的I/O模型。有兴趣的可以看下node.js的官网,下载一个玩玩。

文档地址:http://www.blogjava.net/killme2008/archive/2011/06/06/351793.html

代码地址: https://github.com/killme2008/node-zk-browser

通过git下载源码后,需要安装下node.js的几个模块express,express-namespace, zookeeper。 node.js下有个比较方便的模块管理器npm,类似于redhat的rpm,ubuntu的apt-get。

安装模块:

Java代码 收藏代码
  1. npminstall-gexpress

几个界面:


分享到:
评论

相关推荐

    zookeeper学习笔记

    zookeeper学习笔记

    Zookeeper学习笔记.docx

    Zookeeper学习笔记

    Zookeeper学习笔记

    自己整理的ZooKeeper学习笔记,适合刚刚接触ZooKeeper的人学习

    ZooKeeper学习笔记

    java ZooKeeper学习笔记\ZooKeeper原理、运用

    Zookeeper学习笔记.pdf

    ZooKeeper是一种为分布式应用所设计的高可用、高性能且一致的开源协调服务,它提供了一项基本服务:**分布式锁服务**。由于ZooKeeper的开源特性,后来我们的开发者在分布式锁的基础上,摸索了出了其他的使用方法:**...

    zookeeper学习笔记.pptx

    本文适合但不限于软件开发人员阅读。本文档能够使阅读者对zookeeper有一个宏观且全面的了解,内容主要包含zookeeper架构、数据模型、读写及工作原理、典型应用场景、指令汇总等,

    Zookeeper学习资源和笔记(附代码)

    适合初学入门,知识巩固。涵盖安装配置、命令操作、Java API操作、事件监听、分布式锁、集群搭建等知识

    zookeeper一站式学习资料

    zookeeper一站式学习资料包含国内首部Zookeeper从入门到精通+搜索引擎等一条龙学习资料以及视频讲解包含笔记代码资源

    学习笔记--zookeeper

    zookeeper学习,包括zookeeper架构,原理,安装,配置,命令管理,API编程以及可以应用的场景

    zookeeper完整学习笔记

    - 概述 - 术语 - 分布式应用 - 介绍 - 架构 - 工作流 - Leader选举 - 安装服务 - CLI 操作 - java-api - 动态感知服务器上下线 - 实现Hadoop高可用(Hadoop-HA-High Availability)

    java后端学习笔记

    activeMq,rabbitMq,activity工作流,docker,dubbo,netty,rpc,springcloud,zookeeper学习笔记

    云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-基于ssm的云的学习笔记系统-ssm-java代码

    zookeeper云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-...

    zookeeper 学习分享

    这是我自己学习zookeeper的一些过程记录,希望能够帮到大家

    ZooKeeper.pdf

    Zookeeper技术的基础详细学习笔记,总结了Zookeeper的各个知识点,可以用来复习以及对基础知识的巩固,对新人的学习很有帮助。

    zookeeper笔记

    zookeeper原理以及应用笔记学习总结,适合初学者!欢迎下载

    4.zookeeper运维实战视频教程资料-详细课件笔记总结

    学习zookeeper的运行原理、集群搭建、应用场景、理解相关概念,总结常见面试题,提高面试通过率 课程简介: 1.zookeeper介绍、安装方式和应用场景 2.zookeeper的单机安装和配置文件介绍 3.zookeeper的集群安装 4....

    zookeeper-01.xmind

    zk学习笔记

    1.笔记_zookeeper_

    Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目。该文档适合学习者学习zookeeper,结构清晰,层次递增,需要学习者配合xmind软件学习

    Zookeeper集群架构全面实战

    非常强的一套Zookeeper集群实战,包含了全套的学习代码,学习笔记还有学习资料。内容从Zookeeper入门教学,本地安装,Zookeeper集群实战,项目需求,Zookeeper企业面试实战,Zookeeper算法实战,Zookeeper核心源码...

Global site tag (gtag.js) - Google Analytics