zkClient操作
基本增刪改查代碼如下
public class createSession {
public static void main(String[] args) {
ZkClient zkClient = new ZkClient("119.45.52.68:2181");
System.out.println("connect success");
}
}
public class CreateNode {
public static void main(String[] args) {
ZkClient zkClient = new ZkClient("119.45.52.68:2181");
System.out.println("connect success");
//true代表可以遞歸創建目錄
zkClient.createPersistent("/zkclient/persistent/children",true);
System.out.println("create node success");
}
}
public class DeleteNode {
public static void main(String[] args) {
ZkClient zkClient = new ZkClient("119.45.52.68:2181");
zkClient.deleteRecursive("/zkclient");
System.out.println("success delete node");
}
}
public class GetChildrenChanged {
public static void main(String[] args) throws Exception{
ZkClient zkClient = new ZkClient("119.45.52.68:2181");
zkClient.createPersistent("/zkClient");
Thread.sleep(1000);
List<String> children = zkClient.getChildren("/zkClient");
System.out.println(children);
zkClient.subscribeChildChanges("/zkClient", new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println("父路徑"+parentPath+"當前子路徑的"+currentChilds);
}
});
zkClient.createPersistent("/zkClient/child1");
Thread.sleep(1000);
zkClient.delete("/zkClient/child1");
Thread.sleep(Integer.MAX_VALUE);
}
}
public class GetNodeData {
public static void main(String[] args) throws Exception{
String path = "/zkclient-node";
ZkClient zkClient = new ZkClient("119.45.52.68:2181");
boolean exist = zkClient.exists(path);
if (!exist){
zkClient.createEphemeral(path,"123");
}
zkClient.subscribeDataChanges(path, new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println(dataPath+"節點內容被更新"+data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println(dataPath+"節點內容被刪除");
}
});
Object o = zkClient.readData(path);
System.out.println(o);
zkClient.writeData(path,"4567");
Thread.sleep(2000);
zkClient.delete(path);
Thread.sleep(2000);
}
}
curator操作api使用
public class CreateSession {
public static void main(String[] args) {
//curator第一種創建回話方式
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("119.45.52.68:2181",
5000,3000,retryPolicy);
curatorFramework.start();
System.out.println("create session success");
//第二種方式
CuratorFramework client =CuratorFrameworkFactory.builder()
.connectString("119.45.52.68:2181")
.sessionTimeoutMs(5000)
.sessionTimeoutMs(3000)
.retryPolicy(retryPolicy)
.namespace("base")
.build();
client.start();
System.out.println("createe session2 success ");
}
}
public class CreateNode {
public static void main(String[] args) throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("119.45.52.68:2181")
.sessionTimeoutMs(5000)
.sessionTimeoutMs(3000)
.retryPolicy(retryPolicy)
//.namespace("base")
.build();
client.start();
String path = "/curator/child1";
client.create().creatingParentContainersIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(path,"init".getBytes());
System.out.println("success create node");
}
}
public class DeleteNode {
public static void main(String[] args) throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("119.45.52.68:2181")
.sessionTimeoutMs(5000)
.sessionTimeoutMs(3000)
.retryPolicy(retryPolicy)
//.namespace("base")
.build();
client.start();
String path = "/curator";
client.delete().deletingChildrenIfNeeded().withVersion(-1).forPath(path);
System.out.println("delete node success ");
}
}
public class GetNodeData {
public static void main(String[] args) throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("119.45.52.68:2181")
.sessionTimeoutMs(5000)
.sessionTimeoutMs(3000)
.retryPolicy(retryPolicy)
//.namespace("base")
.build();
client.start();
String path = "/curator/child1";
client.create().creatingParentContainersIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(path,"init".getBytes());
System.out.println("success create node");
Stat stat = new Stat();
byte [] data = client.getData().storingStatIn(stat).forPath(path);
System.out.println("數據爲"+new String(data));
}
}
public class UpdateNodeData {
public static void main(String[] args) throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("119.45.52.68:2181")
.sessionTimeoutMs(5000)
.sessionTimeoutMs(3000)
.retryPolicy(retryPolicy)
//.namespace("base")
.build();
client.start();
String path = "/curator/child1";
// client.create().creatingParentContainersIfNeeded()
// .withMode(CreateMode.PERSISTENT)
// .forPath(path,"init".getBytes());
// System.out.println("success create node");
Stat stat = new Stat();
byte [] data = client.getData().storingStatIn(stat).forPath(path);
System.out.println("數據爲"+new String(data));
int version = client.setData().withVersion(stat.getVersion()).forPath(path).getVersion();
System.out.println("update node "+path+" version "+version);
client.setData().withVersion(stat.getVersion()).forPath(path).getAversion();
}
}
最後一行代碼會報錯,原因是不能設置同樣的數據版本。
除了上述的兩種zkclient和curatir的常用操作。還有一些其他操作都可以去嘗試
代碼的地址爲https://github.com/zhendiao/deme-code/tree/main/zk
歡迎搜索關注本人與朋友共同開發的微信面經小程序【大廠面試助手】和公衆號【微瞰技術】,以及總結的分類面試題https://github.com/zhendiao/JavaInterview