preface

ZooKeeper: I’m sure you’ve heard this word before. How much do you know about ZooKeeper? ZooKeeper can also serve as a registry. ZooKeeper can be used as an implementation of distributed locks.

Until I learned about Kafka, I realized that Kafka also relies on ZooKeeper. Kafka uses ZooKeeper to manage its own metadata configuration.

This article is to write my notes on learning ZooKeeper. I hope you can point out any mistakes in the comments section.

What is ZooKeeper

From the above, we can also find that there seems to be ZooKeeper everywhere. What is ZooKeeper? Let’s go to the official website to see the introduction:




ZooKeeper official website

Here’s another quote from the website:

ZooKeeper: A Distributed Coordination Service for Distributed Applications

I actually like ZooKeeper more than it does on the official website:



The wiki is introduced to ZooKeeper

(Tears of not knowing English)

Let me summarize this briefly:

  • ZooKeeper mainly serves distributed systems. You can use ZooKeeper to perform the following operations: unified configuration management, unified naming service, distributed lock, and cluster management.
  • Distributed system cannot avoid the problems of node management (real-time perception of node status, unified management of nodes, etc.), and because these problems may be relatively troublesome to deal with and improve the complexity of the system, ZooKeeper emerged as a middleware that can solve these problems in general.

Why can ZooKeeper do so much?

ZooKeeper provides unified configuration management, unified naming service, distributed locking, and cluster management.

  • We will leave out the specific meaning of unified configuration management, unified naming service, distributed locking, and cluster management (more on that later).

So why does ZooKeeper do so many things? ZooKeeper: ZooKeeper: ZooKeeper: ZooKeeper: ZooKeeper

ZooKeeper nodes store their data in a hierarchical name space, much like a file system or a tree data structure

ZooKeeper’s data structure is similar to that of a Unix file system
The tree, each node is called
ZNode. Each node can pass through
The pathThe structure diagram is as follows:




ZooKeeper structure

What are the characteristics of ZooKeeper? ZooKeeper nodes are called ZNodes, which are divided into two types:

  • Ephemeral: When the client and server are disconnected, the Created ZNodes are automatically deleted
  • Persistent: ZNodes created when the client and server are disconnected are not deleted

ZooKeeper, like Redis, is a C/S architecture (client and server).



Znode and the type of Znode

2.1 the listener

ZooKeeper also uses listeners to do so many things.

Common listening scenarios are as follows:

  • Listen for Znode data changes
  • Listen for changes in child nodes




Listen for data changes on the Znode



Listen for changes in child nodes

That’s right, ZooKeeper can do just that by listening on the +Znode (persistent/transient/temporary) node.

How does ZooKeeper do it?

Here’s how to use ZooKeeper: Unified configuration management, unified naming service, distributed locking, and cluster management.

3.1 Unified Configuration Management

For example, we now have three systems A, B and C, and they have three configurations, namely asystem. yml, bSystem. yml and csystem. yml.

  • At this point, if we change the information for one of the configuration items, it is likely that we will change the information for the other two items. Also, changing information about configuration items may require a system reboot

Yml, BSystem. Yml, and CSystem. Yml can be extracted into A common configuration common.




Systems A, B, and C all use this configuration

How to do this: We can put the common.yml configuration in the Znode of ZooKeeper. System A, B, and C monitor the Znode for changes and respond promptly if changes are made.



Systems A, B, and C monitor ZooKeeper nodes and respond to changes in common.yml

References:

  • Unified configuration management based on ZooKeeper
  • Blog.csdn.net/u011320740/…

3.2 Unified Naming Service

In fact, the unified Naming service is the same as the domain name, we give a name to this part of the resource, others can get the corresponding resource through this name.

For example, now I have a domain name www.java3y.com, but I have multiple machines under this domain name:

  • 192.168.1.1
  • 192.168.1.2 instead
  • 192.168.1.3
  • 192.168.1.4

Other people can access my machine by visiting www.java3y.com, not by IP.




Access the IP address of the flag by name

3.3 Distributed Lock

The concept of lock in this I will not say, if the concept of lock is not familiar with the students, can refer to the following article

  • Java lock? Distributed lock? Optimistic locking? Row locks?

We can use ZooKeeper to implement distributed locks. How does that work? Here’s a look:

Systems A, B, and C all access the /locks node




Systems A, B, and C all access the LOCKS node

Sequential EPHEMERAL_SEQUENTIAL nodes are created upon access, for example, the ID_000000 node is created on system A, the ID_000002 node is created on system B, and the ID_000001 node is created on system C.




Create a node with a temporary sequence number

Then, get all the children under the/LOCKS node (ID_000000, ID_000001, ID_000002) and determine if you are creating the smallest node

  • If so, get the lock.
  • Release lock: After this operation, delete the created node
  • If it is not, it listens for changes in nodes 1 smaller than it is

Here’s an example:

  • System A gets all the children under the/LOCKS node, compares them, and finds that it (ID_000000) is the smallest of all the children. So I get the lock
  • System B gets all the children under the/LOCKS node and, after comparing them, finds that it (ID_000002) is not the smallest of all the children. Therefore, listen for the status of the node ID_000001 that is 1 smaller than the node
  • System C gets all the children under the/LOCKS node, compares them, and finds that it (ID_000001) is not the smallest of all the children. So listen for the status of the node ID_000000 that is 1 smaller than you
  • After system A completes the operation, delete the node created by system A (ID_000000). By listening, system C finds that the id_000000 node has been deleted and finds that it is the smallest node, so it successfully obtains the lock
  • … . System B is shown as follows

3.4 Cluster Status

After the examples above, it’s easy to imagine how ZooKeeper can “sense” the dynamic addition or deletion of nodes.

Take our three systems A, B, and C as examples to create temporary nodes in ZooKeeper:




Each maintains a temporary node

If system A fails, the /groupMember/A node will be deleted. By listening on the child nodes of groupMember, systems B and C can sense that system A has failed. (The same goes for new ones)

ZooKeeper can also dynamically elect a Master. (If the cluster is in master/slave mode)

The principle is simple. If you want to dynamically elect a Master, the type of Znode is EPHEMERAL_SEQUENTIAL.

  • Zookeeper elects the lowest number as the Master. If the Master fails, the Corresponding Znode is automatically deleted. Then let the new minimum number be the Master, so that the dynamic election function can be implemented.

The last

ZooKeeper through the Znode node type + listening mechanism to achieve so many useful functions!

Of course, ZooKeeper is not so simple to consider, if there is a chance to further, I will continue to share, I hope this article is helpful to you. If you have any questions, please comment or private message me oh





Author: Count Java


Link: https://www.jianshu.com/p/024905c35a1f


Source: Jane Book


Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.