ZooKeeper学习

举报
百里长疯 发表于 2020/12/27 22:59:59 2020/12/27
4.3k+ 0 0
【摘要】 ZooKeeper学习 java连接代码 package zooKeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.omg.CORBA.CODESET_INCOMPATIBLE; import org.apache.zookeeper.Zo...

ZooKeeper学习

java连接代码

package zooKeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.omg.CORBA.CODESET_INCOMPATIBLE;
import org.apache.zookeeper.ZooDefs.Ids;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
 * @description:
 * @author: mxd
 * @time: 2020/7/13 下午9:55
 */
public class ZookeeperDemo1 implements Watcher { private ZooKeeper zk = null; //超时时间 private static final int SESSION_TIME_OUT = 10000; private static final String CONNECTION_STRING="node2:2181,node3:2181,node4:2181"; private static final String ZK_PATH="/test"; private CountDownLatch countDownLatch = new CountDownLatch(1);  //栅栏闭锁 public static void main(String[] args) throws Exception { ZookeeperDemo1 zk = new ZookeeperDemo1(); zk.createConnection(CONNECTION_STRING,SESSION_TIME_OUT); //  zk.createPath(ZK_PATH,"mxd12345"); //   zk.getDataAndWatch();   //这个方法有加入线程延时sleep zk.isExist("/mxd"); } /** * 创建ZK连接 * * @param connectString * ZK服务器地址列表 * @param sessionTimeout * Session超时时间 */ public void createConnection(String connectString, int sessionTimeout) { this.releaseConnection(); try { zk = new ZooKeeper(connectString, sessionTimeout, this); /*countDownLatch.await(); } catch (InterruptedException e) { System.out.println("连接创建失败,发生 InterruptedException"); e.printStackTrace();*/ } catch (IOException e) { System.out.println("连接创建失败,发生 IOException"); e.printStackTrace(); } } /** * 关闭ZK连接 */ public void releaseConnection() { if (this.zk!=null) { try { this.zk.close(); } catch (InterruptedException e) { // ignore e.printStackTrace(); } } } /** * 创建节点 * * @param path * 节点path * @param data * 初始数据内容 * @return */ public boolean createPath(String path, String data) { try { System.out.println("节点创建成功, Path: " + this.zk.create(path, // data.getBytes(), // Ids.OPEN_ACL_UNSAFE, // 节点类型,是一个权限控制 CreateMode.PERSISTENT) + ", content: " + data); //持久或者暂时 } catch (KeeperException e) { System.out.println("节点创建失败,发生KeeperException"); e.printStackTrace(); } catch (InterruptedException e) { System.out.println("节点创建失败,发生 InterruptedException"); e.printStackTrace(); } return true; } //获取子结点,并监控节点的变化情况 public void getDataAndWatch() throws Exception{ List<String> children = this.zk.getChildren("/", true); for (String child : children) { System.out.println(child); } Thread.sleep(Long.MAX_VALUE);  //停止一段事件观察节点变化 } public void process(WatchedEvent watchedEvent) { //时时监听 List<String> children = null; try { children = this.zk.getChildren("/", true); for (String child : children) { System.out.println(child); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 判断节点是否存在 * * @param path * 节点path * @return */ public void isExist(String path) throws KeeperException, InterruptedException { Stat exists = this.zk.exists(path, false); System.out.println(exists==null? "not exist" : "good"); } /** * 删除指定节点 * * @param path * 节点path */ public void deleteNode(String path) { try { this.zk.delete(path, -1); System.out.println("删除节点成功,path:" + path); } catch (KeeperException e) { System.out.println("删除节点失败,发生KeeperException,path: " + path); e.printStackTrace(); } catch (InterruptedException e) { System.out.println("删除节点失败,发生 InterruptedException,path: " + path); e.printStackTrace(); } } }
  
 

文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_44613138/article/details/107333106

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。