0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12719
1 重点关注
1.1 本节内容
java api客户端新增临时节点和永久节点
1.2 java api 新增节点同步调用
/** * 同步或者异步创建节点,都不支持子节点的递归创建,异步有一个callback函数 * 参数: * path:创建的路径 * data:存储的数据的byte[] * acl:控制权限策略 * Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa * CREATOR_ALL_ACL --> auth:user:password:cdrwa * createMode:节点类型, 是一个枚举 * PERSISTENT:持久节点 * PERSISTENT_SEQUENTIAL:持久顺序节点 * EPHEMERAL:临时节点 * EPHEMERAL_SEQUENTIAL:临时顺序节点 */ result = zookeeper.create(path, data, acls, CreateMode.EPHEMERAL); //使用部分 zkServer.createZKNode("/testnode", "testnode".getBytes(), Ids.OPEN_ACL_UNSAFE);
1.3 java api 新增节点异步调用(回调函数)
/** * 同步或者异步创建节点,都不支持子节点的递归创建,异步有一个callback函数 * 参数: * path:创建的路径 * data:存储的数据的byte[] * acl:控制权限策略 * Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa * CREATOR_ALL_ACL --> auth:user:password:cdrwa * createMode:节点类型, 是一个枚举 * PERSISTENT:持久节点 * PERSISTENT_SEQUENTIAL:持久顺序节点 * EPHEMERAL:临时节点 * EPHEMERAL_SEQUENTIAL:临时顺序节点 */ result = zookeeper.create(path, data, acls, CreateMode.EPHEMERAL); String ctx = "{'create':'success'}"; zookeeper.create(path, data, acls, CreateMode.PERSISTENT, new CreateCallBack(), ctx); package com.imooc.zk.demo; import org.apache.zookeeper.AsyncCallback.StringCallback; public class CreateCallBack implements StringCallback { @Override public void processResult(int rc, String path, Object ctx, String name) { System.out.println("创建节点: " + path); System.out.println((String)ctx); } }
2 课程内容
3 Coding
3.1 java api 新增节点同步调用Demo
- 启动服务端
进入到
cd /usr/local/zookeeper/bin
重启zookeeper服务端
./zkServer.sh restart
- 运行java类,创建临时节点
package com.imooc.zk.demo;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
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.ACL;
/**
*
* @Title: ZKConnectDemo.java
* @Description: zookeeper 操作demo演示
*/
public class ZKNodeOperator implements Watcher {
private ZooKeeper zookeeper = null;
public static final String zkServerPath = "172.26.139.4:2181";
public static final Integer timeout = 5000;
public ZKNodeOperator() {}
public ZKNodeOperator(String connectString) {
try {
zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeOperator());
} catch (IOException e) {
e.printStackTrace();
if (zookeeper != null) {
try {
zookeeper.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
/**
*
* @Title: ZKOperatorDemo.java
* @Description: 创建zk节点
*/
public void createZKNode(String path, byte[] data, List<ACL> acls) {
String result = "";
try {
/**
* 同步或者异步创建节点,都不支持子节点的递归创建,异步有一个callback函数
* 参数:
* path:创建的路径
* data:存储的数据的byte[]
* acl:控制权限策略
* Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa
* CREATOR_ALL_ACL --> auth:user:password:cdrwa
* createMode:节点类型, 是一个枚举
* PERSISTENT:持久节点
* PERSISTENT_SEQUENTIAL:持久顺序节点
* EPHEMERAL:临时节点
* EPHEMERAL_SEQUENTIAL:临时顺序节点
*/
result = zookeeper.create(path, data, acls, CreateMode.EPHEMERAL);
// String ctx = "{'create':'success'}";
// zookeeper.create(path, data, acls, CreateMode.PERSISTENT, new CreateCallBack(), ctx);
System.out.println("创建节点:\t" + result + "\t成功...");
//new Thread().sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
ZKNodeOperator zkServer = new ZKNodeOperator(zkServerPath);
// 创建zk节点
zkServer.createZKNode("/testnode", "testnode".getBytes(), Ids.OPEN_ACL_UNSAFE);
/**
* 参数:
* path:节点路径
* data:数据
* version:数据状态
*/
// Stat status = zkServer.getZookeeper().setData("/testnode", "xyz".getBytes(), 2);
// System.out.println(status.getVersion());
/**
* 参数:
* path:节点路径
* version:数据状态
*/
// zkServer.createZKNode("/test-delete-node", "123".getBytes(), Ids.OPEN_ACL_UNSAFE);
// zkServer.getZookeeper().delete("/test-delete-node", 2);
// String ctx = "{'delete':'success'}";
// zkServer.getZookeeper().delete("/test-delete-node", 0, new DeleteCallBack(), ctx);
// Thread.sleep(2000);
}
public ZooKeeper getZookeeper() {
return zookeeper;
}
public void setZookeeper(ZooKeeper zookeeper) {
this.zookeeper = zookeeper;
}
@Override
public void process(WatchedEvent event) {
}
}
- 打印日志:
创建节点: /testnode 成功...
- linux 连接zk客户端,验证临时节点已创建
--连接服务端
zkCli.sh -server localhost:2181
--查看节点是否创建
ls /
--output:
[imooc2, names, zookeeper, imooc3, imooc, testnode, imooc5]
- 过了心跳检测时间之后,再次用linux客户端,验证临时节点已不存在(java api客户端执行完代码,java api客户端session就关了,详见代码 )
--查看节点是否创建
ls /
--output:
[imooc2, names, zookeeper, imooc3, imooc, imooc5]
3.2 java api 新增永久节点回调 Demo
- 启动服务端
进入到
cd /usr/local/zookeeper/bin
重启zookeeper服务端
./zkServer.sh restart
- 运行java类,创建永久节点
package com.imooc.zk.demo; import java.io.IOException; import java.util.List; import org.apache.zookeeper.CreateMode; 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.ACL; /** * * @Title: ZKConnectDemo.java * @Description: zookeeper 操作demo演示 */ public class ZKNodeOperator implements Watcher { private ZooKeeper zookeeper = null; public static final String zkServerPath = "172.26.139.4:2181"; public static final Integer timeout = 5000; public ZKNodeOperator() {} public ZKNodeOperator(String connectString) { try { zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeOperator()); } catch (IOException e) { e.printStackTrace(); if (zookeeper != null) { try { zookeeper.close(); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } /** * * @Title: ZKOperatorDemo.java * @Description: 创建zk节点 */ public void createZKNode(String path, byte[] data, List<ACL> acls) { String result = ""; try { /** * 同步或者异步创建节点,都不支持子节点的递归创建,异步有一个callback函数 * 参数: * path:创建的路径 * data:存储的数据的byte[] * acl:控制权限策略 * Ids.OPEN_ACL_UNSAFE --> world:anyone:cdrwa * CREATOR_ALL_ACL --> auth:user:password:cdrwa * createMode:节点类型, 是一个枚举 * PERSISTENT:持久节点 * PERSISTENT_SEQUENTIAL:持久顺序节点 * EPHEMERAL:临时节点 * EPHEMERAL_SEQUENTIAL:临时顺序节点 */ // result = zookeeper.create(path, data, acls, CreateMode.EPHEMERAL); String ctx = "{'create':'success'}"; zookeeper.create(path, data, acls, CreateMode.PERSISTENT, new CreateCallBack(), ctx); System.out.println("创建节点:\t" + result + "\t成功..."); new Thread().sleep(2000); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { ZKNodeOperator zkServer = new ZKNodeOperator(zkServerPath); // 创建zk节点 zkServer.createZKNode("/testnode", "testnode".getBytes(), Ids.OPEN_ACL_UNSAFE); /** * 参数: * path:节点路径 * data:数据 * version:数据状态 */ // Stat status = zkServer.getZookeeper().setData("/testnode", "xyz".getBytes(), 2); // System.out.println(status.getVersion()); /** * 参数: * path:节点路径 * version:数据状态 */ // zkServer.createZKNode("/test-delete-node", "123".getBytes(), Ids.OPEN_ACL_UNSAFE); // zkServer.getZookeeper().delete("/test-delete-node", 2); // String ctx = "{'delete':'success'}"; // zkServer.getZookeeper().delete("/test-delete-node", 0, new DeleteCallBack(), ctx); // Thread.sleep(2000); } public ZooKeeper getZookeeper() { return zookeeper; } public void setZookeeper(ZooKeeper zookeeper) { this.zookeeper = zookeeper; } @Override public void process(WatchedEvent event) { } }
- 回调类:
package com.imooc.zk.demo; import org.apache.zookeeper.AsyncCallback.StringCallback; public class CreateCallBack implements StringCallback { @Override public void processResult(int rc, String path, Object ctx, String name) { System.out.println("创建节点: " + path); System.out.println((String)ctx); } }
- 打印日志:
创建节点: 成功... 创建节点: /testnode {'create':'success'}
- linux 连接zk客户端,验证永久节点已创建
--连接服务端
zkCli.sh -server localhost:2181
--查看节点是否创建
ls /
--output:
[imooc2, names, zookeeper, imooc3, imooc, testnode, imooc5]
诸葛