基础组件链接:https://blog.csdn.net/qq_39246466/article/details/116994336
引入:pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bfxy</groupId>
<artifactId>foundation-zookeeper-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>foundation-zookeeper-test</name>
<description>foundation-zookeeper-test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 自己的zookeeper组件加入进来 -->
<dependency>
<groupId>com.bfxy</groupId>
<artifactId>foundation-zookeeper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 顺序UUID -->
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
控制层使用
package com.bfxy.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bfxy.service.LisenterService;
import com.bfxy.zookeeper.api.ZookeeperClient;
@RestController
public class IndexController {
@Autowired
private ZookeeperClient client;
@RequestMapping("/index")
public String index() throws Exception {
return "zk-test";
}
@RequestMapping("/insertparent")
public String insertparent() throws Exception {
client.addPersistentNode("/super", "parent node");
return "insert success!";
}
@Autowired
private LisenterService lisenterService;
//第一件事情
@RequestMapping("/watcher")
public String watcher() throws Exception {
client.listener4ChildrenPath("/super", lisenterService);
return "wather success!";
}
//第二件事情 添加子节点
@RequestMapping("/insertc1")
public String insertc1() throws Exception {
client.addPersistentNode("/super/c1", "c1 data");
return "insert success!";
}
}
service实现监听watcher:可以实现子节点改变后动作
package com.bfxy.service;
import org.springframework.stereotype.Component;
import com.bfxy.zookeeper.api.ChangedEvent;
import com.bfxy.zookeeper.api.NodeListener;
import com.bfxy.zookeeper.api.ZookeeperClient;
@Component
public class LisenterService implements NodeListener {
@Override
public void nodeChanged(ZookeeperClient client, ChangedEvent event) throws Exception {
System.err.println("收到通知, 节点路径: " + event.getPath() + ", 节点内容: " + event.getData() + ", 变更操作: " + event.getType());
}
}
工具:
package com.bfxy.utils;
import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
public class KeyUtil {
public static String generatorUUID(){
TimeBasedGenerator timeBasedGenerator = Generators.timeBasedGenerator(EthernetAddress.fromInterface());
return timeBasedGenerator.generate().toString();
}
public static void main(String[] args) {
// insert into table ()
System.err.println(KeyUtil.generatorUUID());
// insert into table ()
System.err.println(KeyUtil.generatorUUID());
}
}