1. What is Zookeeper?

2. Zookeeper file system?

  • Zookeeper provides a multi-node namespaceznode), these nodes can also set associated data, whereas only file nodes can hold data in the file system, but not directory nodes
  • Zookeeper to ensure high throughput and low latency, is inmemoryZookeeper maintains the tree directory structure. Based on this feature, ZooKeeper cannot store a large amount of data. Each node can store a maximum of 1 MB of data.Data stored in memory, naturally not suitable for storing a large amount of data, more suitable for storing some core key data information)

3. Znodes in ZooKeeper, which fall into three categories

  • Persistent Nodes
  • Ephemeral nodes
  • Sequential node
Persistent: After the client is disconnected from ZooKeeper, the node still maintains the sequence: After the client is disconnected from ZooKeeper, the node automatically deletes the sequence of persistent + temporary + sequence:Copy the code

Zookeeper automatically controls and generates the serial number. The serial number increases according to the created node (for example, there is a Z01 node, and then a node without a serial number is created, and then another node with a serial number is created. So the node name with serial number is z03, and 02 will be skipped.

4. Zookeeper notification mechanism

  • The Client will create a Watcher listening event for a ZNode. When the ZNode changes, ZooKeeper will notify the client, and the client will process its related services according to the change.To realize the Watch, it is necessary to realize the org. Apache. The zookeeper. Watcher interface, wathcer is a one-off event, if you want to continue to monitor, you need to continue to register, server-side words can do a callback, so every time Watcher, is to register a new monitor
  • Zookeeper monitoring, this article describes in detail

5. Application scenarios

1. Unified Naming Service (File System)

  • In a distributed environment, applications and services need to be named in a unified manner for easy identification. For example, an IP address is not easy to remember, but a domain name is easy to remember
  • Naming service refers to obtaining the address of a resource or service by using a specified nameUse ZK to create a global path, that is, a unique path, that can be used as a name to point to a cluster in a cluster, the address of a service, a remote object, and so on.

2. Unified configuration management (file system, notification mechanism)

  • Program distributed deployment in different machines, the program configuration information in the ZK znode, when the configuration changes, that is, when znode changes, you can change the content of a directory node in ZK, using Watcher notification to each client, so as to change the configuration.

3. Unified cluster management

  • Cluster management does not care about two things: whether a machine exits or joins, or elects a master.
  • For the first point, all machines have a convention to create a temporary directory node under the parent directory and then listen for child node change messages from the parent directory node. Once a machine dies, its connection to ZooKeeper is disconnected, the temporary directory node it created is deleted, and all other machines are notified that a sibling directory has been deleted, so everyone knows it’s on board.
  • In the same way, all machines are notified that the new sibling directory has been added, and the highcount is now available again. For the second point, we change it slightly. All machines create a temporary sequentially numbered directory node, and select the machine with the smallest number each time as the master.

4. Soft load balancer

The server obtains the service node of the current system from ZooKeeper and dynamically modifies the background service to be accessed based on the service node (the front-loaded service can do this).

5. Distributed lock

  • Distributed lock implementation details
  • Distributed lock in the final analysis, in fact, or the use of zooKeeper features, through the creation of znode, who first created znode, so who will get the lock.

  • Zookeeper consistency

6. Queue management

6. Message broadcasting

1. Data set writing process

The last

Some of the above is based on the following authors:

  • Juejin. Cn/post / 688016…
  • Juejin. Cn/post / 684490…