How Much Do You Know about Distributed Coordination Service Zookeeper?
Zookeeper profile
Since we learned about a distributed framework (dubbo), which involved zookeeper, let’s start with a brief introduction to zookeeper.Zookeeper is a distributed coordination service that manages a large number of hosts.
1. Distributed applications
Distributed applications can perform specific tasks by coordinating them between them, and a fast and effective way of running networks across multiple systems at a given time (and at the same time)
Distributed applications have two parts: the server and the client application.As shown in the figure below:
2. Advantages of distributed applications
Reliability extensibility transparency
3. Services provided by zookeeper
Naming Service Configuration management Cluster management Node leader elections Locking and synchronization services Data registry
Basics of ZooKeeper
1. The architecture of ZooKeeper
Describe the “client-server architecture” of ZooKeeper, as shown below:
Some of the components of the ZooKeeper architecture are explained in the table below.
(1) Client: Client, send message to server.
(2) Server: a node of Server, ZooKeeper integration, providing all services to customers.
(3) Group: ZooKeeper server group.
(4) Leader: it performs automatic recovery, if any node of the connection fails the server node.
(5) Follower: follow the leader to indicate the server node
2. Hierarchical namespace
The following figure shows the tree structure used to represent the ZooKeeper file system in memory. The ZooKeeper node is called znode. Each znode is identified by a name and separated by the path (/) sequence.
The name space of zookeeper is made up of node znode, whose organization is similar to the file system, where each node corresponds to the directory and file, and the path is the unique identification. Unlike the file system, each node has the corresponding data content, and it can also have child nodes. Each znode in the ZooKeeper data model maintains a stat structure. A statistics (stat) just provides a znode metadata. It consists of version number, action control list (ACL), timestamp, and data length.
ZooKeeper components
There are two types of server under the same zookeeper service, one is leader server and the other is follower server. Leader is special because it has the power to decide. Each server under zookeeper’s entire service copies each component. The Replicated Database is a memory Database that contains all of the data.
Zookeeper as leader
Let’s analyze the election of a leading node in ZooKeeper collection. Consider that there are more than N nodes in the cluster. The process of leadership elections is as follows:
All nodes create a sequence, znode has the same path, / app/leader/guid_.
The collection of ZooKeeper will append the path of the additional 10 sequence Numbers.
For a given instance, it creates a minimum number of nodes in znode to become a leader and a follower of all other nodes.
Each follower node monitors the next smallest znode.
Zookeeper installation configuration
1. Installation of Java (abbreviated)
2. Installation of the ZooKeeper framework
(1) Download and tar open pressure (abbreviated)
(2) Create a configuration file open and edit conf/zoo. The CFG configuration files, and all of the following parameter is set to the starting point.
1 2 3 4 5 | tickTime = 2000 dataDir = /path/to/zookeeper/data clientPort = 2181 initLimit = 10 syncLimit = 5 |
(3) Launch the ZooKeeper server
1 | $ bin/zkServer.sh start |
(4) Start the CLI
1 | $ bin/zkCli.sh |
(5) Stop ZooKeeper server
1 | $ bin/zkServer.sh stop |
Zookeeper CLI
The ZooKeeper command-line interface (CLI) is used to interact with ZooKeeper integration. This is useful when debugging and using different options.
In order to perform the ZooKeeper CLI operation, the ZooKeeper server first to start (” bin/zkServer. Sh start “), and then use the ZooKeeper client (” bin/zkCli. Sh “). When the client starts, you can do the following: (1) create znodes, (2) to get the data, and (3) monitoring znode change, (4) set data, (5) create a znode znode, (6) to list a znode znode, check status (7), (8) to delete a znode.
1. Create Znodes
1 | create /path /data |
2. Get data
1 | get /path |
3. Monitor
1 | get /path [watch] 1 |
4. Set Data
1 | set /path /data |
5. Create child znode
1 | create /parent/path/subnode/path /data |
6. List child znode
1 | ls /path |
7. check mode
1 | stat /path |
8. delete Znode
1 | rmr /path |
Commonly used API of Zookeeper
ZooKeeper has a Java and C binding official API. 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 ZooKeeper.
1. Basic knowledge of ZooKeeper’s API
The client should follow the following steps to integrate a clear interaction with ZooKeeper.
Connect to ZooKeeper. The ZooKeeper integration assigns the client’s session ID.
Send heartbeat to server regularly. Otherwise, the ZooKeeper integrates the expired session ID, so the client needs to reconnect.
Get/set as long as the znodes session ID is active.
Disconnect from ZooKeeper, when all tasks are completed. If the client is inactive for a long time, the ZooKeeper integration automatically disconnects the client.
2. Java binding
Let’s 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 to ZooKeeper integration in its construction, with the following methods:
• connect − connected to the integration of ZooKeeper
• create − create a znode
• exists − Check that znode exists and information
• getData − Get data from a specific znode
• setData − Set the data to specific znode
• getChildren − Gets all the available child nodes for a particular znode
• delete − Get a specific znode and all its children
• close − close junction
3. Connect to the ZooKeeper collection
The ZooKeeper class provides connectivity through its constructor. The constructor is as follows:
1 | ZooKeeper(String connectionString, int sessionTimeout, Watcher watcher) |
4. Create a Znode
The ZooKeeper class provides a way to create a new znode in a collection of ZooKeeper. Create the following methods:
1 | create(String path, byte[] data, List<ACL> acl, CreateMode createMode) |
5. exists −Check that znode exists and information
The exists method checks the existence of znode. If the specified znode exists, it returns a znode metadata. The exists method is as follows:
1 | exists(String path, boolean watcher) |
6. GetData method
The getData method is used to retrieve data that is connected to the specified znode and its state. The getData method is as follows:
1 | getData(String path, Watcher watcher, Stat stat) |
7. SetData method
The SetData method changes the data attached to the specified znode.The SetData method is as follows:
1 | setData(String path, byte[] data, int version) |
8. GetChildren method
GetChildren method to get a specific znode all child nodes. The getChildren method is as follows:
1 | getChildren(String path, Watcher watcher) |
9. Delete a Znode
The delete method deletes the specified znode. The delete method is as follows:
1 | delete(String path, int version) |