package com.zk.demo1;
import java.io.File;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.Before;
import org.junit.Test;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
public class Demo1 {
private ZooKeeper zk = null;
/**
* 创建zk的客户端连接
* @throws Exception
*/
@Before
public void init() throws Exception {
zk = new ZooKeeper("129.204.6.53:2181", 2000, new Watcher() {
/**
* 监听事件发生时的回调方法
*/
@Override
public void process(WatchedEvent event) {
System.out.println(event.getType());
System.out.println(event.getPath());
System.out.println(event.getState());
System.out.println("开始监听......");
}
});
}
/**
* 创建一个节点
* @throws Exception
*/
@Test
public void testCreateZnode() throws Exception{
zk.create("/address/nanming", "南明区".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
zk.create("/address/nanming", "云岩区".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
zk.create("/address/nanming", "白云区".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
zk.create("/address/nanming", "花溪区".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
byte[] address = zk.getData("/address", true, null);
System.out.println(address.toString()+"111111111");
}
/**
* 删除节点 删除只能为空的数据
*/
@Test
public void testDeleteZnode() throws Exception{
zk.delete("/a", -1);
Stat exists = zk.exists("/a", false);
System.out.println("查看节点数据:" + exists);
}
/**
* 更改节点
*/
@Test
public void testUpdateZnode() throws Exception{
zk.setData("/name", "赵云".getBytes("utf-8"), -1);
byte[] data = zk.getData("/name", false, null);
System.out.println(new String(data,"UTF-8"));
System.out.println(data.toString().getBytes("UTF-8"));
}
/**
* 查看子节点的数据
*/
@Test
public void testGetChildren() throws Exception{
List<String> list = zk.getChildren("/address", false);
for (String string : list) {
System.out.println(string);
}
}
/**
* 监听机制
* 1、事先定义好监听的回调函数
* 2、在对znode进行各种数据操作时可以注册监听
* 3、监听znode上发生相应事件时,客户端zk会接收到zookeeper集群的事件通知
* 4、客户端zk根据事件调用我们事先定义好的回调函数
*/
@Test
public void testWatch() throws Exception{
//监听是一次性的
zk.getData("/address", new Watcher() {
@Override
public void process(WatchedEvent event) {
if(event.getType() != EventType.None) {
System.out.println("事件监听的节点:" + event.getPath());
System.out.println("事件监听的类型:" + event.getType());
}
}
}, null);
Thread.sleep(Long.MAX_VALUE);
}
/**
* 上传一个配置文件到zookeeper
* @throws Exception
*/
@Test
public void testUploadFile() throws Exception{
String springMVC = FileUtils.readFileToString(new File("D:\\springMVC.xml"));
zk.create("/spring", "spring的配置文件".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/spring/springMVC.xml", springMVC.getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
byte[] b = zk.getData("/spring/springMVC.xml", false, null);
System.out.println(new String(b,"utf-8"));
}
}