本文整理汇总了Java中org.I0Itec.zkclient.ZkClient.readData方法的典型用法代码示例。如果您正苦于以下问题:Java ZkClient.readData方法的具体用法?Java ZkClient.readData怎么用?Java ZkClient.readData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.I0Itec.zkclient.ZkClient
的用法示例。
在下文中一共展示了ZkClient.readData方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nextId
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
/**
* 利用zookeeper
* @return
*/
@Override
public String nextId() {
String app = this.registerDto.getApp();
String host = this.registerDto.getHost();
ZkClient zkClient = this.registerDto.getZkClient();
String path = Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host;
if (zkClient.exists(path)) {
// 如果已经有该节点,表示已经为当前的host上部署的该app分配的编号(应对某个服务重启之后编号不变的问题),直接获取该id,而无需生成
return zkClient.readData(Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host);
} else {
// 节点不存在,那么需要生成id,利用zk节点的版本号每写一次就自增的机制来实现
Stat stat = zkClient.writeDataReturnStat(Constants.ZK_REGISTRY_SEQ, new byte[0], -1);
// 生成id
String id = String.valueOf(stat.getVersion());
// 将数据写入节点
zkClient.createPersistent(path, true);
zkClient.writeData(path, id);
return id;
}
}
示例2: client
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
private static void client() {
try {
ZkClient zc = new ZkClient("localhost:2181");
String userTwo = "gao:gao";
zc.addAuthInfo("digest", userTwo.getBytes());
//使用admin就可以创建
// String userOne = "admin:admin";
// zc.addAuthInfo("digest",userOne.getBytes());
String data = zc.readData("/safe");
log.info("只读用户 " + userTwo + "读取safe节点返回数据:" + data);
zc.create("/safe2", "test", CreateMode.EPHEMERAL);
log.info("只读用户创建节点safe2成功:" + zc.readData("/safe2"));
zc.create("/safe/test", "zookeeper加密是针对节点加密的", CreateMode.EPHEMERAL);
log.info("只读用户创建节点失败:" + zc.readData("/safe/test"));
} catch (Exception e) {
e.printStackTrace();
}
}
示例3: start
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
public synchronized void start(String name) {
List<String> list = addressMap.get(name);
if (null != list) {
return;
}
list = new ArrayList<>();
// 创建 ZooKeeper 客户端
ZkClient zkClient = new ZkClient(zkAddress, Constant.ZK_SESSION_TIMEOUT, Constant.ZK_CONNECTION_TIMEOUT);
LOGGER.info("connect zookeeper={}", zkAddress);
// 获取 service 节点
String servicePath = Constant.ZK_REGISTRY_PATH + "/" + name;
if (!zkClient.exists(servicePath)) {
throw new RpcFrameworkException(RpcErrorMsgConstant.FRAMEWORK_REGISTER_ERROR,
String.format("can not find any service node on path: %s", servicePath));
}
List<String> addressList = zkClient.getChildren(servicePath);
if (CollectionUtil.isEmpty(addressList)) {
throw new RpcFrameworkException(RpcErrorMsgConstant.FRAMEWORK_REGISTER_ERROR,
String.format("can not find any address node on path: %s", servicePath));
}
for (String path : addressList) {
String addressPath = servicePath + "/" + path;
String ipPort = zkClient.readData(addressPath);
list.add(ipPort);
}
addressMap.put(name, list);
}
示例4: load
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Override
public void load() {
ZkClient client = client();
try {
byte[] bytes = client.readData(path, true);
if (bytes == null) return;
JSONObject json = (JSONObject) new JSONParser().parse(new String(bytes, Charset.forName("utf-8")));
Nodes.fromJson(json);
} catch (ParseException e) {
throw new IOError(e);
} finally {
client.close();
}
}
示例5: test1
import org.I0Itec.zkclient.ZkClient; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000, 5000);
Object readData = zkClient.readData("/asdfdfs/asdfsf", true);
System.out.println("read - " + readData);
Thread.sleep(10000L);
zkClient.close();
}