Zookeeper 系列(四)ZKClient API
Zookeeper 系列(四)ZKClient API
环境准备:
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
一、ZKClient 基本用法
public class ZkClientBase {
private static final String CONNECT_ADDR = "127.0.0.1:2181";
private static final int SEESION_OUTTIME = 5 * 1000;
public static void main(String[] args) {
ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), 10 * 1000);
//1. create and delete
zkClient.createEphemeral("/temp");
zkClient.createPersistent("/super/c1", true);
zkClient.deleteRecursive("/super");
//2. 设置path和data,并且读取于节点和每个节点的内容
zkClient.createPersistent("/super", "1234");
zkClient.createPersistent("/super/c1", "c1");
zkClient.createPersistent("/super/c2", "c2");
List<String> list = zkClient.getChildren("/super");
for (String path : list) {
String rp = "/super/" + path;
System.out.println(rp + ":" + zkClient.readData(rp).toString());
}
//3. 更新和判断节点是否存在
zkClient.writeData("/super/c1", "新内容");
System.out.println("/super/c1:" + zkClient.readData("/super/c1").toString());
System.out.println("/super/c1:" + zkClient.exists("/super/c1"));
}
}
一、ZKClient Watcher
(一)节点变化
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
/**
* 监听节点变化
* @author: leigang
* @version: 2018-04-06
*/
public class zkClientWatcher2 {
private static final String CONNECT_ADDR = "127.0.0.1:2181";
private static final int SEESION_OUTTIME = 5 * 1000;
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), SEESION_OUTTIME);
zkClient.deleteRecursive("/super");
zkClient.createPersistent("/super");
// 对父节点添加子节点变化的事件
zkClient.subscribeDataChanges("/super", 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);
}
});
zkClient.writeData("/super", "super");
Thread.sleep(1000);
}
}
(一)子节点变化
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import java.util.List;
/**
* 监听子节点变化
* @author: leigang
* @version: 2018-04-06
*/
public class zkClientWatcher {
private static final String CONNECT_ADDR = "127.0.0.1:2181";
private static final int SEESION_OUTTIME = 5 * 1000;
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient = new ZkClient(new ZkConnection(CONNECT_ADDR), SEESION_OUTTIME);
zkClient.deleteRecursive("/super");
zkClient.subscribeChildChanges("/super", new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println("parentPath:" + parentPath);
System.out.println("currentChilds:" + currentChilds);
}
});
zkClient.createPersistent("/super");
Thread.sleep(1000);
zkClient.createPersistent("/super/c1", "c1");
Thread.sleep(1000);
zkClient.createPersistent("/super/c2", "c2");
Thread.sleep(1000);
// 修改不会监听
zkClient.writeData("/super/c2", "c2新内容");
Thread.sleep(1000);
zkClient.deleteRecursive("/super");
Thread.sleep(Integer.MAX_VALUE);
}
}
Zookeeper 系列(四)ZKClient API的更多相关文章
- Zookeeper系列四:Zookeeper实现分布式锁、Zookeeper实现配置中心
一.Zookeeper实现分布式锁 分布式锁主要用于在分布式环境中保证数据的一致性. 包括跨进程.跨机器.跨网络导致共享资源不一致的问题. 1. 分布式锁的实现思路 说明: 这种实现会有一个缺点,即当 ...
- Zookeeper系列2 原生API 以及核心特性watcher
原生API 增删改查询 public class ZkBaseTest { static final String CONNECT_ADDR = "192.168.0.120"; ...
- go微服务系列(四) - http api中引入protobuf
1. protobuf相关依赖安装 2. 改造之前的client 2.1 新建proto文件 2.2 运行protoc命令生成go文件 2.3 然后把原来的map修改成具体的类型就可以了 3. 处理j ...
- Natasha 4.0 探索之路系列(四) 模板 API
Natasha 模板 Natasha 在编译单元的基础上进行了封装整理, 并提供了多种模板帮助开发者构建功能. 使用此篇的 API 前提是您对 C# 非常熟悉, 对系统的一些类型足够了解. 据此 Na ...
- Zookeeper 系列(三)Zookeeper API
Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...
- Zookeeper 系列(五)Curator API
Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...
- 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用
zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...
- 转:arcgis api for js入门开发系列四地图查询
原文地址:arcgis api for js入门开发系列四地图查询 arcgis for js的地图查询方式,一般来说,总共有三种查询方式:FindTask.IdentifyTask.QueryTas ...
- curator框架的使用以及实现分布式锁等应用与zkclient操作zookeeper,简化复杂原生API
打开zookeeper集群 先体会一下原生API有多麻烦(可略过): //地址 static final String ADDR = "192.168.171.128:2181,192.16 ...
随机推荐
- Axure8.1.0.3372 注册码
Axure8.1.0.3372 注册码 转载:http://blog.csdn.net/cslucifer/article/details/79355007 Koshy wTADPqxn3KChzJx ...
- Postman的基础使用
postman的基础功能,官方文档介绍的是相当啰嗦,所以笔者这里先简单介绍一下主界面,入门功能就都提到了.稍后我们再一一介绍基础功能的使用方法. Collections:在Postman中,Colle ...
- 类继承-super, 私有变量
多继承 class A: def ces(self): print('a-ces') class B(A): def ces(self): print('b-ces') class C(A): def ...
- centos7.3安装zip,unzip
安装命令: yum install -y unzip zip
- 多数据源springboot-jta-atomikos
参考: https://github.com/classloader/springboot-jta-atomikos-demo 參考:二 :建议参考 https://blog.csdn.net/a ...
- Annoying “Remote System Explorer Operation” causing freeze for couple of seconds
Eclipse -> Preferences -> General -> Startup and Shutdown. -Uncheck RSE UI. Eclipse -> P ...
- ABAP-HTTP支持
ABAP 中对HTTP的支持 SAP Web Application Server -> Internet Communication Framework. http://help.sap.c ...
- 多线程-threading
多线程-threading python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用 1. 使用threading模块 ...
- 【Java】JVM(六)虚拟机字节码执行引擎
一.概述 执行引擎是虚拟机中最核心的部分之一, 虚拟机自己实现引擎,自己定义指令集和执行引擎的结构体系. 二.栈帧 栈帧包含(1)局部变量表.(2)操作数栈.(3)动态链接.(4)方法返回地址.(5) ...
- linux一些基本常识(三)
acl:对本身权限的扩展 打包:zip 111.zip a.txt b.txt..... zip -r /etc/sysconfig/* (样才能第归所有内容0) 解宝:uzip 1 ...