Distributed - distributed coordination framework Zookeeper (a) - Basic concepts and common commands

A, ZooKeeper Overview

1.1 What is ZooKeeper

ZooKeeper is a distributed coordination framework. It is a typical distributed data consistency solutions . Distributed applications can be based on ZooKeeper  realize publish such data / subscribe, load balancing, distributed coordination / notification, cluster management, master election, distributed lock, distributed queue functions.

One of the most common scenario is for his service to producers and consumers of the service registry , provided publish - subscribe to the service, for example, in Dubbo served as a registration center in the role:

Dubbo

1.2, combined with the personal circumstances of talk about ZooKeeper

ZooKeeper used in the project as Dubbo registry. In addition, when using the built in solr ZooKeeper cluster as a cluster management tools (three service areas can ZooKeeper cluster ), then ZooKeeper provide the following functions: 1, cluster management: centralized management of fault-tolerant, balanced complex 2, 3 profiles cluster entrance

Why is better to use an odd number of servers constitute ZooKeeper cluster?

Zookeeper so-called fault-tolerant, means that when the server goes down after a few zookeeper, the remaining number must be greater than the number of outages throughout the zookeeper was still available . So the best odd

1.3, Zookeeper role

(1) leader (Leader): origination and vote on the resolution, updates the system status

(2) learners (Learner):

- followers (Follower) : receiving a client request, return the results to the client, to vote in the election process;

- the viewer (the Observer) : receiving client is connected, a write request forwarded to the leader node. But Observer does not participate in the vote, only the synchronous state leader . Observer just to expand the system, increase the reading speed.

(3) client (Client) : the request initiator

 

1.4, Zookeeper (animal Administrator) works

Zookeeper cluster each server on the same data, leader election when the cluster starts, as shown:

Write operation, a request to be sent to a server, the server then forwards the leader, leader (leaders) to each server to send messages to vote, the voting results to each server leader, have a half times serer unified request, leader it will commit to each server to perform the write operation.

 

Write operation Some notes:

(1) write data, a Follower hung up, after the repair will ZAB good repair follower and leader through data coherency protocol to reach a final agreement data on each server;

(2) write data, over a period of time, ZK will put all server data mirroring to write, and then delete the data on each server to ensure that each sever capacity;

(3) write data in a table follower write a certain data, while another follower just read the information written not succeed, because each server data synchronization interval there will be little, so that eventually consistent sex.

Second, some of the important concepts of ZooKeeper

2.1, summarize important concepts

ZooKeeper itself is a distributed program (more than half as long as the surviving node, you can normally service);

To ensure high availability, the best clusters;

ZooKeeper  data stored in memory, which also ensures high throughput and low latency;

ZooKeeper  have temporary node concept. Create a temporary node client session remains active, transient nodes have been present when the session ends transient nodes are removed. Persistent node refers Once this ZNode is destined to create a delete operation unless otherwise has been preserved in the ZK.

2.2, session (Session)

It refers to the ZooKeeper server and client session. In a client ZK refers to a connection between the client and server TCP connection length. First, a TCP connection with the server when the client initiates resume possible to maintain an active session by detecting the heartbeat client services through this link, it is possible to send a request and receive a response ZK.

Before a client to create a session, the server will first client is assigned a sessionID, this globally unique ID

2.3 ZNode

Data model data units called data node ZNode. In the ZK, node can be divided into temporary and persistent node node types. Data can be saved on each ZNode, while still a child.

Persistent Node : Once this ZNode is created, unless otherwise been actively delete saved on ZK;

Temporary node : its life cycle and client session binding , once the client session fails, then all the client creates a temporary nodes have been removed (such as closing the temporary node after starting the background disappears).

2.4, version

Data are stored on each of ZK znode corresponding to each znode, ZK will maintain its data structure of a Start, Stat data is recorded in the three versions of this znode, respectively version (the current version of the data), cversion (current ZNode child node version), aversion (ACL ZNode the current version) .

2.5、ACL

ZK uses ACL policies to control permissions, ZK 5 defines the permissions as follows:

  • CREATE: permission to create child nodes;
  • READ: Get Permissions sub-node data and the list of children;
  • WRITE: update node data rights;
  • DELETE: Removes the child node rights;
  • ADMIN: Setting Node ACL permissions.

Three, ZooKeeper Features

Sequential Consistency : the same request from a client to initiate things, will eventually apply to ZK in order;

Atomic : Either all machines across the cluster have been successfully applied to a certain thing, or do not apply;

Single System Image : No matter where the client is connected on a server ZK, saw its server-side data models are the same;

Reliability : Once a change request is applied, the results of your changes will be persisted until the next change coverage.

 

Four, ZooKeeper design goals

4.1, simple data model

Namespace allows distributed processes to coordinate with each other through a shared hierarchical namespace called ZNode (similar to files and directories). Traditional file systems differ, ZK data stored in memory , which means ZK can achieve high throughput and low latency.

4.2, you can build cluster

ZooKeeper cluster deployment

4.3, sequential access

For each amount of the client's request, ZK is assigned a globally unique incremental number, this number reflects the sequence of operations of all things, this number is also called time stamp --zxid

4.4, high-performance

Reading and writing in particular, little environmental performance, as written will cause synchronization between all the servers into the state

Five, ZooKeeper cluster

ZK does not use a conventional Master / Slave (standby mode), but the introduction of the Leader, Follower and Observer three roles.

ZK all the machines in the cluster to select a Leader election process by a machine called a "Leader" of, Leader not only provides clients with writing services can provide reading services, but the Follower and Observer can only provide reading services. The only difference is the Follower and Observe Observer  machine does not participate in the electoral process of the Leader.

When the Leader server has network outages, crashes anomalies quit, reboot, ZAB agreement will enter recover lost and election of a new Leader server process is as follows:

(1) election period: nodes are in the beginning stages of the election, as long as there is a node of the votes of more than half the votes node, you can choose whichever leader;

(2) discovery stage: followers to communicate with prospective leader, synchronous followers things recently received the proposal;

(3) the synchronization phase: using the latest proposal historic leader of the previous stage to get all the copies synchronized cluster, the synchronization is complete quasi-leader will become a true leader;

(4) Radio Stage: This stage cluster Zookeeper things really provide external services, and lerder can broadcast a message, and if a new node is added, also need to synchronize the new node.

 

Six, ZAB agreement and Paxos algorithm

ZooKeeper not completely Paxos algorithm, but the use of AZB protocol as its core algorithms to ensure data consistency.

6.1, ZAB agreement introduced

ZAB ( Atomic Broadcast ) protocol is a distributed coordination service designed ZooKeeper an atomic broadcast protocol support crash recovery. In the ZK depends ZAB distributed data consistency protocol to achieve , based on the protocol, ZK implemented system architecture for the master to the standby mode to maintain data consistency between copies of each cluster.

Two basic modes 6.2, ZAB protocol: message broadcast and crash recovery

整个框架在启动过程或者Leader服务器出现异常时ZAB就会进入恢复模式并选举产生新的Leader服务器。集群中已有过半机器与该Leader服务器完成同步之后,ZAB协议就会推出恢复模式。其中状态同步就是指数同步,用来保证集群中存在过半的机器能够和Leader服务器的数据状态保持一致。

当集群中有过半的Follower服务器完成了和Leader服务器的状态同步,那么整个服务框架就可以进入消息广播模式了。

 

七、ZooKeeper常用命令

 7.1、测试 ZooKeeper 中的常见操作

(1)连接命令:连接ZooKeeper服务

进入安装 ZooKeeper文件夹的 bin 目录下执行下面的命令连接 ZooKeeper 服务(Linux环境下)

(连接之前首选要确定你的 ZooKeeper 服务已经启动成功)

./zkCli.sh -server 127.0.0.1:2181

(2)help命令:查看常用命令

(3)create 命令:创建节点

通过 create 命令在根目录创建了node1节点,与它关联的字符串是"node1"

[zk: 127.0.0.1:2181(CONNECTED) 34] create /node1 “node1”

通过 create 命令在根目录创建了node1节点,与它关联的内容是数字 123

[zk: 127.0.0.1:2181(CONNECTED) 1] create /node1/node1.1 123
Created /node1/node1.1

(4)set 命令:更新节点数据内容

[zk: 127.0.0.1:2181(CONNECTED) 11] set /node1 "set node1" 

(5)get命令:获取节点数据
可以获取指定节点的数据内容和节点的状态

(如下可看出,通过set命令已经将节点数据内容改为“set node1”)

set node1
cZxid = 0x47
ctime = Sun Jan 20 10:22:59 CST 2019
mZxid = 0x4b
mtime = Sun Jan 20 10:41:10 CST 2019
pZxid = 0x4a
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 1

(6)ls命令:查看某个目录下的子节点

通过 ls 命令查看根目录下的节点:

[zk: 127.0.0.1:2181(CONNECTED) 37] ls /
[dubbo, zookeeper, node1]

通过 ls 命令查看 node1 目录下的节点:

[zk: 127.0.0.1:2181(CONNECTED) 5] ls /node1
[node1.1]

zookeeper 中的 ls 命令和 linux 命令中的 ls 类似, 这个命令将列出绝对路径path下的所有子节点信息(列出1级,并不递归)

(7)stat命令:查看节点状态

[zk: 127.0.0.1:2181(CONNECTED) 10] stat /node1
cZxid = 0x47
ctime = Sun Jan 20 10:22:59 CST 2019
mZxid = 0x47
mtime = Sun Jan 20 10:22:59 CST 2019
pZxid = 0x4a
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 1

(8)ls2命令:查看节点信息和状态

ls2命令更像是ls命令和stat命令的结合。ls2命令返回的信息包括2部分:子节点列表+当前节点的stat信息

[zk: 127.0.0.1:2181(CONNECTED) 7] ls2 /node1
[node1.1]
cZxid = 0x47
ctime = Sun Jan 20 10:22:59 CST 2019
mZxid = 0x47
mtime = Sun Jan 20 10:22:59 CST 2019
pZxid = 0x4a
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 11
numChildren = 1

(9)delete命令:删除节点

Note : If you want to delete a node, then the node must be free of child nodes for the job

[zk: 127.0.0.1:2181(CONNECTED) 3] delete /node1/node1.1

 

 

  Reference: https://zhuanlan.zhihu.com/p/44731983

  Reference: https://zhuanlan.zhihu.com/p/24996631

 

 

Published 52 original articles · won praise 116 · views 50000 +

Guess you like

Origin blog.csdn.net/RuiKe1400360107/article/details/103709430