ZooKeeper学习
【摘要】 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(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44613138/article/details/107333106
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)