How much do you know about the distributed coordination service zookeeper

Introduction to zookeeper
Since I learned about a distributed framework (dubbo) before, zookeeper is involved, so today I will briefly introduce zookeeper. Zookeeper is a distributed coordination service used to manage a large number of hosts.
(1) Distributed applications
Distributed applications can perform specific tasks by coordinating among them, in a fast and efficient manner, in a network of multiple systems at a given time (simultaneously)
There are two types of distributed applications. parts, namely: server and client applications. As shown in the following figure:



(2) Advantages of distributed applications,
reliability , scalability, transparency
(3) Services provided by zookeeper,
naming service configuration management, cluster management node leader election, locking and synchronization service data registry
ZooKeeper basics
(1) The Architecture of ZooKeeper
Depicts the "client-server architecture of ZooKeeper, as shown in the figure below.




Part of the components of the ZooKeeper architecture are explained in the following table.
1. Client: Client, which sends messages to the server.
2. Server: Server, which is integrated by ZooKeeper A node that provides all services to clients.
3. Group: a ZooKeeper server group.
4. Leader: a server node that performs automatic recovery if any connected node fails.
5. Follower: a server node that follows the leader instructions
(two ) Hierarchical namespace
The following figure shows the tree structure used to represent the ZooKeeper file system in memory. ZooKeeper nodes are called znodes. Each znode is identified by a name and is separated by a sequence of paths (/).



The zookeeper namespace is composed of node znodes, which are organized like a file system, in which each node is equivalent to a directory and a file, and the path is used as a unique identifier. Different from the file system, each node has its corresponding data content and can also have child nodes. Each znode maintains a stat structure in the ZooKeeper data model. A stat (stat) just provides a znode metadata. It consists of version number, action control list (ACL), timestamp, data length.
ZooKeeper component There are two types of servers under the
same zookeeper service, one is the leader server and the other is the follower server. The special thing about the leader is that it has the right to decide. Each server under the entire zookeeper service will replicate each component.
Replicated Database is the leader of ZooKeeper , an in-memory database that contains all data.
Let's analyze the election of a leader node in the ZooKeeper ensemble. Consider that there are more than N nodes in the cluster. The process of leader election is as follows
All nodes are created in an order, znodes have the same path, /app/leader/guid_.
The path to the 10-digit sequence number that ZooKeeper's ensemble will append.
For a given instance, it creates a minimum number of nodes on the znode to be the leader and followers of all other nodes.
Each follower node monitors the next smallest znode.

Zookeeper installation configuration
(1) Install Java (omitted)
(2) Installation of ZooKeeper framework
1. Download and tar unzip (omitted)
2. Create configuration file
Open and edit the conf/zoo.cfg configuration file, and set all the following parameters as the starting point.
tickTime = 2000
dataDir = /path/to/zookeeper/data
clientPort = 2181
initLimit = 10
syncLimit = 5
3. Start ZooKeeper server
$ bin/zkServer.sh start
4. Start CLI
$ bin/zkCli.sh
5. Stop ZooKeeper server
$ bin/zkServer.sh stop
Zookeeper CLI The
ZooKeeper Command Line Interface (CLI) is used to interact with the ZooKeeper ensemble for development. This is useful when debugging and working with different options.
To perform ZooKeeper CLI operations, the ZooKeeper server must first be started (“bin/zkServer.sh start”), and then the ZooKeeper client (“bin/zkCli.sh”). Once the client is started, the following operations can be performed: ( 1) create znodes, (2) get data, (3) monitor znode changes, (4) set data, (5) create child znodes of znode, (6) list child znodes of a znode, (7) check status, (8) Delete a znode
(1) Create Znodes
create /path /data
(2) Get data
get /path
(3) Monitor
get /path [watch] 1
(4) Set data
set /path /data
(5) Create child znode
create /parent/path /subnode/path /data
(6) List sub znodes
ls /path
(7) Check status
stat /path
(8) Delete Znode
rmr /path

Zookeeper Common API
ZooKeeper has an official API bound by Java and C. The ZooKeeper community provides unofficial APIs for most languages ​​(.NET, Python, etc.). Using the ZooKeeper API, applications can connect, interact, manipulate data, coordinate, and disconnect from the ZooKeeper ensemble.
(1) API Basics of ZooKeeper
Clients should follow the steps given below to integrate a clear interaction with ZooKeeper.
Connect to ZooKeeper. The ZooKeeper ensemble assigns the client's session ID.
Periodically send heartbeats to the server. Otherwise, ZooKeeper integrates expired session IDs and the client needs to reconnect.
Get/set as long as the znodes session id is active.
Disconnect from the ZooKeeper ensemble when all tasks have completed. The ZooKeeper ensemble automatically disconnects the client if the client is inactive for an extended period of time.

(2) Java bindings
Let us understand the most important ZooKeeper API in this chapter. The central part of the ZooKeeper API is the ZooKeeper class. It provides some options to connect the ZooKeeper ensemble in its construction, there are the following methods:
connect − connect to the ZooKeeper ensemble
create − create a znode
exists − check if the znode exists and its information
getData − from a specific znode Get data
• setData - set data in a specific znode
• getChildren - get all available children of a specific znode
• delete - get a specific znode and all its children
• close - close the

connection
Its constructor provides connection functionality. The constructor is as follows:
ZooKeeper(String connectionString, int sessionTimeout, Watcher watcher)

(4) Create a Znode The
ZooKeeper class provides a method to create a new znode in the set ZooKeeper. The creation method is as follows:
create(String path, byte[] data, List<ACL> acl, CreateMode createMode)

(5) Exists – check the existence of a znode The
exists method checks the existence of a znode. If the specified znode exists it returns a znode metadata. The exists method is as follows –
exists(String path, boolean watcher)

(6) getData method
The getData method is used to obtain the data connected to the specified znode and its state. The getData method is as follows:
getData(String path, Watcher watcher, Stat stat)

(7) setData method
The SetData method modifies the data attached to the specified znode. The SetData method is as follows –
setData(String path, byte[] data, int version)

(8) getChildren method
The getChildren method is used to get all the child nodes of a specific znode. The getChildren method is as follows -
getChildren(String path, Watcher watcher)

(9) delete a Znode
delete method to delete the specified znode. The delete method is as follows –
delete(String path, int version)

This article was originally published on Cobub's official website blog, author: He Cai
If you reprint, please indicate the author and source!
Recommend an open source privatized deployment of mobile application data statistical analysis system Cobub Razor
Project address: https://github.com/cobub/razor
official website: www.cobub.com




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987234&siteId=291194637