zookeeper(1)-basic configuration

zookeeper(1)-basic configuration

zookeeper role

1.master节点选举, After the master node hangs up, the slave node will take over the work and ensure that this node is unique. This is also the so-called head mode, so as to ensure that our cluster is highly available. 2.统一配置文件管理, That is, only one server needs to be deployed, and the same network distribution files can be synchronized to all other servers. This operation is particularly used in cloud computing. 3.发布与订阅Similar to the MQ (amq, rmq...) of the message queue, the dubbo publisher stores the data on the znode, and the subscriber will read the data. 4. 提供分布式锁Competing for resources between different processes in a distributed environment is similar to locks in multiple threads. 5 集群管理Ensure strong data consistency in the cluster.

zookeeper installation

Download link https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

wget http://mirrors.shu.edu.cn/apache/zookeeper/stable/zookeeper-3.4.12.tar.gz

Unzip and install

 tar -zxvf zookeeper-3.4.12.tar.gz -C/usr/local/

Configure global variables

vim .bash_profile

Increase ZK_HOME

export ZK_HOME=/usr/local/zookeeper-3.4.12
export PATH=$ZK_HOME/bin:$PATH

Effective immediately

source .bash_profile

mac os system can directly brew reinstall zookeeper

brew reinstall zookeeper
To have launchd start zookeeper now and restart at login:
  brew services start zookeeper
Or, if you don't want/need a background service you can just run:
  zkServer start
==> Summary
?/usr/local/Cellar/zookeeper/3.4.13: 244 files, 33.4MB

Directory Structure

$ tree -d -L 1
├── bin
├── include
├── lib
└── libexec

4 directories

Bin mainly runs the command conf to store configuration files

# The number of milliseconds of each tick
# The number of ticks that the initial 
# synchronization phase can take
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
# the directory where the snapshot is stored.
# do not use/tmp for storage,/tmp here is just 
# example sakes.
# the port at which the clients will connect
# the maximum number of client connections.
# increase this if you need to handle more clients
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
# The number of snapshots to retain in dataDir
# Purge task interval in hours
# Set to "0" to disable auto purge feature

tickTime: The basic event unit, in milliseconds. This time is used as the time interval for maintaining heartbeats between Zookeeper servers or clients. dataDir: The location of the database snapshot in memory. As the name implies, it is the directory where Zookeeper saves data. By default, Zookeeper Save the log file for writing data in this directory. clientPort: This port is the port for the client to connect to the Zookeeper server. Zookeeper will listen to this port and accept the client's access request. initLimit: This configuration item is used to configure Zookeeper to accept client initialization The longest heartbeat time interval that can be tolerated when connecting. When the time of more than 10 heartbeats is (ticktime), Zookeeper server has not received the client's return information, then it indicates that the client has failed to connect. The total length of time is :10 2000 = 20s syncLimit: This configuration item indicates the length of the request and response time for sending messages between Leader and Follower. The longest cannot exceed the length of tickTime. The total length of time is: 5 2000 = 10s server.A = B:C:DA: Which server is this number? B: The IP address of the server C: The port through which the server exchanges information with the Leader server in the cluster D: Once the Leader server in the cluster hangs up, a port is required for re-election. Select a new Leader 2181: provide external port 2888: internal synchronization port 3888: node is down, elect port

Modify the zookerper configuration file and modify the data storage directory. The default path is the tmp directory and needs to be modified

$cd $ZK_HOME/conf
$cp zoo_sample.cfg zoo.cfg
$vim zoo.cfg
# the directory where the snapshot is stored.
# do not use/tmp for storage,/tmp here is just
# example sakes.

Delete the files ending with cmd under the bin under zookeeper under Windows

-rwxr-xr-x 1 baxiang baxiang 232 Mar 27 12:32 README.txt
-rwxr-xr-x 1 baxiang baxiang 1937 Mar 27 12:32 zkCleanup.sh
-rwxr-xr-x 1 baxiang baxiang 1534 Mar 27 12:32 zkCli.sh
-rwxr-xr-x 1 baxiang baxiang 2696 Mar 27 12:32 zkEnv.sh
-rwxr-xr-x 1 baxiang baxiang 6773 Mar 27 12:32 zkServer.sh

Open zookeeper

$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config:/usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg

View zookeeper status

zkServer status
ZooKeeper JMX enabled by default
Using config:/usr/local/etc/zookeeper/zoo.cfg
Mode: standalone

Zookeeper basic data structure

Zookeeper is a tree structure, similar to Linux file directories. Each node is called a znode, and it can have child nodes or data. Each node is divided into a temporary node and a permanent node. The temporary node disappears after the client is disconnected. Each zk node has its own version number, and the node information can be displayed through the command line. Whenever the node data changes, the version number of the node will be accumulated (optimistic locking). Delete/modify outdated nodes. If the version number does not match, an error will be reported. The data stored in each zk node should not be too large, just a few K. Nodes can set permissions acl, and user access can be restricted through permissions

zookeeper client

Connecting to localhost:2181

Create node

create/zk_test test_data
[zk: localhost:2181(CONNECTED) 3] ls/
[zookeeper, zk_test]

Delete node

Reference: https://cloud.tencent.com/developer/article/1438226 zookeeper(1)-basic configuration-cloud + community-Tencent Cloud