0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12720
1 重点关注
1.1 本节内容
java api客户端修改节点,只做了同步修改,异步修改方式如1.3,可以参考6-3异步新增
1.2 java api 修改节点同步修改
/** * 参数: * path:节点路径 * data:数据 * version:数据状态 从服务端获取到的版本号dataversion */ Stat status = zkServer.getZookeeper().setData("/testnode", "xyz".getBytes(),0); System.out.println(status.getVersion());
1.3 java api 修改节点异步调用(回调函数)
/** * The asynchronous version of setData. * * @see #setData(String, byte[], int) */ public void setData(final String path, byte data[], int version, StatCallback cb, Object ctx) { }
2 课程内容
3 Coding
3.1 java api 新增节点同步调用Demo
- 启动服务端
进入到 cd /usr/local/zookeeper/bin 重启zookeeper服务端 ./zkServer.sh restart
启动客户端,查看version
--启动linux zk客户端 zkCli.sh -server localhost:2181 --查看版本 get /testnode output: testnode cZxid = 0x81 ctime = Sat Mar 30 17:40:22 CST 2024 mZxid = 0x81 mtime = Sat Mar 30 17:40:22 CST 2024 pZxid = 0x81 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 0
- 运行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; import org.apache.zookeeper.data.Stat; /** * * @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:数据状态 从服务端获取到的版本号dataversion */ Stat status = zkServer.getZookeeper().setData("/testnode", "xyz".getBytes(),0); 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) { } }
- 打印日志:
1
- linux 连接zk客户端,验证临时节点已创建
--连接服务端 zkCli.sh -server localhost:2181 --查看节点版本及数据是否修改 get /testnode --output:
xyz
cZxid = 0x81
ctime = Sat Mar 30 17:40:22 CST 2024
mZxid = 0x86
mtime = Sun Mar 31 06:54:31 CST 2024
pZxid = 0x81
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
标签:Dubbo,java,String,ZK,zookeeper,中间件,节点,import,public From: https://www.cnblogs.com/1446358788-qq/p/18106346