CuratorWatcher一次註冊只監聽一次,不監聽查詢。java
1.監聽測試類spring
package com.qy.learn.zk.curator; import org.apache.curator.framework.CuratorFramework; /** * @author 七脈 * 描述:CuratorWatcher事件監聽,註冊一次監聽一次 */ public class MyCuratorWatcherTest { public static void main(String[] args) throws Exception { CuratorFramework client = MyCuratorClient.client(); client.start(); //建立節點 MyCuratorClient.create(client, "/father/me", "me"); //添加節點監聽事件(節點需存在,查詢不觸發,且只觸發一次) client.getData().usingWatcher(new MyCuratorWatcher()).forPath("/father/me"); //測試事件 MyCuratorClient.update(client, "/father/me", "me2"); MyCuratorClient.query(client, "/father/me");//查詢不會觸發 MyCuratorClient.delete(client, "/father/me"); //線程睡眠,等待監聽事件響應 Thread.sleep(5000); client.close(); } }
2.監聽類apache
package com.qy.learn.zk.curator; import org.apache.curator.framework.api.CuratorWatcher; import org.apache.zookeeper.WatchedEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author 七脈 * 描述:CuratorWatcher觀察者監聽事件 */ public class MyCuratorWatcher implements CuratorWatcher{ private static final Logger log = LoggerFactory.getLogger(MyCuratorWatcher.class); @Override public void process(WatchedEvent event) throws Exception { // TODO Auto-generated method stub log.info("接收到CuratorWatcher事件,事件類型:{},節點名稱:{}", event.getType(), event.getPath()); } }
3.pom文件api
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <groupId>com.qy.learn</groupId> <artifactId>qy-learn-zk-curator</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> <maven.test.skip>true</maven.test.skip> <java.version>1.8</java.version> <spring.boot.version>2.0.1.RELEASE</spring.boot.version> <qy.code.version>0.0.1-SNAPSHOT</qy.code.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <!-- 不使用springboot默認log --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.12</version> <!-- 排除衝突jar --> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.1.0</version> </dependency> </dependencies> <repositories> <repository> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus-aliyun</id> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <!-- 要將源碼放上去,須要加入這個插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
源碼:https://pan.baidu.com/s/1WxW0NU6p7oAiK9O3Vtq-Hgspringboot