本篇內(nèi)容主要講解“java操作zookeeper實(shí)例代碼”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java操作zookeeper實(shí)例代碼”吧!
成都創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)云陽(yáng),10多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220
package com.rxzx.demo;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
public class ZKDemo {
//連接zookeeper的超時(shí)時(shí)長(zhǎng)(毫秒)
private static final int OUT_TIME=60000;
//zookeeper集群連接對(duì)象
private ZooKeeper zk;
//自定義監(jiān)聽(tīng)事件對(duì)象
private Watcher wc=new Watcher(){
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
if(event.getType()==Event.EventType.NodeDataChanged){
System.out.println(event.getPath()+" "+event.getType());
}else{
System.out.println(event.getPath()+" "+event.getType());
}
}
};
//獲取監(jiān)控對(duì)象
public Watcher getWatcher(){
return this.wc;
}
//獲取zookeeper集群連接對(duì)象
public ZooKeeper getZooKeeper(){
if(zk==null){
try {
zk=new ZooKeeper("192.168.2.100:2181,192.168.2.101:2181,192.168.2.102:2181",OUT_TIME,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return zk;
}
//關(guān)閉zookeeper連接對(duì)象
public void close(){
if(zk!=null){
try {
zk.close();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ZKDemo zd=new ZKDemo();
try {
//獲取zookeeper連接對(duì)象
ZooKeeper zk=zd.getZooKeeper();
//遍歷node節(jié)點(diǎn)的子節(jié)點(diǎn)
List
for(String s:nodelist){
System.out.println(s);
}
//創(chuàng)建node的子節(jié)點(diǎn)son
//zk.create("/node/son", "hello word".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//判斷數(shù)據(jù)節(jié)點(diǎn)是否存在,如果存在則返回該對(duì)象
Stat son=zk.exists("/node/son0000000011", zd.getWatcher());
if(son!=null){
System.out.println(son.getCzxid());
}else{
System.out.println("該節(jié)點(diǎn)不存在");
}
//對(duì)數(shù)據(jù)節(jié)點(diǎn)的數(shù)據(jù)內(nèi)容進(jìn)行修改
//zk.setData("/node/son0000000011", "wwwwww".getBytes(), -1);
//Stat stat=new Stat();
//獲取數(shù)據(jù)節(jié)點(diǎn)的數(shù)據(jù)內(nèi)容
//byte[] d=zk.getData("/node/son0000000011", zd.getWatcher(), null);
//System.out.println(new String(d));
//System.out.println(stat.getCzxid());
zk.delete("/node/son4", -1);
System.out.println("操作完成");
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
zd.close();
}
}
}
到此,相信大家對(duì)“java操作zookeeper實(shí)例代碼”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!