进入节点 ./bin/zkCli.sh
创建一个节点 create /helloworld "123"
获取节点的信息 get /helloworld
修改节点信息 set /helloworld "456"

image.png
创建一个字节点
create /helloworld/sub_node "aaa"
创建一个顺序节点

image.png
删除一个节点然后再创建一个节点,不会再分配000
rmr /helloworld/sub_node/seqnode0000000000

image.png
验证一个监控功能
通过eclipse连接到你的宿主机,创建节点获取数据
public static void main(String[] args) {
// TODO Auto-generated method stub
String zkpath = "/testzk";//字符串
testZK test = new testZK();
test.CreateConnection("192.168.95.128:2181", 100000);
test.createPath(zkpath, "123");//创建
//
String ret = test.readData(zkpath);//读
System.out.println("get data :" + ret);//打印
}

image.png
更改数据
test.writeData(zkpath, "321");
String ret = test.readData(zkpath);
System.out.println("get data :" + ret);

image.png
Process是watcher继承过来的方法,一旦有响应就回调这个函数
public void process(WatchedEvent arg0) {
// TODO Auto-generated method stub
System.out.println("get event" + arg0.getState() + " # "+ arg0.getType() + "\n");
this.bk = false;
}

image.png

image.png
一旦你的节点的信息发生变化,就被获取到了,就该同步你的配置,这部分的代码需要你自己去写
在/test2上增加节点,然后删除节点
test.getChildren("/test2");

image.png

image.png
验证临时节点
create -e /test_e "123"

image.png
如果我这台机器故障了 ,节点就是消失了

image.png
网友评论