ZooKeeper API搭建及操作

搭建IDEA环境

添加zookeeper依赖,以下内容加入pom.xml

<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.5.7</version>
    </dependency>
</dependencies>

在项目下的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]- %m%n

在net.loftiest.zookeeper下创建zkClient类。


初始化

创建一个init函数用来连接到zookeeper,代码如下

@Test
//初始化,连接到zk
public void init() throws IOException {
    
     
    ZooKeeper ZKClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
    
    
        @Override
        public void process(WatchedEvent watchedEvent) {
    
    

        }
    });
}

创建节点

ctr+alt+f 将zkClient升级为全局变量。将init前的@Test改为@Before

创建create函数用以创建节点,代码如下

 @Test
    //创建节点
    public void create() throws InterruptedException, KeeperException {
    
    
        String nodeCreated = zkClient.create("/loftiest", "api_test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
}

ZooKeeper.create()方法(常用的其中一个):

  1. 创建节点的位置

  2. 字节类型的数据

  3. 节点的权限

    ZooDefs.Ids.OPEN_ACL_UNSAFE 打开者拥有所有权限

  4. 创建的文件类型

    ① CreateMode.PERSISTENT 节点类型:永久

    ② EPHEMERAL 临时

    ③ EPHEMERAL_SEQUENTIAL 带序号且临时

    ④ PERSISTENT_SEQUENTIAL 带序号且永久

在命令行客户端中使用ls和get查看loftiest新建节点。
img


监控(查看) 节点

创建getChildren函数用来监控节点。

@Test
//监控节点
public void getChildren() throws InterruptedException, KeeperException {
    
    
    List<String> children = zkClient.getChildren("/", true);
    //便利所有节点
    for (String child : children) {
    
    
        System.out.println(child);
        
    }
    Thread.sleep(Long.MAX_VALUE);
}

ZooKeeper.getChildren()常用的有两个方法:

​ 1.两种方法第一个变量为监控的节点路径

​ 2.①是否使用连接服务端使用的监控器,②创建一个新的监控器

Thread.sleep()线程睡眠,Long.MAX_VALUE一个超大常量。

在init()中加入child遍历。即

public void init() throws IOException {
    
    
    zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
    
    
        @Override
        public void process(WatchedEvent watchedEvent) {
    
    
            System.out.println("----------------------------");
            List<String> children = null;
            try {
    
    
                children = zkClient.getChildren("/", true);
            } catch (KeeperException e) {
    
    
                e.printStackTrace();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            //便利所有节点
            for (String child : children) {
    
    
                System.out.println(child);
            }
            System.out.println("------------------------");
        }
    });
}

​ 注:由于与服务端连接时会启用监控器,第一次监控节点会打印两遍。

在命令行客户端中创建和删除节点,观察到
img

判断节点是否存在

创建exists()函数用来检查节点是否存在

@Test
//判断节点是否存在
public void exists() throws InterruptedException, KeeperException {
    
    
    Stat stat = zkClient.exists("/loftiest", false);
    System.out.println(stat==null? "not exists" : "exists");
}

ZooKeeper.exists():1.检查节点路径 2.是否开启监控

由于关闭了监控,那么要注释掉init中输出节点的代码

创建和删除节点,测试api客户端中的方法是否可行。

猜你喜欢

转载自blog.csdn.net/rfdjds/article/details/121044385