進入節(jié)點 ./bin/zkCli.sh
創(chuàng)建一個節(jié)點 create /helloworld "123"
獲取節(jié)點的信息 get /helloworld
修改節(jié)點信息 set /helloworld "456"
image.png
創(chuàng)建一個字節(jié)點
create /helloworld/sub_node "aaa"
創(chuàng)建一個順序節(jié)點
image.png
刪除一個節(jié)點然后再創(chuàng)建一個節(jié)點赋续,不會再分配000
rmr /helloworld/sub_node/seqnode0000000000
image.png
驗證一個監(jiān)控功能
通過eclipse連接到你的宿主機舆吮,創(chuàng)建節(jié)點獲取數(shù)據(jù)
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");//創(chuàng)建
//
String ret = test.readData(zkpath);//讀
System.out.println("get data :" + ret);//打印
}
image.png
更改數(shù)據(jù)
test.writeData(zkpath, "321");
String ret = test.readData(zkpath);
System.out.println("get data :" + ret);
image.png
Process是watcher繼承過來的方法妻顶,一旦有響應就回調這個函數(shù)
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
一旦你的節(jié)點的信息發(fā)生變化氓皱,就被獲取到了,就該同步你的配置续室,這部分的代碼需要你自己去寫
在/test2上增加節(jié)點栋烤,然后刪除節(jié)點
test.getChildren("/test2");
image.png
image.png
驗證臨時節(jié)點
create -e /test_e "123"
image.png
如果我這臺機器故障了 ,節(jié)點就是消失了
image.png