Zookeeper-day03-API代码 *** 作

Zookeeper-day03-API代码 *** 作,第1张

Zookeeper-day03-API代码 *** 作

1:添加pom文件内容


    
        junit
        junit
        RELEASE
    
    
        org.apache.logging.log4j
        log4j-core
        2.8.2
    
    
        org.apache.zookeeper
        zookeeper
        3.5.7
    

 2:resources目录下og4j.properties

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

 3:创建zookeeper客户端并创建节点

package com.atguigu.zk;

import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;

public class zkClient {

    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zkClient;

    @Before
    public void init() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    @Test
    public void create() throws KeeperException, InterruptedException {
        String  nodeCreated= zkClient.create("/atguigu", "ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

}

4:获取子节点并监听变化内容

package com.atguigu.zk;

import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;

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

public class zkClient {

    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zkClient;

    @Before
    public void init() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

                System.out.println("--------------------");
                List children = null;
                try {
                    children = zkClient.getChildren("/", true);

                    for (String child : children) {
                        System.out.println(child);
                    }
                    System.out.println("-------------------");
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        //注册一次,生效一次

        //持续监听,监听到输出
        Thread.sleep(Long.MAX_VALUE);
    }

}

5:判断znode是否存在

@Test
public void exist() throws KeeperException, InterruptedException {
    Stat stat = zkClient.exists("/atguigu", false);

    System.out.println(stat==null? "not exist" : "exist");
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4966522.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存