监听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() );
    }
}

 

上一篇:ZooKeeper学习笔记三:使用ZooKeeper实现一个简单的配置中心


下一篇:二、Kafka与ZK安装