zookeeper-简介

简介
ZooKeeper: A Distributed Coordination Service for Distributed Applications
zooKeeper:一个分布式应用的分布式协调服务
ZooKeeper is a distributed, open-source coordination service for distributed applications. It exposes a simple set of primitives that distributed applications can build upon to implement higher level services for synchronization, configuration maintenance, and groups and naming. It is designed to be easy to program to, and uses a data model styled after the familiar directory tree structure of file systems. It runs in Java and has bindings for both Java and C.
zooKeeper是一个分布式应用的分布式,开源,协调服务。它提供了一组简单的原语,分布式应用可以在这些原语基础上实现更高级的服务,用于同步,配置维护,组合命名。它被设计易于使用,并使用了风格和文件系统相似的数据模型。它在java运行,并具有java和c绑定。
Coordination services are notoriously hard to get right. They are especially prone to errors such as race conditions and deadlock. The motivation behind ZooKeeper is to relieve distributed applications the responsibility of implementing coordination services from scratch.
协调服务是出了名的难写。他们特别容易出现竞争条件和死锁错误。zookeeper背后的动机是为了减少分布式应用程序从头实现协调服务的责任。
Design Goals
ZooKeeper is simple. ZooKeeper allows distributed processes to coordinate with each other through a shared hierarchical namespace which is organized similarly to a standard file system. The namespace consists of data registers - called znodes, in ZooKeeper parlance - and these are similar to files and directories. Unlike a typical file system, which is designed for storage, ZooKeeper data is kept in-memory, which means ZooKeeper can achieve high throughput and low latency numbers.
zooKeeper是简单的。zooKeeper允许分布式进程通过共享的层次命名空间相互协调,该命名空间类似于标准文件系统。命名空间由数据寄存器注册,用zooKeeper的话说叫znodes,类似于文件系统中的目录和文件。不像典型的文件系统,zookeeper是设计用于保存在内存中,意味着zooKeeper可以实现高吞吐量和低延迟率。
The ZooKeeper implementation puts a premium on high performance, highly available, strictly ordered access. The performance aspects of ZooKeeper means it can be used in large, distributed systems. The reliability aspects keep it from being a single point of failure. The strict ordering means that sophisticated synchronization primitives can be implemented at the client.
ZooKeeper实施对高性能,高可用性,严格有序访问加以重视。 ZooKeeper的性能方面意味着它可以在大型的分布式系统中使用。 可靠性方面使它不会成为单点故障。 严格排序意味着可以在客户端上实现复杂的同步原语。
ZooKeeper is replicated. Like the distributed processes it coordinates, ZooKeeper itself is intended to be replicated over a set of hosts called an ensemble.
ZooKeeper是复制的。与它所协调的分布式进程一样,ZooKeeper本身也打算在一组称为集合的主机上进行复制。
The servers that make up the ZooKeeper service must all know about each other. They maintain an in-memory image of state, along with a transaction logs and snapshots in a persistent store. As long as a majority of the servers are available, the ZooKeeper service will be available.
组成ZooKeeper服务的服务器必须相互了解。它们在内存中维护状态映像,以及持久存储中的事务日志和快照。只要大多数服务器可用,ZooKeeper服务就可用。
Clients connect to a single ZooKeeper server. The client maintains a TCP connection through which it sends requests, gets responses, gets watch events, and sends heart beats. If the TCP connection to the server breaks, the client will connect to a different server.
客户端连接到单个ZooKeeper服务器。客户端维护一个TCP连接,通过它发送请求、获取响应、获取监视事件和发送心跳。如果到服务器的TCP连接中断,客户机将连接到另一台服务器。
ZooKeeper is ordered. ZooKeeper stamps each update with a number that reflects the order of all ZooKeeper transactions. Subsequent operations can use the order to implement higher-level abstractions, such as synchronization primitives.
Zookeeper是有序的。ZooKeeper用一个数字来标记每个更新,这个数字反映了所有ZooKeeper事务的顺序。后续操作可以使用该顺序实现更高级别的抽象,比如同步原语。
ZooKeeper is fast. It is especially fast in "read-dominant" workloads. ZooKeeper applications run on thousands of machines, and it performs best where reads are more common than writes, at ratios of around 10:1.
ZooKeeper很快。在“以读取为主”的工作负载中,它的速度特别快。ZooKeeper应用程序运行在数千台机器上,当读操作比写操作更常见时,它的性能最好,比率约为10:1。
Data model and the hierarchical namespace
The namespace provided by ZooKeeper is much like that of a standard file system. A name is a sequence of path elements separated by a slash (/). Every node in ZooKeeper's namespace is identified by a path.
数据模型和分层名称空间

ZooKeeper提供的名称空间与标准文件系统的名称空间非常相似。 名称是由斜杠(/)分隔的一系列路径元素。 ZooKeeper命名空间中的每个节点都由路径标识。
Nodes and ephemeral nodes
Unlike standard file systems, each node in a ZooKeeper namespace can have data associated with it as well as children. It is like having a file-system that allows a file to also be a directory. (ZooKeeper was designed to store coordination data: status information, configuration, location information, etc., so the data stored at each node is usually small, in the byte to kilobyte range.) We use the term znode to make it clear that we are talking about ZooKeeper data nodes.

Znodes maintain a stat structure that includes version numbers for data changes, ACL changes, and timestamps, to allow cache validations and coordinated updates. Each time a znode's data changes, the version number increases. For instance, whenever a client retrieves data it also receives the version of the data.

The data stored at each znode in a namespace is read and written atomically. Reads get all the data bytes associated with a znode and a write replaces all the data. Each node has an Access Control List (ACL) that restricts who can do what.

ZooKeeper also has the notion of ephemeral nodes. These znodes exists as long as the session that created the znode is active. When the session ends the znode is deleted.
节点和短暂节点

与标准文件系统不同,ZooKeeper命名空间中的每个节点都可以具有与其关联的数据以及子节点。 就像拥有一个文件系统一样,该文件系统也允许文件成为目录。 (ZooKeeper旨在存储协调数据:状态信息,配置,位置信息等,因此存储在每个节点上的数据通常很小,在字节到千字节范围内。)我们使用术语znode来明确表示 在谈论ZooKeeper数据节点。

Znodes维护一个统计信息结构,其中包括用于数据更改,ACL更改和时间戳的版本号,以允许进行缓存验证和协调更新。 znode的数据每次更改时,版本号都会增加。 例如,每当客户端检索数据时,它也接收数据的版本。

原子地读取和写入存储在名称空间中每个znode上的数据。 读取将获取与znode关联的所有数据字节,而写入将替换所有数据。 每个节点都有一个访问控制列表(ACL),用于限制谁可以执行操作。

ZooKeeper还具有短暂节点的概念。 只要创建znode的会话处于活动状态,这些znode就存在。 会话结束时,将删除znode。

Conditional updates and watches
ZooKeeper supports the concept of watches. Clients can set a watch on a znode. A watch will be triggered and removed when the znode changes. When a watch is triggered, the client receives a packet saying that the znode has changed. If the connection between the client and one of the ZooKeeper servers is broken, the client will receive a local notification.

条件更新和手表ZooKeeper支持手表的概念。客户端可以在znode上设置手表。当znode发生变化时,将触发并删除一个手表。当一个手表被触发时,客户端会收到一个数据包,说znode已经改变了。如果客户端和一个ZooKeeper服务器之间的连接断开,客户端将收到一个本地通知。
安装zookeeper

Standalone Operation

1.下载zookeeper,通过fileZile上传上去
tar -zxvf apache-zookeeper-3.6.0-bin.tar.gz
2.安装java
3.将解压后的文件名重命名
mv apache-zookeeper-3.6.0-bin zookeeper
并移动至/usr/lib
mv /tmp/zookeeper/ /usr/lib
Standalone Operation
单击演示

4.生成一个zoo.cfg
vi conf zoo.cfg
​```
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
​```
启动
sh bin/zkServer.sh start
5.测试
sh bin/zkCli.sh -server 127.0.0.1:2181
ls /
[ zookeeper]
Next, create a new znode by running create /zk_test my_data. This creates a new znode and associates the string "my_data" with the node. You should see:
[zkshell: 9] create /zk_test my_data
Created /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 0] ls /
[zk_test, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 3] get /zk_test
my_data

[zk: 127.0.0.1:2181(CONNECTED) 5] get -s /zk_test
my_data
cZxid = 0x3
ctime = Wed Apr 22 18:33:23 CST 2020
mZxid = 0x3
mtime = Wed Apr 22 18:33:23 CST 2020
pZxid = 0x3
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 6] set /zk_test junk
[zk: 127.0.0.1:2181(CONNECTED) 7] get -s /zk_test
junk
cZxid = 0x3
ctime = Wed Apr 22 18:33:23 CST 2020
mZxid = 0x7
mtime = Fri Apr 24 17:48:13 CST 2020
pZxid = 0x3
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
可以看到mTime被改变了,以及znode的值
[zk: 127.0.0.1:2181(CONNECTED) 8] delete /zk_test
删除/zk_test
[zk: 127.0.0.1:2181(CONNECTED) 9] get -s /zk_test
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /zk_test

集群模式 后续再介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值