ZooKeeper service usage and data model

Application Using the ZooKeeper client library Using the ZooKeeper service. The ZooKeeper client interacts with the ZooKeeper cluster.

ZooKeeper’s data model is a hierarchical model. Hierarchical models are common in file systems. Hierarchical model and key-value model are two mainstream data models. ZooKeeper uses the file system model based on the following two considerations:

  1. The tree structure of a file system facilitates the representation of hierarchical relationships between data.
  2. The tree structure of the file system facilitates the allocation of independent namespaces (namespaces) for different applications.

ZooKeeper’s hierarchical model is called a Data Tree. Each Datatree node is called a Znode. Znode differs from a file system in that each node can hold data. Each node has a version. Versions are counted from 0.

The data tree interface

ZooKeeper provides a simplified file system API for accessing data Tree:

  • Use unix-style pathnames to locate ZNodes. For example, /A/X indicates the child node X of ZNode A.
  • Znode only supports full writes and reads of data, not partial writes and reads as common file systems do.
  • All data Tree apis are wait-free. Ongoing API calls do not affect the completion of other apis.
  • Data Tree apis are wait-free operations on file systems and do not directly provide distributed cooperation mechanisms such as locks. However, the Data Tree API is very powerful and can be used to implement a variety of distributed collaboration mechanisms.
Znode classification

A ZNode can be made persistent or temporary:

  1. PERSISTENT ZNodes: Such ZNodes will not be lost in the event of ZboKeeper cluster outages or Clien outages after they are created.
  2. EPHEMERAL: The ZNode is EPHEMERAL when the client is down or does not send messages to the ZooKeeper cluster within the specified timeout period.

Znode can also be sequential. Each sequential Znode is associated with a unique monotonically increasing integer. This monotonically increasing integer is the suffix of the ZNode name. If the above two types of ZNodes are sequential, the following two types of ZNodes are available: 3. Znode (PERSISTENT_SEQUENTIAL): In addition to the characteristics of a persistent Znode, znode names are sequential. Znode (EPHEMERAL_SEQUENTIAL): In addition to being persistent, znode names are sequential.

Zookeeper simple example

The directory of ZooKeeper is as follows:

The configuration file is in./conf/zoo.cfg

The two most important parameters in the configuration file, data and clientPort

The following is a brief configuration of path for using commands:

Export ZOOKEEPER_HOME="$HOME/application/ apache-Zookeeper-3.7.0-bin "export ZOOBINDIR="$ZOOKEEPER_HOME/bin" export PATH="$ZOOBINDIR:$PATH"Copy the code

After the startup, run the zkcli. sh command to view zNode information.

[zk: localhost:2181(CONNECTED) 0] ls -R /
/
/watch
/workers
/zookeeper
/zookeeper/config
/zookeeper/quota
Copy the code

Implement a simple distributed lock: