The last time I configured the zooKeeper distribution, there was a very important file zoo. CFG, which mainly configured several information clientPort, server.1 port numbers. There are actually two port numbers used: 2181,2888, and then 2888 which is the alternate port number. Some people say that ZK is the cornerstone of the Internet. It is no exaggeration. Source: github.com/limingios/n… [they are]

(1) Restatement of principle

Zk itself is a server, it is directly with the client is a long link (TCP), through port 2181. Distributed with multiple ZKS, communication between them is through 2188, 2888 can also be used if the network jitter. Org. Apache. Zookeeper ClientCnxn is management network I/o module.

  • 1.1 Starting the ZK (Distributed)
cd~ / zookeeper - 3.4.10 / bin sh zkServer. Sh startCopy the code

  • 1.2 enter the zk
Sh zkCli. Sh - server 192.168.69.101:2181192168 69.102:2181192168 69.103:2181Copy the code

  • 1.3 the zk command

Check the command

help
Copy the code

(2) ZooKeeper server

  • 2.1 Common scripts in the bin directory

CMD is Windows, sh is Linux

  • 2.2 Going to the bin directory

Run the zkserver. sh start command to start the service

  • 2.3 Service Status

Run the JPS command to check whether the QuorumPeerMain process exists. Zookeeper has been started

jps
sh zkServer.sh status
Copy the code

  • 2.4 Stopping Services

Run the zkserver. sh stop command in the bin directory to stop the service

sh zkServer.sh stop
jps
Copy the code

Zookeeper client

  • 3.1 Connecting a Client to a Server
sh zkCli.sh -server ip:port
# If there are multiple servers
sh zkCli.sh -server ip1:port1,ip2:port2,ip3:port3
Copy the code

  • 3.2 Creating a Node

Using the create command, you can create a Zookeeper node

Create [-s] [-e] path data acl -s or -e specifies node features, sequential nodes, or temporary nodes. If this parameter is not specified, persistent nodes are specified. Acls are used for permission control

  • 3.2.1 Creating sequence Nodes

Use the command to create the zK-test sequence node. You can see that a string of numbers is added after the created ZK-test node to distinguish it.

 create -s /zk-test 123
Copy the code

  • 3.2.2 Creating a Temporary Node

Run the zK-temp command to create a temporary node

 create -e /zk-temp 123
Copy the code

The temporary node will be automatically deleted after the client session ends. Run the quit command to exit the client.

  • 3.2.2 Creating a Permanent Node

Run the zk-permanent command to create a permanent zk-permanent node

 create /zk-permanent 123 
Copy the code

You can see that the permanent node, unlike the sequential node, is not automatically followed by a string of numbers.

  • 3.3 Reading A Node

The ls command and get command are used to read information. The ls command lists information under the specified Zookeeper node

For all child nodes, you can only view all child nodes of the first level under the specified node. The get command is used to obtain data content and property information about a specified Zookeeper node. The usage is as follows: ls path [watch] get path [watch] ls2 path [watch] To obtain all child nodes under the root node, run ls /

1. To obtain all child nodes under the root node, run ls / 2. To obtain the data content and attribute information of the root node, run the get/command. You can also run the ls2 / command to view ls2=ls+get 4. To view an attribute, use get/name

You can see that the data content is 123, and there are other attributes.

  • 3.4 Updating a Node

Set path data [version] Where data is the new content to be updated, version is the data version. For example, update the data of the /zk-permanent node to 456. You can use the following command:

set /zk-permanent 456
Copy the code

Datspanning has now changed to 1, indicating an update.

  • 3.5 Deleting a Node

To delete a specified Zookeeper node, run the delete command as follows

Delete path [version] In the command, version also indicates the data version. Run the delete /zk-permanent command to delete the /zk-permanent node

delete /zk-permanent
Copy the code

You can see that the /zk-permanent node is successfully deleted. Note that if a node to be deleted has child nodes, the node cannot be deleted. The child node must be deleted before the parent node is deleted.

  • 3.5 the Znode structure

Stat: indicates the status or version

  • 3.6 Node Types

Two types, four types persistent, temporary, persistent and ordered, temporary and ordered

  • 3.7 serial number

The sequence identifier is set when creating a ZNode, with a value appended to the zNode name

An ordinal is a monotonically increasing counter maintained by the parent node. In a distributed system, an ordinal can be used to globally sort all events so that clients can infer the order of events from the ordinal.

  • 3.8 Access Control List (ACL) Permission

Built-in ACL Schemes:

  • 3.9 Watcher

  • 3.10 ZAB agreement

At the core of Zookeeper is atomic broadcasting, which ensures synchronization between servers. The protocol that implements this is called the Zab protocol. Zab protocol has two modes: recovery mode and broadcast mode. Zab goes into recovery mode when the service starts or after the leader crashes, and the recovery mode ends when the leader is elected and most of the servers are in sync with the leader state. State synchronization ensures that the leader and Server have the same system state. Once the leader has synchronized the status of most followers, he can start to broadcast messages. When a server is added to the ZooKeeper service, it starts in recovery mode, discovers the Leader, and synchronizes the status with the Leader. When the synchronization ends, it also participates in the message broadcast.

The Zookeeper service remains Broadcast until the Leader crashes or the leader loses most of the followers. The broadcast mode needs to ensure that proposals are processed sequentially, so ZK uses an incremented transaction ID number (ZXID) to ensure that. All proposals had zxIDS added to them when they were put forward. In the implementation, the ZXID is a 64 digit number, whose 32 bits higher is the epoch, which is used to identify whether the leader relationship changes. Each time a leader is elected, it will have a new epoch. The lower 32 bits are an increasing count. When the leader crashes or loses most of the followers, zK enters recovery mode. In recovery mode, a new leader needs to be elected to restore all the servers to a correct state.

  • 3.11Leader election Process

LOOKING, FOLLOWING, LEADING, OBSERVING

PS: Key principles and basic commands. Zookeeper is a cluster that has a Leader, follower, and Observer relationship. The ZooKeeper cluster is connected through Seesion (TCP long link). After the client link, you can add, delete, modify, and query nodes (stored in ZNode on ZooKeeper). There are four types of ZNodes: temporary, temporary ordered, persistent, and persistent ordered. When adding, deleting, modifying, or querying a Znode, we can monitor its actions (Watcher mechanism) and set permissions to access the node.