• zookeeper 监听事件 NodeCacheListener


    zookeeper 监听事件 NodeCacheListener

    NodeCacheListener一次注册,每次监听,但是监听不到操作类型,不知道是增加?删除?还是修改?

    1.测试类:

    package com.qy.learn.zk.curator;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.recipes.cache.NodeCache;
    import org.apache.curator.framework.recipes.cache.NodeCacheListener;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * @author 七脉
     * 描述:NodeCacheListener监听触发事件,每次都会触发
     */
    public class NodeCacheTest {
        
        private final static Logger log = LoggerFactory.getLogger(NodeCacheTest.class);
        
        public static void main(String[] args) throws Exception {
            //获取curator客户端
            CuratorFramework client = MyCuratorClient.client();
            //开启客户端
            client.start();
            //创建缓存节点
            NodeCache nodeCache = new NodeCache(client, "/father/me");
            
            //将该节点数据初始化到本地缓存
            nodeCache.start(true);
            //添加节点监听事件,NodeCacheListener每次都会触发,但不能获取监听的操作类型到底是添加还是删除等。
            nodeCache.getListenable().addListener(new NodeCacheListener() {
                @Override
                public void nodeChanged() throws Exception {
                    String value = null;
                    if(null!=nodeCache.getCurrentData()){
                        value = new String(nodeCache.getCurrentData().getData());
                    }
                    log.info("接收到NodeCacheListener事件,节点:{},值:{}", nodeCache.getPath(), value);
                }
            });
            
            //测试事件
            MyCuratorClient.create(client, "/father/me", "me");
            MyCuratorClient.update(client, "/father/me", "me");
            MyCuratorClient.query(client, "/father/me");//查询不会触发
            MyCuratorClient.delete(client, "/father/me");
            
            //睡眠等待监听事件触发
            Thread.sleep(15000);
            
            //关闭
            nodeCache.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

  • 相关阅读:
    yum clean all大坑解决
    RHEL 7 “There are no enabled repos” 的解决方法
    exportfs命令 – 管理NFS服务器共享的文件系统
    Linux放大缩小字体的快捷键
    chcon命令详解
    通过配置hosts.allow和hosts.deny文件允许或禁止ssh或telnet操作
    安装RHEL7配置本地yum源 -- yum不能安装时,在本地安装,亲测成功
    块存储、文件存储、对象存储意义及差异
    在Windows Server 2012 R2域环境中禁用(取消)密码复杂策略
    bat脚本静默安装软件示例
  • 原文地址:https://www.cnblogs.com/zwcry/p/10442536.html
Copyright © 2020-2023  润新知