- ZooKeeper JavaAPI 操作
- 3.1 pom.xml引入
- 3.2 创建连接【命令:./zkCli.sh –server ip:port】
- 3.3 创建节点【命令:create /节点path】
- 3.4 创建节点加内容【命令:create /节点path value】
- 3.5创建节点类型 【命令:create -e /节点path】
- 3.6 创建多级节点 【命令:create /父节点path create /子节点path】
- 3.7 查询节点 【命令:get /节点path】
- 3.8 查询子节点【命令:ls】
- 3.9 查询节点状态信息 【命令:ls -s】
- 3.10 修改节点内容【命令:set /节点path value】
- 3.11 锁定节点后,进行修改节点【命令:stat /节点path [watch] set /节点path value [version]】
- 3.12 删除节点【命令:delete /节点path】
- 3.13 删除带有子节点的节点【命令:deleteall /节点path】
- 3.14 必须成功删除节点
- 3.15 删除成功后的回调方法
- 4 Zookeeper JavaAPI操作-Watch监听概述
ZooKeeper JavaAPI 操作
3.1 pom.xml引入
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><!--curator--><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>4.0.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.0.0</version></dependency>
3.2 创建连接【命令:./zkCli.sh –server ip:port】
@Testpublic void testConnection() {// 第一种方式CuratorFramework newClient = CuratorFrameworkFactory.newClient("192.168.135.20:2181",60 * 1000, 15 * 1000,new ExponentialBackoffRetry(3000, 10));// 第二种方式CuratorFramework client = CuratorFrameworkFactory.builder().connectString("192.168.135.20:2181") // 服务端.sessionTimeoutMs(60 * 1000) // session失效时间.connectionTimeoutMs(15 * 1000) // 连接最大时间.retryPolicy(new ExponentialBackoffRetry(3000, 10)) // 重试策略.namespace("hikktn") // 客户端名称.build();// 开启连接client.start();}
3.3 创建节点【命令:create /节点path】
@Testpublic void testCreate(){try {// 如果没有指定数据,默认将客户端ip作为节点内容String path = client.create().forPath("/app1");System.out.println(path);} catch (Exception e) {e.printStackTrace();}}
3.4 创建节点加内容【命令:create /节点path value】
@Testpublic void testCreate2(){try {// 创建节点和内容String path = client.create().forPath("/app1","hikktn2".getBytes());System.out.println(path);} catch (Exception e) {e.printStackTrace();}}
3.5创建节点类型 【命令:create -e /节点path】
(持久化节点,临时节点,顺序节点)
@Testpublic void testCreate3(){try {// 设置节点类型 -- CreateMode 枚举类,选择什么关键字就是什么类型节点,持久化节点,临时节点String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app1");System.out.println(path);} catch (Exception e) {e.printStackTrace();}}
3.6 创建多级节点 【命令:create /父节点path create /子节点path】
可以不再像命令执行一样,先创建父节点,再来创建子节点
@Testpublic void testCreate4(){try {// 创建多级节点// 如果父节点不存在,则创建父节点String path = client.create().creatingParentsIfNeeded().forPath("/app1/h1");System.out.println(path);} catch (Exception e) {e.printStackTrace();}}
3.7 查询节点 【命令:get /节点path】
@Testpublic void testGet1(){try {// 查询节点 getbyte[] bytes = client.getData().forPath("/app1");System.out.println(new String(bytes));} catch (Exception e) {e.printStackTrace();}}
3.8 查询子节点【命令:ls】
@Testpublic void testGet2(){try {// 查询子节点 lsList<String> list = client.getChildren().forPath("/app1");System.out.println(list);} catch (Exception e) {e.printStackTrace();}}
3.9 查询节点状态信息 【命令:ls -s】
@Testpublic void testGet3(){try {// 查询节点状态信息 ls -sStat stat =new Stat();byte[] bytes = client.getData().storingStatIn(stat).forPath("/app1");System.out.println(new String(bytes));} catch (Exception e) {e.printStackTrace();}}
3.10 修改节点内容【命令:set /节点path value】
@Testpublic void testSet1(){try {Stat stat = client.setData().forPath("/app1", "hehe".getBytes());System.out.println(stat);} catch (Exception e) {e.printStackTrace();}}
3.11 锁定节点后,进行修改节点【命令:stat /节点path [watch] set /节点path value [version]】
@Testpublic void testSet2(){try {Stat status = new Stat();//3. 查询节点状态信息:ls -sclient.getData().storingStatIn(status).forPath("/app1");int version = status.getVersion();//查询出来的 3System.out.println(version);client.setData().withVersion(version).forPath("/app1", "hehe".getBytes());} catch (Exception e) {e.printStackTrace();}}
3.12 删除节点【命令:delete /节点path】
@Testpublic void testDelete() throws Exception {// 1. 删除单个节点client.delete().forPath("/app1");}
3.13 删除带有子节点的节点【命令:deleteall /节点path】
@Testpublic void testDelete2() throws Exception {//2. 删除带有子节点的节点client.delete().deletingChildrenIfNeeded().forPath("/app4");}
3.14 必须成功删除节点
@Testpublic void testDelete3() throws Exception {//3. 必须成功的删除client.delete().guaranteed().forPath("/app2");}
3.15 删除成功后的回调方法
@Testpublic void testDelete4() throws Exception {//4. 回调client.delete().guaranteed().inBackground(new BackgroundCallback() {@Overridepublic void processResult(CuratorFramework client, CuratorEvent event) throws Exception {System.out.println("我被删除了~");System.out.println(event);}}).forPath("/app1");}
4 Zookeeper JavaAPI操作-Watch监听概述
•NodeCache : 只是监听某一个特定的节点
•PathChildrenCache : 监控一个ZNode的子节点.
•TreeCache : 可以监控整个树上的所有节点,类似于PathChildrenCache和NodeCache的组合
4.1 监听一个节点注册,当父节点新增父节点的情况,附带一些操作
/*** 演示 NodeCache:给指定一个节点注册监听器*/@Testpublic void testNodeCache() throws Exception {//1. 创建NodeCache对象final NodeCache nodeCache = new NodeCache(client, "/app1");//2. 注册监听nodeCache.getListenable().addListener(new NodeCacheListener() {@Overridepublic void nodeChanged() throws Exception {System.out.println("节点变化了~");//获取修改节点后的数据byte[] data = nodeCache.getCurrentData().getData();System.out.println(new String(data));}});//3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据nodeCache.start(true);while (true) {}}
4.2 监听一个节点注册,监听父节点下的所有子节点变化
@Testpublic void testPathChildrenCache() throws Exception {//1.创建监听对象PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/app2", true);//2. 绑定监听器pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {@Overridepublic void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {System.out.println("子节点变化了~");System.out.println(event);//监听子节点的数据变更,并且拿到变更后的数据//1.获取类型PathChildrenCacheEvent.Type type = event.getType();//2.判断类型是否是updateif (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {System.out.println("数据变了!!!");byte[] data = event.getData().getData();System.out.println(new String(data));}}});//3. 开启pathChildrenCache.start();while (true) {}}
4.3 监听一个节点注册,包括父节点和父节点下所有子节点变化
/*** 演示 TreeCache:监听某个节点自己和所有子节点们*/@Testpublic void testTreeCache() throws Exception {//1. 创建监听器TreeCache treeCache = new TreeCache(client, "/app2");//2. 注册监听treeCache.getListenable().addListener(new TreeCacheListener() {@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {System.out.println("节点变化了");System.out.println(event);}});//3. 开启treeCache.start();while (true) {}}
