【Zookeeper】zk_客户端API_创建节点

客户端API操作

前提:保证服务器上zk集群服务端启动

配置maven依赖

复制代码
   <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
</dependencies>

拷贝log4j.properties文件到项目根目录

在项目的src/main/resources目录下,新建一个文件,命名为log4j.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] 

创建zookeeper客户端

复制代码
package com.baidu.zk;

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

public class zkClient {

    @Test
    public void init()throws Exception{

        ZooKeeper zooKeeper = new ZooKeeper("master:2181,slave1:2181,slave2:2181", 100, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
}

创建子节点

复制代码
package com.baidu.zk;
import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;
public class zkClient {
    private ZooKeeper zkClient;
    @Before
    public void init()throws Exception{

        zkClient = new ZooKeeper("master:2181,slave1:2181,slave2:2181", 30000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    @Test
    public void create() throws KeeperException, InterruptedException {

        String nodeCreated = zkClient.create("/sg", "zgl".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);


    }
}

获取子节点并监听节点变化

复制代码
   @Test
    public void getChildren() throws Exception{

        List<String> children = zkClient.getChildren("/", true);
        for (String child : children) {
            System.out.println(child);
        }
    }

让程序不结束:

复制代码
     @Test
    public void getChildren() throws Exception {
        List<String> children = zkClient.getChildren("/", true);
        for (String child : children) {
            System.out.println(child);
        }
        Thread.sleep(Long.MAX_VALUE);
    }

  private ZooKeeper zkClient;
    @Before
    public void init() throws Exception {
        zkClient = new ZooKeeper("master:2181,slave1:2181,slave2:2181", 5000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("---------------------------");
                try {
                    List<String> 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();
                }
            }
        });
        Thread.sleep(10000);
    }

判断Znode是否存在

复制代码
    @Test
    public void exist() throws Exception{
        Stat stat = zkClient.exists("/sanguo", false);

        System.out.println(stat == null ? "not exist" : "exist");
    }
相关推荐
BD_Marathon1 小时前
【Zookeeper】监听器原理
linux·分布式·zookeeper
无心水1 小时前
【分布式利器:分布式ID】5、UUID/GUID方案:无依赖实现,优缺点与场景选型
分布式·分库分表·uuid·分布式id·水平分库·分布式利器·guid
F***E2392 小时前
【分布式文件存储系统Minio】2024.12保姆级教程
分布式
拾忆,想起3 小时前
Dubbo服务超时与重试策略配置指南:构建 resilient 微服务架构
服务器·网络·微服务·云原生·架构·dubbo
杭州杭州杭州4 小时前
实验3 微服务介绍以及开发环境搭建
微服务·云原生·架构
i***71954 小时前
RabbitMQ 集群部署方案
分布式·rabbitmq·ruby
k***21604 小时前
RabbitMQ 客户端 连接、发送、接收处理消息
分布式·rabbitmq·ruby
p***c9499 小时前
后端在微服务中的服务网关
微服务·云原生·架构
2501_9418814012 小时前
Kubernetes 容器集群资源调度与弹性扩容高可用架构在互联网业务实战经验总结
云原生·容器·kubernetes