Zookeeper client use and classic application scenarios

Table of contents

1. Create a configuration center

 2. Use the zookeeper client to solve the problem that can only be monitored once

2.1 Client writing

 2.2 Configuration class writing

 3. The curator client connects to zookeeper

4. Build a cluster environment

 4.1 The zookeeper client connects to the cluster environment

 4.2 Cluster dynamic configuration


1. Create a configuration center

 Monitor files through other clients, the first window

第二次进入服务器:
cd /usr/local/zookeeper/apache-zookeeper-3.8.0-bin
bin/zkServer.sh start conf/zoo.cfg
bin/zkCli.sh

# 第一个窗口创建节点并修改节点内容
[zk: localhost:2181(CONNECTED) 1] create /config {"key":"anykey","url":"http://redis.com"}
Created /config
[zk: localhost:2181(CONNECTED) 2] get -w /config
{"key":"anykey","url":"http://redis.com"}
[zk: localhost:2181(CONNECTED) 3] set /config {"key":"anykey","url":"redis://redis.com"}

The second window monitors, first enter get -w /config to monitor the node, and then the second window will receive a notification after the first window is modified, but this monitor can only take effect once

[zk: localhost:2181(CONNECTED) 0] get -w /config
{"key":"anykey","url":"redis://redis.com"}
[zk: localhost:2181(CONNECTED) 1] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/config

 2. Use the zookeeper client to solve the problem that can only be monitored once

2.1 Client writing

Function display: the zookeeper client is established and can dynamically sense data changes

 

 Introduce the jar package, the version is consistent with the server

 

 

 Client code writing, after the main thread is executed, it will run the asynchronous thread executed by zookeeper

 

 

 2.2 Configuration class writing

Create configuration class and

 

 

Implement a loop monitoring function.

 

 

 

Change the data based on the version number. If the version number is incorrect, the data cannot be modified.

Delete and fetch data based on version number

 3. The curator client connects to zookeeper

pom.xml introduces curator

connect client 

Create child nodes recursively

 

 The protection mode uses withProtection to generate a UUID to prevent the creation of a duplicate node.

Set up nodes. 

 delete node. 

child node 

Specify thread pool 

 

4. Build a cluster environment

There are three roles in the cluster: the leader is responsible for reading and writing, and the follower is responsible for reading data. When the leader fails, one of them is elected to become the leader. The observer has the same data as the leader, but is only responsible for reading data.

 Create folders zk1, zk2, zk3, zk4, and generate myid unique ID

 

Modify zoo1.cfg

 

Modify the zoo1.cfg file to add the following configuration 

 Modify the second configuration file, and the third configuration file is as follows

 

 Start the server in turn 

 

 View server status in turn 

connect to the server 

 4.1 The zookeeper client connects to the cluster environment

cluster configuration

 Try to reconnect to the cluster environment

 View server connection port 

Close the server to view the reconnection mechanism 

test cluster

 4.2 Cluster dynamic configuration

 

 

Guess you like

Origin blog.csdn.net/qq_21575929/article/details/125772804