zookeeper 监听事件 PathChildrenCacheListener

zookeeper 监听事件 PathChildrenCacheListener

PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询。

1.测试类:

package com.qy.learn.zk.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 七脉
 * 描述:利用父节点,监听子节点的事件,每次都会触发
 */
public class PathChildrenCacheTest {
    
    private static final Logger log = LoggerFactory.getLogger(PathChildrenCacheTest.class);
    
    public static void main(String[] args) throws Exception {
        
        //获取curator客户端
        CuratorFramework client = MyCuratorClient.client();
        //开启客户端
        client.start();
        
        //cacheData if true, node contents are cached in addition to the stat
        PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/father",true);
        //StartMode.BUILD_INITIAL_CACHE同步初始化缓存数据
        pathChildrenCache.start(StartMode.BUILD_INITIAL_CACHE);
        
        //添加节点监听事件,PathChildrenCacheListener每次都会触发
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                log.info("接收到PathChildrenCacheListener监听事件,节点:{},事件类型:{}", event.getData().getPath(), event.getType());
            }
        });
        
        //测试事件
        MyCuratorClient.create(client, "/father/me", "me");
        MyCuratorClient.update(client, "/father/me", "me2");
        MyCuratorClient.query(client, "/father/me");//查询不会触发
        MyCuratorClient.delete(client, "/father/me");
        
        //睡眠等待监听事件触发
        Thread.sleep(15000);
        
        pathChildrenCache.close();
        client.close();
    }
}

 

2.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>
    
    <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-Hg

 

转载于:https://www.cnblogs.com/zwcry/p/10442586.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Zookeeper提供了一种事件监听机制,用于实时监控Zookeeper节点的状态变化。当节点发生变化时,Zookeeper会通知注册了相应监听器的客户端,以便客户端可以及时处理相关事件Zookeeper事件监听机制主要包括两个部分:Watcher和Listener。 1. Watcher:Watcher是Zookeeper提供的一种轻量级的事件通知机制。当客户端注册了Watcher,并且指定了需要监控的节点路径和事件类型后,Zookeeper会在节点的状态发生变化时,向客户端发送相应的事件通知。客户端可以通过Watcher回调方法来处理这些事件,例如节点的创建、删除、数据更新等。 2. Listener:Listener是Zookeeper客户端提供的一种高级抽象,用于对Zookeeper节点的变化进行监听。与Watcher不同,Listener可以一次性监听多个节点,并提供更灵活的事件处理方式。通过Listener,客户端可以设置节点的创建、删除、数据更新等多个事件类型,并通过回调方法来处理这些事件。 使用Zookeeper事件监听机制,可以实现实时监控分布式系统中各个节点的状态变化,并及时做出相应的操作。例如,在分布式锁场景中,可以通过监听节点的删除事件来实现锁的释放;在配置管理场景中,可以监听节点的数据更新事件来实时获取最新的配置信息。这种机制可以帮助开发者更好地处理分布式系统中的数据一致性和协调问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值