What are the functions of zookeeper

(1) Naming service
Create a directory in the zookeeper's file system, which has a unique path.

(2) Configuration management
Programs always need to be configured. If the programs are deployed on multiple machines, it becomes difficult to change the configuration one by one. Ok, now put all these configurations on Zookeeper, save them in a certain directory node of Zookeeper, and then all related applications will listen to this directory node. Once the configuration information changes, each application will receive Zookeeper And then get the new configuration information from Zookeeper and apply it to the system.
Insert picture description here

(3)Cluster management,
check whether any machine exits or joins, and elect the master

Insert picture description here
For the first point, all machines agree to create a temporary directory node under the parent directory GroupMembers, and then monitor the child node change messages of the parent directory node.

Once a machine hangs up, the machine is disconnected from zookeeper, the temporary directory node it created is deleted, and all other machines are notified: a brother directory has been deleted, so everyone knows: it’s on board Up.

The addition of new machines is similar, all machines receive notifications: the new brother directory has been added, and highcount has been added again.

For the second point, let's change it a bit. All machines create temporary sequential numbered directory nodes, and each time the machine with the smallest number is selected as the master.

(4) Distributed locks
With the consistent file system of zookeeper, the problem of locks becomes easy. Lock services can be divided into two categories, one is to keep exclusive, and the other is to control timing.

For the first category, we regard a znode on zookeeper as a lock, which is implemented by createznode. All clients create the /distribute_lock node, and the client that is successfully created finally owns the lock. The toilet has a saying: Flush when you come, flush when you go, and release the lock when you use up and delete the distribute_lock node you created.

For the second type, /distribute_lock already exists in advance, and all clients create temporary sequential numbered directory nodes under it. Just like selecting master, the one with the smallest number gets the lock, and it is deleted when it is used up, which is convenient in order.

Insert picture description here

(5) Queue management
There are two types of queues:
1. Synchronous queue, when the members of a queue are all gathered, the queue is available, otherwise it has been waiting for members to arrive.

2. The queue enters and dequeues in FIFO mode.
The first type is to create temporary directory nodes under the agreed directory and monitor whether the number of nodes is the number we require.

The second category is consistent with the basic principle of the control sequence scenario in the distributed lock service. The entry is numbered, and the exit is numbered.

Guess you like

Origin blog.csdn.net/weixin_55580097/article/details/114112252