Zookeeper--Java客戶端
1 建立會話
public class ZkService {
private static final StringBuilder HOSTSTR = new StringBuilder();
private static final Integer SESSION_TIMEOUT = 6000;
private static ZooKeeper zooKeeper = null;
static {
HOSTSTR.append("192.168.209.136:2181,");
HOSTSTR.append("192.168.209.137:2181,");
HOSTSTR.append("192.168.209.138:2181");
try {
zooKeeper = new ZooKeeper(HOSTSTR.toString(), SESSION_TIMEOUT, null);
} catch (IOException e) {
// TODO Auto-generated catch block
if (zooKeeper != null) {
try {
zooKeeper.close();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
e.printStackTrace();
System.err.print("連線Zookeeper失敗");
}
}
public static ZooKeeper getZkService() {
if (zooKeeper == null) {
try {
zooKeeper = new ZooKeeper(HOSTSTR.toString(), SESSION_TIMEOUT,
null);
} catch (IOException e) {
if (zooKeeper != null) {
try {
zooKeeper.close();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
e.printStackTrace();
System.err.print("連線Zookeeper失敗");
}
}
return zooKeeper;
}
}
2 建立節點
建立節點分為同步和非同步,同步建立的時候需要捕獲異常,而非同步不需要,但是需要例項化一個實現StringCallBack的類,一般情況下非同步往往效能較高些。
同步建立節點:
/**
* 同步建立節點
*/
private static void createNodeBySynchronous() {
ZooKeeper zkService = ZkService.getZkService();
String actualPath = "";
if (zkService != null) {
try {
actualPath = zkService.create("/test01", "test01".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("建立的節點路徑為" + actualPath);
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
非同步建立節點測試的時候,建立的執行緒需要睡眠一段時間,等待回撥執行。原因未知。
/**
* 非同步建立節點
*/
private static void createNodeAsynchronous() {
ZooKeeper zkService = ZkService.getZkService();
if (zkService != null) {
String context = "上下文物件測試";
zkService.create("/test02", "/test02".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT,
new MyCallBack(), context);
//等待回撥方法執行
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
測試結果:
建立的節點需要登入驗證
/**
* 同步建立節點
* @throws NoSuchAlgorithmException
*/
private static void createNodeBySynchronous(){
ZooKeeper zkService = ZkService.getZkService();
String actualPath = "";
if (zkService != null) {
try {
//建立的節點需要登入驗證
List<ACL> acls=new ArrayList<ACL>();
String auth = DigestAuthenticationProvider.generateDigest("tang:123123");
Id digest=new Id("digest",auth);
acls.add(new ACL(Perms.ALL, digest));
actualPath = zkService.create("/test01", "test01".getBytes(),
acls, CreateMode.PERSISTENT);
System.out.println("建立的節點路徑為" + actualPath);
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
側試
引數說明:
3 刪除節點
/**
* 非同步刪除節點
*/
public static void testDeleteNode01() {
ZooKeeper zooKeeper = ZkService.getZkService();
try {
zooKeeper.delete("/test01", 0);
System.out.println("刪除成功");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
引數說明:
4 檢視子節點路徑
/**
* 同步檢視子節點資訊
*/
public static void testSelectNode01(){
ZooKeeper zkService = ZkService.getZkService();
if(zkService!=null){
try {
List<String> lstChildren = zkService.getChildren("/", false);
for (String str : lstChildren) {
System.out.println(str);
}
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
各欄位說明:
5 檢視節點資訊
public static void getNodeData01() {
ZooKeeper zkService = ZkService.getZkService();
if (zkService != null) {
try {
Stat stat = new Stat();
byte[] data = zkService.getData("/zookeeper", false, stat);
System.out.println("節點資料=" + new String(data));
System.out.println("節點資料長度 dataLength=" + stat.getDataLength());
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6 修改資料
public static void setNodeData01() {
ZooKeeper zkService = ZkService.getZkService();
if (zkService != null) {
try {
byte[] data = "zookeeper02".getBytes();
Stat stat = zkService.setData("/zookeeper", data, 1);
System.out.println("修改後節點資料版本:" + stat.getVersion());
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相關文章
- dubbo客戶端客戶端
- Pulsar客戶端客戶端
- mqtt 客戶端MQQT客戶端
- 客戶端,服務端客戶端服務端
- 服務端,客戶端服務端客戶端
- Nacos - 客戶端心跳續約及客戶端總結客戶端
- 物理DataGuard客戶端無縫切換--客戶端TAF 配置客戶端
- [Redis 客戶端整合] Java 中常用Redis客戶端比較Redis客戶端Java
- 客戶端加解密客戶端解密
- Zookeeper 客戶端 API客戶端API
- java websocket 客戶端JavaWeb客戶端
- 客戶端筆記客戶端筆記
- Redis-客戶端Redis客戶端
- redis客戶端管理Redis客戶端
- CXF--客戶端客戶端
- 中斷客戶端客戶端
- github客戶端使用Github客戶端
- ftp客戶端,ftp客戶端軟體具體怎麼使用?FTP客戶端
- 《samba搭建win客戶端和linux客戶端的區別》Samba客戶端Linux
- IE客戶客戶端程式開發的利器Bindows客戶端
- VNC客戶端推薦,Windows系統下VNC客戶端推薦VNC客戶端Windows
- VNC客戶端是Windows,VNC客戶端是Windows如何進行操作VNC客戶端Windows
- bilibili mac客戶端 嗶哩嗶哩 b站mac客戶端Mac客戶端
- Winform客戶端引用WCF客戶端後,部分類無法正常使用ORM客戶端
- FTP 客戶端使用教程FTP客戶端
- 微軟ToDo客戶端(macOS)微軟客戶端Mac
- Oracle 客戶端安裝Oracle客戶端
- MySQL 客戶端安裝MySql客戶端
- 開發JAXR客戶端客戶端
- Mac版YouTube客戶端Mac客戶端
- Zookeeper Java 客戶端搭建Java客戶端
- Transmit 5 for MacFTP客戶端MITMacFTP客戶端
- NEO 客戶端外掛客戶端
- ZooKeeper客戶端CRUD命令客戶端
- websocket(多個客戶端)Web客戶端
- Nacos - 客戶端註冊客戶端
- YouTube macYouTube客戶端Mac客戶端
- Tower:GIt客戶端Git客戶端