客户端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");
}