收藏 分享(赏)

zookeeper 理论知识.docx

上传人:ddeeee 文档编号:11733791 上传时间:2023-03-17 格式:DOCX 页数:39 大小:107.47KB
下载 相关 举报
zookeeper 理论知识.docx_第1页
第1页 / 共39页
zookeeper 理论知识.docx_第2页
第2页 / 共39页
zookeeper 理论知识.docx_第3页
第3页 / 共39页
zookeeper 理论知识.docx_第4页
第4页 / 共39页
zookeeper 理论知识.docx_第5页
第5页 / 共39页
亲,该文档总共39页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、简介Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。 Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。Zookeeper 维护了大规模分布式系统中的常用对象,

2、比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。Zookeeper 安装与配置本文采用 Zookeeper-3.4.0 以基础介绍它的安装步骤以及配置信息,最新的代码可以到 Zookeeper 的官网:http:/zookeeper.apache.org/下载。Zookeeper功能强大,但是安装却十分简单,下面重点以伪分布式模式来介绍 Zookeeper 的安装。伪分布式模式安装Zookeeper 安装模式包括:单机模式,

3、伪分布式模式和完全的集群模式。单机模式最简单,本文将跳过单机模式安装(单机模式安装步骤参见 Zeekeeper 官方文档:http:/zookeeper.apache.org/doc/current/zookeeperStarted.html),伪分布式模式与集群模式配置差别不大,由于手头机器有限,所以本文采用了在单台机器上伪分布式安装。本文在Ubuntu 12.04上操作,Java 环境为 OpenJDK 1.7。安装 Zookeeper 前首先下载你需要的版本,暂时解压到指定目录(本文解压至/zookeeper/目录下),并修改配置(可能需要多次修改配置文件),本次伪分布式模拟 5 个 Z

4、ookeeper 节点,事先在/tmpzookeeper目录下建立5个文件夹,分别命名 为:server001,server002,server003,server004,server005,然后在每个server00#文件夹下面新 建 data 和 logs 子文件夹。Zookeeper 的配置文件主要在 conf 目录,包括zoo.cfg (zoo_sample.cfg)和log4j.properties,修改 zoo_sample.cfg,重命名为zoo.cgf,打开zoo.cfg,内容如下:# The number of milliseconds of each ticktickTim

5、e=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sa

6、kes.dataDir=/tmp/zookeeper# the port at which the clients will connectclientPort=2181# Be sure to read the maintenance section of the # administrator guide before turning on autopurge.# http:/zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance# The number of snapshots to retain in da

7、taDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to 0 to disable auto purge feature#autopurge.purgeInterval=1将内容修改为(server001节点的配置文件):# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The num

8、ber of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=/tmp/zookeeper/server001/datadataLogDir=/tmp/zookeeper/server001/logs# the port at whi

9、ch the clients will connectclientPort=2181# Be sure to read the maintenance section of the # administrator guide before turning on autopurge.# http:/zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance# The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge ta

10、sk interval in hours# Set to 0 to disable auto purge feature#autopurge.purgeInterval=1server.1=127.0.0.1:8881:7771server.2=127.0.0.1:8882:7772server.3=127.0.0.1:8883:7773server.4=127.0.0.1:8884:7774server.5=127.0.0.1:8885:7775 tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送

11、一个心跳。 dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。 clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。 initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是

12、tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5*2000=10 秒 syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4 秒 server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选

13、举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server001/ 目录下,并在 /tmp/zookeeper/server001/data/ 下建立一个myid文件,文件内容为 1,echo 1 /tmp/zookeeper/server001/data/myid继续修改/zookeeper/目录中的zookeeper配置文件文件(server002的配置文件

14、,注意 clientPort=2182,与 server001 中的 clientPort=2181 不同,后续修改配置均需设置不同的 clientPort),内容如下:# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an

15、 acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=/tmp/zookeeper/server002/datadataLogDir=/tmp/zookeeper/server002/logs# the port at which the clients will connectclientPort=2182# Be sure to read the maint

16、enance section of the # administrator guide before turning on autopurge.# http:/zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance# The number of snapshots to retain in dataDir#autopurge.snapRetainCount=3# Purge task interval in hours# Set to 0 to disable auto purge feature#autopurg

17、e.purgeInterval=1server.1=127.0.0.1:8881:7771server.2=127.0.0.1:8882:7772server.3=127.0.0.1:8883:7773server.4=127.0.0.1:8884:7774server.5=127.0.0.1:8885:7775然后将此 zookeeper 包拷贝至 /tmp/zookeeper/server002/ 目录下,并在 /tmp/zookeeper/server002/data/ 下建立一个myid文件,文件内容为 2,echo 2 /tmp/zookeeper/server001/data/my

18、id依次修改配置文件,建立 server003,server004,server005节点文件夹,完成上述步骤后/tmp/zookeeper目录结构如下:forhappyforhappy-lenovo:/tmp/zookeeper$ tree -d -L 2. server001 data logs zookeeper-3.4.0 server002 data logs zookeeper-3.4.0 server003 data logs zookeeper-3.4.0 server004 data logs zookeeper-3.4.0 server005 data logs zooke

19、eper-3.4.0然后依次进入每个文件夹节点的zookeeper目录中,启动zookeeper服务,$ bin/zkServer.sh start如果一切顺利,Zookeeper 伪分布式模式安装成功,下面验证 Zookeeper 安装的正确性。进入任意一个文件夹节点的zookeeper包所在的目录,执行一下命令:$ bin/zkCli.sh -server 127.0.0.1:2181执行成功后:forhappyforhappy-lenovo:/tmp/zookeeper/server001/zookeeper-3.4.0$ bin/zkCli.sh -server 127.0.0.1:2

20、181Connecting to 127.0.0.1:2181Welcome to ZooKeeper!WATCHER:WatchedEvent state:SyncConnected type:None path:nullzk: 127.0.0.1:2181(CONNECTED) 0help 帮助:zk: 127.0.0.1:2181(CONNECTED) 0 helpZooKeeper -server host:port cmd args connect host:port get path watch ls path watch set path data version rmr pat

21、h delquota -n|-b path quit printwatches on|off create -s -e path data acl stat path watch close ls2 path watch history listquota path setAcl path acl getAcl path sync path redo cmdno addauth scheme auth delete path version setquota -n|-b val path至此,Zookeeper 安装完成,下一篇博客将介绍 Zookeeper Java API,并给出 Zook

22、eeper 典型的应用场景。简介Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。 Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。Zookeeper 维护了大

23、规模分布式系统中的常用对象,比如配置信息,层次化命名空间等,本文将从开发者的角度详细介绍 Zookeeper 的配置信息的意义以及 Zookeeper 的典型应用场景(配置文件的管理、集群管理、分布式队列、同步锁、Leader 选举、队列管理等)。上一篇博客主要讲了 Apache Zookeeper 的安装与配置,本文主要介绍 Zookeeper Java API。Zookeeper 提供了原生的Java API,另外还有C调用接口,本文暂不介绍Zookeeper 的 C API。Zookeeper Java APIZookeeper 作为一个分布式服务框架,主要用来解决分布式集群中应用系统的

24、一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储数据的,它的作用主要是用来维护和监控你存储的数据的状态变化。通过监控这些数据状态的变化,从而可以达到基于数据的集群管理。Zookeeper 客户端在连接 Zookeeper 服务器需要实例化一个org.apache.zookeeper.ZooKeeper对象,然后调用该类提供的接口与Zookeeper服务器进行交互。如果不指明,该类的所有方法均是线程安全的。一旦 Zookeeper 客户端与服务器建立连接,客户端就会被分配一个会话ID(session ID),客户端会定期向服务器端发送

25、心跳以保持该会话有效。只要客户端会话有效,应用程序可以调用Zookeeper客户端的接口与服务器端进行交互。下表主要介绍了org.apache.zookeeper. ZooKeeper 方法(该表摘自IBM Developerworks:分布式服务框架 Zookeeper - 管理分布式环境中的数据)方法名方法功能描述String create(Stringpath, bytedata, Listacl, CreateModecreateMode)创建一个给定的目录节点 path, 并给它设置数据,CreateMode标 识有四种形式的目录节点,分别是 PERSISTENT:持久化目录节点,这

26、个目录节点存储的数据不会丢失;PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目 录节点会根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名;EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是 session 超时,这种节点会被自动删除;EPHEMERAL_SEQUENTIAL:临时自动编号节点。Stat exists(Stringpath,booleanwatch)判断某个 path 是否存在,并设置是否监控这个目录节点,这里的 watcher 是在创建 ZooKeeper 实例时指定的 watcher,exists

27、方法还有一个重载方法,可以指定特定的watcher。Stat exists(Stringpath, Watcherwatcher)重载方法,这里给 某个目录节点设置特定的 watcher,Watcher 在 ZooKeeper 是一个核心功能,Watcher 可以监控目录节点的数据变化以及子目录的变化,一旦这些状态发生变化,服务器就会通知所有设置在这个目录节点上的 Watcher,从而每个客户端都很快知道它所关注的目录节点的状态发生变化,而做出相应的反应。void delete(Stringpath, intversion)删除 path 对应的目录节点,version 为 -1 可以匹配任何

28、版本,也就删除了这个目录节点所有数据。List getChildren(Stringpath, booleanwatch)获取指定 path 下的所有子目录节点,同样 getChildren方法也有一个重载方法可以设置特定的 watcher 监控子节点的状态。Stat setData(Stringpath, bytedata, intversion)给 path 设置数据,可以指定这个数据的版本号,如果 version 为 -1 怎可以匹配任何版本。byte getData(Stringpath, booleanwatch, Statstat)获取这个 path 对应的目录节点存储的数据,数据

29、的版本等信息可以通过 stat 来指定,同时还可以设置是否监控这个目录节点数据的状态。void addAuthInfo(Stringscheme,byteauth)客户端将自己的授权信息提交给服务器,服务器将根据这个授权信息验证客户端的访问权限。Stat setACL(Stringpath, Listacl, intversion)给某个目录节点重新设置访问权限,需要注意的是 Zookeeper 中的目录节点权限不具有传递性,父目录节点的权限不能传递给子目录节点。目录节点 ACL 由两部分组成:perms 和 id。Perms 有 ALL、READ、WRITE、CREATE、DELETE、AD

30、MIN 几种 而 id 标识了访问目录节点的身份列表,默认情况下有以下两种:ANYONE_ID_UNSAFE = new Id(world, anyone) 和 AUTH_IDS = new Id(auth, ) 分别表示任何人都可以访问和创建者拥有访问权限。 List getACL(Stringpath, Statstat) 获取某个目录节点的访问权限列表 除了以上这些上表中列出的方法之外还有一些重载方法,如都提供了一个回调类的重载方法以及可以设置特定 Watcher 的重载方法,具体的方法可以参考 org.apache.zookeeper. ZooKeeper 类的 API 说明。下面给出

31、Java API 的基本操作:/ 创建一个与服务器的连接 ZooKeeper zk = new ZooKeeper(localhost: + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, new Watcher() / 监控所有被触发的事件 public void process(WatchedEvent event) System.out.println(已经触发了 + event.getType() + 事件!); ); / 创建一个目录节点 zk.create(/testRootPath, testRootData.getBytes(), Ids

32、.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); / 创建一个子目录节点 zk.create(/testRootPath/testChildPathOne, testChildDataOne.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); System.out.println(new String(zk.getData(/testRootPath,false,null); / 取出子目录节点列表 System.out.println(zk.getChildren(/testRootPath,tru

33、e); / 修改子目录节点数据 zk.setData(/testRootPath/testChildPathOne,modifyChildDataOne.getBytes(),-1); System.out.println(目录节点状态:+zk.exists(/testRootPath,true)+); / 创建另外一个子目录节点 zk.create(/testRootPath/testChildPathTwo, testChildDataTwo.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); System.out.println

34、(new String(zk.getData(/testRootPath/testChildPathTwo,true,null); / 删除子目录节点 zk.delete(/testRootPath/testChildPathTwo,-1); zk.delete(/testRootPath/testChildPathOne,-1); / 删除父目录节点 zk.delete(/testRootPath,-1); / 关闭连接 zk.close();简介Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的顶级项

35、目。Zookeeper 为分布式系统提供了高效可靠且易于使用的协同服务,它可以为分布式应用提供相当多的服务,诸如统一命名服务,配置管理,状态同步和组服务等。 Zookeeper 接口简单,开发人员不必过多地纠结在分布式系统编程难于处理的同步和一致性问题上,你可以使用 Zookeeper 提供的现成(off-the-shelf)服务来实现分布式系统的配置管理,组管理,Leader 选举等功能。英文原文地址:http:/zookeeper.apache.org/doc/current/javaExample.html一个简单的 Zookeeper Watch 客户端为了介绍 Zookeeper J

36、ava API 的基本用法,本文将带你如何一步一步实现一个功能简单的 Zookeeper 客户端。该 Zookeeper 客户端会监视一个你指定 Zookeeper 节点 Znode, 当被监视的节点发生变化时,客户端会启动或者停止某一程序。基本要求该客户端具备四个基本要求: 客户端所带参数: o Zookeeper 服务地址。o 被监视的 Znode 节点名称。o 可执行程序及其所带的参数 客户端会获取被监视 Znode 节点的数据并启动你所指定的可执行程序。 如果被监视的 Znode 节点发生改变,客户端重新获取其内容并再次启动你所指定的可执行程序。 如果被监视的 Znode 节点消失,客

37、户端会杀死可执行程序。程序设计一般而言,Zookeeper 应用程序分为两部分,其中一部分维护与服务器端的连接,另外一部分监视 Znode 节点的数据。在本程序中,Executor 类负责维护 Zookeeper 连接,DataMonitor 类监视 Zookeeper 目录树中的数据, 同时,Executor 包含了主线程和程序主要的执行逻辑,它负责少量的用户交互,以及与可执行程序的交互,该可执行程序接受你向它传入的参数,并且会根据被监视的 Znode 节点的状态变化停止或重启。Executor类Executor 对象是本例程最基本的“容器”,它包括Zookeeper 对象和DataMoni

38、tor对象。public static void main(String args) if (args.length 4) System.err .println(USAGE: Executor hostPort znode filename program args .); System.exit(2); String hostPort = args0; String znode = args1; String filename = args2; String exec = new Stringargs.length - 3; System.arraycopy(args, 3, exec,

39、0, exec.length); try new Executor(hostPort, znode, filename, exec).run(); catch (Exception e) e.printStackTrace(); public Executor(String hostPort, String znode, String filename, String exec) throws KeeperException, IOException this.filename = filename; this.exec = exec; zk = new ZooKeeper(hostPort,

40、 3000, this); dm = new DataMonitor(zk, znode, null, this); public void run() try synchronized (this) while (!dm.dead) wait(); catch (InterruptedException e) 回忆一下 Executor 的任务是根据 Zookeeper 中 Znode 节点状态改变所触发的事件来启动和停止你在命令行指定的可执行程序, 在上面的代码你可以看到,Executor 类在其构造函数中实例化 Zookeeper 对象时,将其自身的引用作为 Watch 参数传递给 Zo

41、okeeper 的构造函数,同时它也将其自身的引用作为DataMonitorListener 参数传递给DataMonitor 的构造函数。Executor 本身实现了以下接口:public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener .Watcher 接口是在ZooKeeper Java API 中定义的。 ZooKeeper 用它来与“容器”(此处“容器”与上面的 Executor 类相似)进行通信,Watcher 只支持一个方法,即process(), ZooKeeper 用该函

42、数来处理主线程可能感兴趣的事件,例如 Zookeeper 连接或会话的状态,本例中的“容器” Executor只是简单地把事件向下传递给 DataMonitor,具体如何处理事件是由 DataMonitor 决定的。本文只是简单地描述了如何使用 Watcher,通常情况下,Executor 或 与 Executor 类似的对象拥有 与Zookeeper 服务端的连接,但它可以将事件传递给其他对象,并有其它的对象处理该事件。public void process(WatchedEvent event) dm.process(event); DataMonitorListener 接口本身不是Zo

43、okeeper API 的一部分,它完全是一个自定义的接口,可以说是专门为本程序设计的。DataMonitor 对象使用该接口和“容器”(即 Executor 类)进行通信,DataMonitorListener 接口如下:public interface DataMonitorListener /* * The existence status of the node has changed. */ void exists(byte data); /* * The ZooKeeper session is no longer valid. * * param rc * the ZooKeep

44、er reason code */ void closing(int rc);该接口在 DataMonitor 中定义,Executor 类实现该接口,当 Executor.exists() 被调用的时候,Executor 决定是否启动或停止事先指定的应用程序(回忆一下前文所说的,当 Znode 消失时 Zookeeper 客户端会杀死该可执行程序)。当 Executor.closing() 被调用的时候,Executor 会根据 Zookeeper 连接永久性地消失来决定是否关闭自己。你或许已经猜到,DataMonitor 对象根据 Zookeeper 状态变化来调用这些方法吧?以下是 Executor 类中实现 DataMonitorListener.exists() 和 DataMonitorListener.closing()的代码:public void exists( byte data ) if (data = null) if (child != null) System.out.println(Killing process); chil

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文献 > 事务文书

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报