Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

ZooKeeper installation and basic functions

1. Install

Download address: zookeeper.apache.org/releases.ht…

Select bin download to avoid local compilation, such as the latest version 3.6.2

  • Mirrors.bfsu.edu.cn/apache/zook…

Once the download is complete, unzip and place it in the target location

Tar XVF - apache - they are - 3.6.2 - bin. Tar. GzCopy the code

Ensure a JDK8 or higher environment before starting

Start the command

The foreground starts with the default configuration
bin/zkServer.sh start-foreground
Copy the code

2. The early experience

Use the bin/ zkcli. sh connection to view information

# Resource list
ls path List the resources under the path

Create node
# -e: temporary, no persistent node
# -s denotes the sequential node
# path path
# data Carries data information
create [-e] [-s] path data 


View node data
get path

# set data
set path data [version]

# delete node
delete path [version]
Copy the code

Examples demonstrate

[zk: localhost:2181(CONNECTED) 28] create/zk: localhost:2181(CONNECTED) 28] create/zk: localhost:2181(CONNECTED) 28] create/zk: Localhost :2181(CONNECTED) 29] get /hello localhost:2181(CONNECTED) 30] create /hello/01 '00' Created /hello/01 [zk: localhost:2181(CONNECTED) 31] create /hello/02 '02' Created /hello/02 [zk: localhost:2181(CONNECTED) 32] ls /hello [01, 02] [zk: localhost:2181(CONNECTED) 33] get /hello/01 00 [zk: localhost:2181(CONNECTED) 34] delete /hello Node not empty: /hello [zk: localhost:2181(CONNECTED) 35] delete /hello/01 [zk: localhost:2181(CONNECTED) 36] ls /hello [02] [zk: Localhost :2181(CONNECTED) 39] create-e-s /hello/02 'order' Created /hello/020000000002 [zk: Localhost :2181(CONNECTED) 40] get /hello/02 02 [zk: 2181(CONNECTED) 41] get /hello/020000000002 Localhost :2181(CONNECTED) 42] create-e-s /hello/03 'sequential' Created /hello/030000000003 [zk: localhost:2181(CONNECTED) 43] ls /hello [02, 020000000002, 030000000003]Copy the code

3. Basic concepts

Four types of nodes:

  • Persistent nodePERSISTENT: A node exists after it is created unless it is manually deleted
  • Persist sequential nodesPERSISTENT_SEQUENTIALIn ZK, each parent node maintains the time sequence for the first level of child nodes and records the order of the creation of child nodes. The order here can be understood as adding an ordered numeric suffix to the node name as the new node name
  • Temporary nodeEPHEMERALThe difference between a temporary node and a persistent node is that the lifetime of the temporary node is bound to the client. When the client terminates, the node is destroyed
  • Temporary ordered nodeEPHEMERAL_SEQUENTIAL: temporary node, ordered, ZK distributed lock is generally used to achieve it

II. The other

1. A gray Blog:liuyueyi.github.io/hexblog

A gray personal blog, recording all the study and work in the blog, welcome everyone to go to stroll

2. Statement

As far as the letter is not as good, the above content is purely one’s opinion, due to the limited personal ability, it is inevitable that there are omissions and mistakes, if you find bugs or have better suggestions, welcome criticism and correction, don’t hesitate to appreciate

  • Micro Blog address: Small Gray Blog
  • QQ: a gray /3302797840