本地java【动态监听】zk集群节点变化
生活随笔
收集整理的這篇文章主要介紹了
本地java【动态监听】zk集群节点变化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【README】搭建zk cluster, refer 2?https://blog.csdn.net/PacosonSWJTU/article/details/111404364?
?
【1】 動態監聽代碼,需要寫在 建立zk 連接的watcher 實現類里,如下:
public class TestZK {/*** zk server 連接串 */private String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";/*** 超時時間*/private int sessionTimeout = 3000; /*** zk客戶端實例 */private ZooKeeper zkClient; /*** 0-獲取zk連接 * @throws IOException*/@Before public void init() throws IOException {/* 連接zk服務器 */zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {try {/*3-獲取子節點并監控節點變化*/System.out.println("-------watcher start---------");zkClient.getChildren("/", true).stream().forEach(System.out::println);System.out.println("-------watcher end ---------");} catch (KeeperException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}});}/*** 2-獲取子節點 * @throws InterruptedException * @throws KeeperException */@Testpublic void getNode() throws KeeperException, InterruptedException {System.out.println("-------getNode start---------");zkClient.getChildren("/", false).stream().forEach(System.out::println);System.out.println("-------getNode end---------");try {System.in.read(); // 阻塞,主線程不結束 } catch (IOException e) {e.printStackTrace();} }/*sichuanzookeeperstar*/}【2】 centos8 zk 客戶端新建和刪除節點
log:? zk client operation on centos 8?
[zk: localhost:2181(CONNECTED) 0] ls / [sichuan, zookeeper, guangdong] [zk: localhost:2181(CONNECTED) 1] rmr /guangdong [zk: localhost:2181(CONNECTED) 2] [zk: localhost:2181(CONNECTED) 2] ls / [sichuan, zookeeper] [zk: localhost:2181(CONNECTED) 3] create /shanghai "shanghai city" Created /shanghai [zk: localhost:2181(CONNECTED) 4] ls / [shanghai, sichuan, zookeeper] [zk: localhost:2181(CONNECTED) 5] create /beijing "beijinj city" Created /beijing [zk: localhost:2181(CONNECTED) 6] ls / [shanghai, sichuan, beijing, zookeeper] [zk: localhost:2181(CONNECTED) 7]java log?
-------getNode start--------- 2020-12-19 15:07:05,968 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server 192.168.163.201/192.168.163.201:2181. Will not attempt to authenticate using SASL (unknown error)2020-12-19 15:07:06,004 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established to 192.168.163.201/192.168.163.201:2181, initiating session2020-12-19 15:07:06,011 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server 192.168.163.201/192.168.163.201:2181, sessionid = 0x1767ab9b8f10001, negotiated timeout = 4000-------watcher start--------- sichuan zookeeper guangdong -------watcher end --------- sichuan zookeeper guangdong -------getNode end--------- -------watcher start--------- sichuan zookeeper -------watcher end --------- -------watcher start--------- shanghai sichuan zookeeper -------watcher end --------- -------watcher start--------- shanghai sichuan beijing zookeeper -------watcher end ---------bingo !?
總結
以上是生活随笔為你收集整理的本地java【动态监听】zk集群节点变化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ITX折腾记:ITX主机入坑指南和配置推
- 下一篇: 企业微信聊天记录能永久保存吗企业微信的聊