监听ZK节点数据修改

Posted 扰扰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听ZK节点数据修改相关的知识,希望对你有一定的参考价值。

一、引入依赖

<?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">
    <parent>
        <artifactId>Dubbo-Code</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>zk-client</artifactId>

    <dependencies>
        <!-- Dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.6</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

二、代码

package com.zk.demo;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Demo {
    static ZooKeeper zooKeeper;
    private static final String parentPath="/node";
    //充当redis
    private static Map<String, Object> map = new LinkedHashMap<String, Object>();

    static {
        try {
            zooKeeper = new ZooKeeper("192.168.20.93:2181", 2000, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        //获取节点数据,new Watcher监听节点数据变化
        if (zooKeeper==null){
            System.out.println("zk为空");
        }
        Watcher watcher=new Watcher() {
            public void process(WatchedEvent event) {
                //event.getPath()获取节点路径,event.getType()获取节点变化类型
                System.out.println(event.getPath()+"节点数据发生了变化:"+event.getType());
                getChlidren(parentPath,this);
            }
        };
        getChlidren(parentPath,watcher);

        Thread.sleep(Integer.MAX_VALUE);
        zooKeeper.close();
    }

    /**
     * 递归获取节点信息
     */
    private static void getChlidren(String path,Watcher watcher)  {
        try {
            if(path.equals(parentPath)) {
                byte[] data = zooKeeper.getData(path, watcher, null);
                String dataStr = new String(data);
                map.put(path, dataStr);
                System.out.println("[" + path + "]" + dataStr);
            }

            List<String> children = zooKeeper.getChildren(path, watcher);
            if(children.isEmpty() || children.size()==0){
                return ;
            }
            for (String child : children) {
                String key = path+"/"+child;
                byte[] data1 = zooKeeper.getData(key, watcher, null);
                String dataStr1=new String(data1);
                map.put(path, dataStr1);
                System.out.println("["+key+"]"+dataStr1);
                getChlidren(key,watcher);
            }
        } catch ( Exception e) {
            System.err.println("获取节点信息异常");
            System.out.println(e.getMessage());
        }
        System.out.println("reloadCache : "+map.toString() );
    }
}

 

以上是关于监听ZK节点数据修改的主要内容,如果未能解决你的问题,请参考以下文章

zookeeper集群的选举机制,监听原理,写数据流程 和 节点类型,

ZooKeeper ---- Zookeeper基本入门

ZooKeeper ---- Zookeeper基本入门

分布式应用,ZooKeeper做了什么?

zookeeper相关

利用ZK来实现一个服务的注册和发现