【 Middleware 】 Introduction to the basic use of Spring integration ZooKeeper
ZooKeeper is a distributed, open source, distributed application coordination service, widely used in distributed systems, such as useful it allocation center, registry, there are also use it to realize the distributed lock, as an indispensable high concurrency technology stack based components, then we will see, zk should be how to play, how to play
This article, as the first part, will mainly introduce the basic use posture based on ZK-Client, and then understand the basic concept of ZK
The I.
1. Zk installation
For the purpose of learning the pilot zK experience, the installation is relatively simple, you can refer to the blog 210310-ZooKeeper Installation and Initial Experience
Wget tar ZXVF - https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz Apache - they are - 3.6.2 - bin. Tar. GzcdApache - they are - 3.6.2 - bin# Foreground start
bin/zkServer.sh start-foreground
Copy the code
2. Project environment
This article demonstrates using zK directly with Apache’s ZooKeeper package, regardless of whether it is a SpringBoot environment
The core depends on
<! -- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
Version Description:
- Zk: 3.6.2
- SpringBoot: 2.2.1. RELEASE
Ii.zk uses posture
1. Zk basic knowledge
First of all, introduce several main knowledge points of ZK, such as data model of ZK and four commonly said nodes
1.1 Data Model
Zk’s data model is similar to our common directory tree, with each level being a node, starting with /
Each node contains data + child nodes
Note: the EPHEMERAL node cannot have child nodes.
Zk often said listener, is based on nodes, generally speaking to monitor the creation of nodes, deletion, data changes
1.2 the node
- Persistent nodes
- The nodes in the persistent sequence are persistent sequental
- Ephemeral node
- Ephemeral sequental is the temporary sequence node
Note:
- Once a node type is specified, it cannot be changed
- Temporary nodes, which are automatically deleted when the session ends and cannot have child nodes
2. Create a node
Let’s look at the use of ZK posture, the first is to create a node, of course, to create the premise is to get zkClient first
Initializing a connection
private ZooKeeper zooKeeper;
@PostConstruct
public void initZk(a) throws IOException {
// The session timeout period is 500s
zooKeeper = new ZooKeeper("127.0.0.1:2181".500 _000.this);
}
Copy the code
Node creation method, two different cases are given below
@Service
public class NodeExample implements Watcher {
/** * Create node **@param path
*/
private void nodeCreate(String path) {
// The third parameter ACL indicates access control permission
// The fourth parameter controls whether a persistent node is created, a persistent sequential node, or a temporary node; Temporary sequence node
// Return the actual path of the created node
/ / a single node to exist, throw exceptions KeeperException. NodeExists
try {
String node = zooKeeper.create(path + "/yes"."Saved Data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create node: " + node);
} catch (KeeperException.NodeExistsException e) {
// The node exists
System.out.println("Node already exists:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
// Nodes with life cycles
try {
Stat stat = new Stat();
// If there is no child on the node and there is no change for 1s, delete the node
// Error detected, unknown cause
String node = zooKeeper.create(path + "/ttl", ("now: " + LocalDateTime.now()).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 1000);
System.out.println("ttl nod:" + node + "|" + stat);
// Create a listener for validation
zooKeeper.exists(path + "/ttl", (e) -> {
System.out.println("TTL node change:" + e);
});
} catch (KeeperException.NodeExistsException e) {
System.out.println("Node already exists:" + e.getMessage());
} catch(Exception e) { e.printStackTrace(); }}}Copy the code
Create (path + “/yes”, “saved data “.getBytes(), zoodefs.ids.open_acl_broadening, createmode.persistent);
- If a node already exists, an exception will be thrown when you create it again
KeeperException.NodeExistsException
- The last parameter determines the type of node we create
- Todo: Failed to specify TTL in the above example. The cause is not found yet and needs to be resolved
3. Check whether the node exists
It is common to check whether a node exists (for example, we may check whether a node exists before creating it).
/** * Checks whether the node exists */
private void checkPathExist(String path) {
try {
// If the node exists, the stat object is returned; If it does not exist, return NULL
// watch: true adds a listener to the node to trigger the Watcher callback when the node is created/deleted or new data is added
Stat stat = zooKeeper.exists(path + "/no".false);
System.out.println("NoStat: " + stat);
} catch (Exception e) {
e.printStackTrace();
}
try {
// Check whether the node exists and listen for node creation + deletion + data change
// Note that this event listener will only trigger once, that is, only the first change of the node data can be retrieved, after the change, need to register the listener again
Stat stat = zooKeeper.exists(path + "/yes".this);
System.out.println("YesStat: " + stat);
} catch(Exception e) { e.printStackTrace(); }}Copy the code
Pay attention to
Zookeeper.exists (path + “/yes”, this);
- When the node exists, the Stat object is returned, containing some basic information; If it does not exist, null is returned
- The second argument, passed in, is the event callback object, our test class
NodeExmaple
Implements the interfaceWatcher
, so the direct transmission isthis
- When registering an event listener, note that the callback is executed only once, and then is cancelled. Any subsequent modification, deletion, or creation of the node will not be received
4. Obtain child nodes
Gets all the children of a node, which returns the first-level children of the current node
/** * get all child nodes of a node@param path
*/
private void nodeChildren(String path) {
try {
// If it succeeds, it listens for the deletion of the current node, the creation and deletion of child nodes, and triggers the callback event. This callback is also triggered once
List<String> children = zooKeeper.getChildren(path, this.new Stat());
System.out.println("path:" + path + " 's children:" + children);
} catch (KeeperException e) {
System.out.println(e.getMessage());
} catch(Exception e) { e.printStackTrace(); }}Copy the code
5. Data acquisition and modification
Data can be stored on a node. You can add data when creating a node. Later can be read, can also be modified
/** * set data, get data **@param path
*/
public void dataChange(String path) {
try {
Stat stat = new Stat();
byte[] data = zooKeeper.getData(path, false, stat);
System.out.println("path: " + path + " data: " + new String(data) + ":" + stat);
// Exact match according to version; Version = -1 does not require version matching
Stat newStat = zooKeeper.setData(path, ("new data" + LocalDateTime.now()).getBytes(), stat.getVersion());
System.out.println("newStat: " + stat.getVersion() + "/" + newStat.getVersion() + " data: " + new String(zooKeeper.getData(path, false, stat)));
} catch(Exception e) { e.printStackTrace(); }}Copy the code
When setting data, you can specify the version. If version > 0, the data is matched according to the version. If it is -1, it is ok as long as the nodes’ paths match
6. Event monitoring
The listener is mainly for nodes. The listener can be set when judging whether the node exists or modifying data, but they are one-time. If we want to be effective for a long time, we can use addWatch below
public void watchEvent(String path) {
try {
// Notice that this node exists
// Add a listener. Different from the listener added when exist determines whether the node exists, the listener is still valid and will be triggered after it is triggered. It can only be cancelled by manually calling remove
// Perception: node creation, deletion, data change; Create and delete child nodes
// Unable to perceive: child node creation/deletion of child node, data change of child node
zooKeeper.addWatch(path + "/yes".new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("Event trigger on" + path + " event:" + event);
}
}, AddWatchMode.PERSISTENT);
} catch (Exception e) {
e.printStackTrace();
}
try {
// Notice that this node does not exist
// Add listener, different from exist, after triggered, still valid and will be triggered, only a manual call to remove will cancel
// The difference is that the changes of its child nodes are also monitored
zooKeeper.addWatch(path + "/no".new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("Event trigger on" + path + " event:" + event);
}
}, AddWatchMode.PERSISTENT_RECURSIVE);
} catch (Exception e) {
e.printStackTrace();
}
// Remove all listeners
//zooKeeper.removeAllWatches(path, WatcherType.Any, true);
}
Copy the code
There are two cases given above,
- Addwatchmode. PERSISTENT: concerns only the deletion, data modification, and creation of current nodes and the creation and deletion of level-1 child nodes. The creation and deletion of child nodes and data changes of child nodes cannot be sensed
- Addwatchmode.persistent_recursive: Equivalent to recursive listening, all changes to the changed node and its children are listened for
7. The node is deleted
Finally, a basic function, node deletion, can be removed only if the child nodes do not exist (similar to Linux rmdir).
/** * Delete the node */
public void deleteNode(String path) {
try {
// Delete according to version restriction. -1 indicates that the version is not required, and the path matches. Otherwise, the version must match, or an exception will be thrown
zooKeeper.delete(path, -1);
} catch(Exception e) { e.printStackTrace(); }}Copy the code
Nodule 8.
This article mainly introduces the basic operation posture of ZooKeeper on the Java side, which can be regarded as the introduction of ZK, to understand the addition, deletion and modification of nodes, event monitoring;
Of course, it is generally recommended to use a Curator for ZK, which is smoother than an Apache jar, as will be explained later
II. The other
0. Project
- Project: github.com/liuyueyi/sp…
- Project source: github.com/liuyueyi/sp…
1. An ashy Blog
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
Below a gray personal blog, record all the study and work of the blog, welcome everyone to go to stroll
Wechat official account: One Grey Blog