ZooKeeper is a top distributed open source project, widely used by well-known open source projects such as Dubbo and Kafka. I had only heard of it before, I had not studied it carefully. Today, I will take you to learn ZooKeeper, mainly from the installation of ZooKeeper, visual tools, application of three aspects to introduce, I hope to help you!

Mall SpringBoot practical electricity project (40 k + star) address: https://github.com/macrozheng/mall

Introduction to the

ZooKeeper is a distributed coordination framework that provides consistent services for distributed systems. ZooKeeper, originally developed by Yahoo and later donated to the Apache Foundation, is now a top Apache project and currently has 9.5K +Star on GitHub.

Distributed coordination

To understand ZooKeeper we need to first understand what is distributed coordination? Take the example of a registry in the Spring Cloud.

There are many services in a micro-service (distributed) system, and there are many instances of the same service. In the application, we can call the service through the service name to load balance, and these services may die, or new instances may join. The registry is the distributed coordinator, and ZooKeeper can be used as the coordinator. In this case, the registry can be used as the distributed coordinator.

The installation

ZooKeeper is easy to install on both Windows and Linux. Let’s learn how to install it first.

Windows installation

  • First download the ZooKeeper installation package, download address: https://www.apache.org/dyn/cl…

  • Unzip to the specified directory, and the directory structure is as follows;

  • inconfCreate configuration files in the directoryzoo.cfg, the contents are as follows;
# set the heartbeat time, Datadir =I:/developer/env/ apache-ZooKeeper -3.7.0-bin/data # Listening on port ClientPort =2181 for client connections
  • Enter thebinDirectory to start the ZooKeeper service;
  • When the service starts successfully, the console outputs the following message.

Linux installation

  • Using Docker to install ZooKeeper is undoubtedly the most convenient. First, we download its Docker image.
Docker pull zookeeper: 3.7.0
  • Create the ZooKeeper configuration file directory and switch to that directory to create the configuration filezoo.cfg;
mkdir /mydata/zookeeper/conf/ -p
cd /mydata/zookeeper/conf/
touch zoo.cfg
  • The configuration filezoo.cfgThe content is as follows, directly use Vim to edit;
# Set heartbeat time in milliseconds tickTime=2000 # Store in-memory database snapshot folder dataDir=/ TMP /zookeeper # Listen on client connection port clientPort=2181
  • Run the ZooKeeper container.
Docker run - p - 2181-2181 the name zookeeper \ v/mydata/zookeeper/conf/zoo. The CFG: / conf/zoo. The CFG \ - d zookeeper: 3.7.0

Command line operation

Next, let’s use the command line to operate ZooKeeper, familiar with the use of ZooKeeper.

  • The first to usezkCliCommand-line tools connect to ZooKeeper;
ZkCli. CMD - server
  • throughhelpYou can command to view the common commands of ZooKeeper;
[zk: 127.0.1:2181 (Connected) 0] Help ZooKeeper -Server Host: Port-Client-Configuration Properties -File CMD Args AddWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE addauth scheme auth close config [-c] [-w] [-s] connect host:port create [-s] [-e] [-c] [-t ttl] path [data] [acl] delete [-v version] path deleteall path [-b batch size] delquota [-n|-b|-N|-B] path get [-s] [-w] path getAcl [-s] path getAllChildrenNumber path getEphemerals path history listquota path ls [-s] [-w] [-R] path printwatches on|off quit reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] redo cmdno removewatches path [-c|-d|-a] [-l] set [-s] [-v version] path data setAcl [-s] [-v version] [-R] path acl setquota  -n|-b|-N|-B val path stat [-w] path sync path version whoami
  • Everybody knows that Redis is throughkey-valueThe form of stored data that ZooKeeper is throughznode-valueIn the form of storing data, ZNode is a bit like a directory while/The directory is the root directory of ZooKeeper. You can view all ZNodes with the following command.
[ZK: 127.0.1:2181 (CONNECTED) 1] ls / [ZooKeeper]
  • Create a ZNode called/zk_test, store the stringmy_data“, which works a bit like Redis;
[zk: 2] create /zk_test my_data
Created /zk_test
  • Looking at all the ZNodes, you can seezk_testThe znode.
[zk: 127.0.1:2181 (CONNECTED) 3] ls / [zk_test, zookeeper]
  • Get the data stored in ZNode;
[zk: 4] get /zk_test
  • Modify data in ZNode;
[zk: 5] set /zk_test test_data
[zk: 6] get /zk_test
  • Delete data from ZNode;
[zk: 7] delete /zk_test
[zk: 8] ls /

Visual management

PrettyZooZooKeeper is a graphical management client based on Apache Toth and JavaFX implementation. Appearance level is high, recommended.

  • downloadPrettyZooInstallation package, download address:https://github.com/vran-dev/P…

  • We need to create a connection to ZooKeeper that can be foundPrettyZooIs supported over SSH channel connection;

  • Double-click the connection, and we can view the data stored in ZooKeeper. It is clear that ZooKeeper stores data according to the directory structure.

  • Right click on the directory and you can create and delete ZNodes. With this tool, you can basically say goodbye to command line operations.

  • If you still think the command line is cool,PrettyZooAlso realized the command line function, open the command line label can be happy to knock the command.

The node type

Nodes in ZooKeeper (zNodes) have a lifetime, depending on the type of node. There are mainly four types as follows:

  • Persistent node: The default node type. After the node is created, it will always exist.
  • Persistent Sequential node: Has a Persistent node feature that adds an incrementing numeric suffix to the node name.
  • Ephemeral: Ephemeral exists temporarily and is deleted when the session that created the node is closed.
  • Ephemeral Sequential node: Has the Ephemeral node feature and adds an incrementing numeric suffix to the node name.

If you create nodes from the command line, the sequential properties correspond to the -s option and the temporary properties correspond to the -e option, such as the following command:

Create -s /test/seqTmp create -s /test/seqTmp create -s /test/seqTmp create -s /test/seqTmp create -s /test/seqTmp create -s /test/seqTmp create -s /test/seqTmp setTmpText

After successful creation, it will display as follows:

If you’re creating it with PrettyZoo, just check one of the options.

Use as a registry

CAP is an important theory in distributed architecture. It includes Consistency, Availability and Partition tolerance. Eureka, which we often use, supports AP, while ZooKeeper supports CP. Let’s look at how ZooKeeper can be used as a registry in the Spring Cloud.

  • ZooKeeper is used as a registry and is basically the same as Eureka and Consul. First we need to use thepom.xmlAdd a ZooKeeper service discovery component to the
  • Then modify the configuration fileapplication.yml, add ZooKeeper related configuration;
Spring: Cloud: ZooKeeper: # ZooKeeper Connect-string: localhost:2181 Discovery: # # Use IP address instead of hostname prefer-ip-address: true when registering
  • Again, using the example from the Spring Cloud tutorial, there are two serviceszookeeper-ribbon-serviceandzookeeper-user-service, the former calls the latter remotely through the Ribbon;

  • Start two services separately, we passPrettyZooYou can see that when ZooKeeper is used as a registry, the name, IP, and port of the registration service are stored in it.

  • We callzookeeper-ribbon-serviceThe interface under test, found that can normal visit, interface address: http://localhost:8301/user/1

  • If at this point we takezookeeper-user-serviceWhen the service is turned off, we can see that ZooKeeper will automatically delete the data stored;

  • It can be seen from this that ZooKeeper, as the registration center of micro-services, is realized through temporary nodes. When the service goes online, it will be registered with ZooKeeper, and when the service goes offline, it will be deleted by ZooKeeper, ensuring the high availability of micro-services.


Today we learned about installing ZooKeeper, using the visualization tool PrettyZoo, and using ZooKeeper as a registry in the Spring Cloud. In fact, there are many applications of ZooKeeper in distributed systems, such as doing distributed locking, implementing the main function, instead of UUID to generate unique ID, you are interested in the further study!

The resources

The official document: https://zookeeper.apache.org/…

Project source address


In this paper, making
https://github.com/macrozheng/mall-learningHas been included, welcome everyone STAR!