版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/84174753
eclipse环境搭建
拷贝zookeeper-3.4.10.jar、jline-0.9.94.jar、log4j-1.2.16.jar、netty-3.10.5.Final.jar、slf4j-api-1.6.1.jar、slf4j-log4j12-1.6.1.jar到工程的lib目录。并build一下,导入工程。
拷贝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
客户端代码
创建ZooKeeper客户端
package com.zyd.zook;
import java.io.IOException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;
public class TestZookeeper {
//连接zkServer
private String connectString = "testnote01,testnote02,testnote03";
//超时间设置
private int sessionTimeout = 2000;
ZooKeeper zkClient;
//初始化客户端
@Test
public void initClient() throws IOException {
zkClient =new ZooKeeper(connectString, sessionTimeout, new Watcher(){
@Override
public void process(WatchedEvent event) {
System.out.println(event.getType()+"--------"+event.getPath());
}
});
}
}
结果
2018-11-17 11:01:32,638 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=testnote01,testnote02,testnote03 sessionTimeout=2000 watcher=com.zyd.zook.TestZookeeper$1@64650ddb
创建子节点
/**
* 创建子节点
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void create() throws KeeperException, InterruptedException{
//路径 数据 权限(通过接口Ids分别所有)节点类型(临时的 临时带序列号的 永久的 永久带序列号的,具体看源码)
String path = zkClient.create("/zyd", "shuai".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(path);
}
修改创建客户端为@Test
结果
2018-11-17 11:15:41,588 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server testnote01/192.168.18.50:2181, sessionid = 0x1672083bdd80004, negotiated timeout = 4000
None--------null
/zyd
获取节点信息
监听模式为false
//获取子节点
@Test
public void getChild() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", false);
for (String child : children) {
System.out.println(child);
}
}
结果
2018-11-17 11:37:44,417 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server testnote03/192.168.18.52:2181, sessionid = 0x3671e81bdeb0000, negotiated timeout = 4000
None--------null
zookeeper
zyd
app1
开启监听模式
//获取子节点
@Test
public void getChild() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
Thread.sleep(Long.MAX_VALUE);
}
调用的是客户端的监听方法
@Override
public void process(WatchedEvent event) {
// System.out.println(event.getType()+"--------"+event.getPath());
System.out.println("------------start--------------");
List<String> children;
try {
children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
System.out.println("------------end--------------");
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
判断子节点是否存在
//判断节点是否存在
@Test
public void exist() throws KeeperException, InterruptedException{
Stat stat = zkClient.exists("/zyd", false);
System.out.println(stat == null ? "not exist":"exist");
}
}