[from getting started to giving up zookeeper] getting started with zookeeper

Time:2019-11-15

Preface

Zookeeper is a distributed service coordination framework, which can be used to maintain distributed configuration information, service registry, and implement distributed locks. It is widely used in HBase, Hadoop, Kafka and other projects. With the popularity of distributed and micro services, zookeeper has become an unavoidable part of our daily development work. This article will start from the installation and configuration to the most basic use, and introduce it.

Installation and deployment

download

Download the recommended stable version at http://mirror.bit.edu.cn/apache/zookeeper/stable/. You can download the compiled bin file directly.
The latest stable version is 3.5.5

decompression

Extract the downloaded file to the working directory, / var / workspace / zookeeper

tar -xzvf apache-zookeeper-3.5.5-bin.tar.gz -C /var/workspace/zookeeper

To configure

[from getting started to giving up zookeeper] getting started with zookeeper

At this time, if you start zookeeper directly, it will fail, indicating that the zoo.cfg file cannot be found.
We need to copy the sample configuration file of Zou sample.cfg provided in the conf directory as a copy of zoo.cfg, which is the default startup configuration file

cp ../conf/zoo_sample.cfg ../conf/zoo.cfg

start-up

[from getting started to giving up zookeeper] getting started with zookeeper

Start by default, use config / zoo.cfg configuration file to start in the background

[from getting started to giving up zookeeper] getting started with zookeeper

When the foreground starts, the startup log will be printed on the terminal. After the terminal is shut down, the service is also shut down.

Visit

[from getting started to giving up zookeeper] getting started with zookeeper

Use zccli.sh – server host: port to access zookeeper server.
When the – server parameter is not added, 127.0.0.1:2181 is used by default

Use

Zookeeper uses a resource file directory like way to manage nodes, each node can store data.
Zookeeper has four different types of nodes:

  • Persistent: persistent node, which will be permanently saved unless manually deleted
  • Persistent ﹣ sequential: persistent sequential node. It will be saved permanently unless it is deleted manually. By default, after the node name set by the user, the number string is sequentially increased by ten digits. Such as test “0000000001
  • Ephemeral: a temporary node. After the session, the temporary node will be deleted automatically.
  • Ephemeral: temporary sequential node. After the session, the temporary node will be deleted automatically. By default, after the node name set by the user, the number string is sequentially increased by ten digits. Such as test “0000000001

help

After using zkcli.sh to connect to the server, you can use help to show how common commands are used.

[from getting started to giving up zookeeper] getting started with zookeeper

ls

[from getting started to giving up zookeeper] getting started with zookeeper

List nodes under a path

create

[from getting started to giving up zookeeper] getting started with zookeeper

You can use create {path} to create a node.
Use create – s {path} to create an ordered node, followed by an incremental 10 digit suffix.

[from getting started to giving up zookeeper] getting started with zookeeper

[from getting started to giving up zookeeper] getting started with zookeeper

Use create – e to create a temporary node. When you disconnect, the temporary node will be deleted.

set/get

[from getting started to giving up zookeeper] getting started with zookeeper

Zookeeper’s node can store data, and use set method to store data in the node.
Use the get method to get the data from the node.

set -v {currentVersion} {path} {data}

The data of the node is incremented every time it is updated. If you want to update the node, please use the above command to modify it. If CurrentVersion is not the latest version, the update fails, similar to optimistic locking CAS.

delete

[from getting started to giving up zookeeper] getting started with zookeeper

You can delete a node through delete.

summary

Through the above learning, we have learned the most basic installation, deployment and use of zookeeper. Keep in mind:

  • Zookeeper’s nodes are similar to file system management
  • Zookeeper has four types of nodes. The temporary node will be deleted automatically after the session is disconnected, and the sequence node suffix number will be increased automatically
  • When setting data, you can specify the version number, which is updated only when it is consistent with the current version. This optimistic lock update method can avoid data overwriting during concurrency

Author: aloof_

Read the original text

This is the original content of yunqi community, which can not be reproduced without permission.