logo资料库

zookeeper详解.pdf

第1页 / 共46页
第2页 / 共46页
第3页 / 共46页
第4页 / 共46页
第5页 / 共46页
第6页 / 共46页
第7页 / 共46页
第8页 / 共46页
资料共46页,剩余部分请下载后查看
自序
概述
安装和运行Zookeeper
Zookeeper开发实例
ZooKeeper中的组和成员
创建组
加入组
成员列表
删除分组
Zookeeper 服务
数据模型 Data Model
操作 Operations
实现 Implementation
数据一致性 Consistency
会话 Sessions
ZooKeeper应用程序 Building Applications with ZooKeeper
配置服务 Configuration Service
坚韧的ZooKeeper应用 The Resilient ZooKeeper Application
一个稳定的配置服务 A reliable configuration service
生产环境中的ZooKeeper ZooKeeper in Production
韧性和性能 Resilience and Performance
配置
Table of Contents 自序 概述 安装和运行Zookeeper Zookeeper开发实例 ZooKeeper中的组和成员 创建组 加入组 成员列表 删除分组 Zookeeper 服务 数据模型 Data Model 操作 Operations 实现 Implementation 数据一致性 Consistency 会话 Sessions ZooKeeper应用程序 Building Applications with ZooKeeper 配置服务 Configuration Service 坚韧的ZooKeeper应用 The Resilient ZooKeeper Application 一个稳定的配置服务 A reliable configuration service 生产环境中的ZooKeeper ZooKeeper in Production 韧性和性能 Resilience and Performance 配置 1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.6 1.6.1 1.6.2 1.6.3 1.7 1.7.1 1.7.2 2
自序 自序 2016年9月底,于北京 最早接触ZooKeeper是因为工作上使用了Kafka集群,看了一些ZooKeeper的资料。那时对 ZooKeeper懵懵懂懂,后来心里有很多疑问。比如,那时根本搞不清ZooKeeper是如何实现集 群调度的,client的意义和znode的意义是什么?leader选举是ZooKeeper服务器之间的策略算 法,还是Client之间的策略算法,甚至当时我真的混淆了(书中也提到了这一点)? ZooKeeper到底起了什么作用?而且最开始的一个错误理解,认为每一台Kafka服务器上都需 要一个ZooKeeper,然后ZooKeeper来帮助Kafka实现集群内的数据一致性等特性。 当时按照教程搭建了Kafka集群,应用起来也是行云流水,好像跟ZooKeeper没有多大关系 了。后来,我买了本《Hadoop: The Definitive Guide 4th Edition》。当通读了ZooKeeper相 关章节后,我的心里对我之前ZooKeeper的认识,只剩下两个字了——呵呵-_-||| 一切都好像拨云见日那样清爽了。不仅仅是对ZooKeeper的认识更深了,也让自己对分布式 系统的认识上升了一个台阶。 最后,我考虑要整理一下关于ZooKeeper的读书笔记,其实内容多是读书时自己的翻译。那 为什么不把《Hadoop: The Definitive Guide 4th Edition》关于ZooKeeper的内容翻译过来 呢,这样不是对于我来说更简单一些?于是,我就在我的博客上开始了翻译工作。 经过两个月陆陆续续的翻译,现在终于可以发出来了!本书的内容来自《Hadoop: The Definitive Guide 4th Edition》,在这里向书的作者和贡献者致以崇高的敬意。 本书的内容纯属个人业余翻译,欢迎各位读者批评!这里留下作者的email,欢迎大家吐 槽!holynull@126.com 3
概述 概述 Zookeeper是Hadoop分布式调度服务,用来构建分布式应用系统。构建一个分布式应用是一 个很复杂的事情,主要的原因是我们需要合理有效的处理分布式集群中的部分失败的问题。 例如,集群中的节点在相互通信时,A节点向B节点发送消息。A节点如果想知道消息是否发 送成功,只能由B节点告诉A节点。那么如果B节点关机或者由于其他的原因脱离集群网络, 问题就出现了。A节点不断的向B发送消息,并且无法获得B的响应。B也没有办法通知A节点 已经离线或者关机。集群中其他的节点完全不知道B发生了什么情况,还在不断的向B发送消 息。这时,你的整个集群就发生了部分失败的故障。 Zookeeper不能让部分失败的问题彻底消失,但是它提供了一些工具能够让你的分布式应用安 全合理的处理部分失败的问题。 4
安装和运行Zookeeper 安装和运行Zookeeper 我们采用standalone模式,安装运行一个单独的zookeeper服务。安装前请确认您已经安装了 Java运行环境。 我们去Apache ZooKeeper releases page下载zookeeper安装包,并解压到本地: % tar xzf zookeeper-x.y.z.tar.gz ZooKeeper提供了一些可执行程序的工具,为了方便起见,我们将这些工具的路径加入到 PATH环境变量中: % export ZOOKEEPER_HOME=~/sw/zookeeper-x.y.z % export PATH=$PATH:$ZOOKEEPER_HOME/bin 运行ZooKeeper之前我们需要编写配置文件。配置文件一般在安装目录下的 conf/zoo.cfg 。 我们可以把这个文件放在 /etc/zookeeper 下,或者放到其他目录下,并在环境变量设 置 ZOOCFGDIR 指向这个个目录。下面是配置文件的内容: tickTime=2000 dataDir=/Users/tom/zookeeper clientPort=2181 tickTime是zookeeper中的基本时间单元,单位是毫秒。datadir是zookeeper持久化数据存放 的目录。clientPort是zookeeper监听客户端连接的端口,默认是2181. 启动命令: % zkServer.sh start 我们通过 nc 或者 telnet 命令访问 2181 端口,通过执行ruok(Are you OK?)命令来检查 zookeeper是否启动成功: % echo ruok | nc localhost 2181 imok 那么我看见zookeeper回答我们“I’m OK”。下表中是所有的zookeeper的命名,都是由4个字符 组成。 5
安装和运行Zookeeper Command Description Prints imok if the server is running and not in an error state. Prints the server configuration (from zoo.cfg). Prints the server environment, including ZooKeeper version, Java version, and other system properties. Prints server statistics, including latency statistics, the number of znodes, and the server mode (standalone, leader, or follower). Prints server statistics and connected clients. Resets server statistics. Shows whether the server is in read-only (ro) mode (due to a network partition) or read/write mode (rw). Lists all the sessions and ephemeral znodes for the ensemble. You must connect to the leader (see srvr) for this command. Lists connection statistics for all the server’s clients. Resets connection statistics. Lists summary information for the server’s watches. Lists all the server’s watches by connection. Caution: may impact server performance for a large number of watches. Lists all the server’s watches by znode path. Caution: may impact server performance for a large number of watches. Lists server statistics in Java properties format, suitable as a source for monitoring systems such as Ganglia and Nagios. Category Server status Client connections Watches ruok conf envi srvr stat srst isro dump cons crst wchs wchc wchp Monitoring mntr 3.5.0以上的版本会有一个内嵌的web服务,通过访问 http://localhost:8080/commands 来访问 以上的命令列表。 6
Zookeeper开发实例 Zookeeper开发实例 这一节我们将讲解如何编写Zookeeper客户端的程序,来控制zookeeper上的数据,以达到管 理客户端所在集群的成员关系。 7
ZooKeeper中的组和成员 ZooKeeper中的组和成员 我们可以把Zookeeper理解为一个高可用的文件系统。但是它没有文件和文件夹的概念,只有 一个叫做znode的节点概念。那么znode即是数据的容器,也是其他节点的容器。(其实 znode就可以理解为文件或者是文件夹)我们用父节点和子节点的关系来表示组和成员的关 系。那么一个节点代表一个组,组节点下的子节点代表组内的成员。如下图所示: 8
分享到:
收藏