java本地监听zk服务器节点【动态上下线】
【README】
java本地訪問 zk cluster, refer 2?https://blog.csdn.net/PacosonSWJTU/article/details/111404364?
【1】 客戶端監(jiān)聽 zk節(jié)點(diǎn)變化?
1) zk客戶端代碼——http訪問的客戶端代碼(請(qǐng)求zk獲取 http服務(wù)器的上下線信息)
(對(duì)于zk來說,http的server或client都是客戶端,這里要能夠理解,本文只是將htpt作為例子以便理解,當(dāng)然也可以是其他請(qǐng)求協(xié)議)?
/*** 分布式客戶端 */ public class DistributeClient {public static void main(String[] args) throws Exception {DistributeClient client = new DistributeClient();/*1-獲取zk連接 */client.getZkConn(); /*2-注冊(cè)監(jiān)聽節(jié)點(diǎn)*/ client.registerListener();/*3-業(yè)務(wù)邏輯加工*/client.doBusi();}/*** zk客戶端*/private ZooKeeper zkClient;/*** 獲取子節(jié)點(diǎn) * @throws KeeperException* @throws InterruptedException */private void registerListener() throws KeeperException, InterruptedException {/* 監(jiān)聽servers路徑 */List<String> children = zkClient.getChildren("/servers", true);/* 存儲(chǔ)服務(wù)器節(jié)點(diǎn)主機(jī)名稱集合 */ArrayList<String> hosts = new ArrayList<>(); for(String child : children) {byte[] data = zkClient.getData("/servers/" + child, false, null);hosts.add(new String(data)); } /* 將所有主機(jī)名稱打印到控制臺(tái) */System.out.println(hosts); }private void doBusi() throws InterruptedException {Thread.sleep(Long.MAX_VALUE); // 進(jìn)程阻塞 }/*** zk server 連接串 */private final static String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";/*** 超時(shí)時(shí)間*/private final static int sessionTimeout = 3000; /*** 0-獲取zk連接 */public ZooKeeper getZkConn() throws IOException {/* 連接zk服務(wù)器 */zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {try {registerListener();} catch (KeeperException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}});return zkClient; } }2) centos8的zk節(jié)點(diǎn)操作
[zk: localhost:2181(CONNECTED) 1] create -e -s /servers/server "hadoo1202" Created /servers/server0000000002 [zk: localhost:2181(CONNECTED) 2] create -e -s /servers/server "hadoop1203" Created /servers/server0000000003 [zk: localhost:2181(CONNECTED) 3] create -e -s /servers/server "hadoop1204" Created /servers/server0000000004 [zk: localhost:2181(CONNECTED) 4] quit Quitting... 2020-12-20 00:25:34,695 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x1767ab9b8f10004 closed 2020-12-20 00:25:34,698 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x1767ab9b8f10004 [root@localhost zookeeper-3.4.10]#3)java日志?
[hadoo1202] [hadoo1202] [hadoop1203, hadoo1202] [hadoop1204, hadoop1203, hadoo1202] []?
【2】zk客戶端代碼——http訪問的服務(wù)端代碼(向zk寫入 http服務(wù)器上線信息)
/*** 分布式服務(wù)器 */ public class DistributeServer {public static void main(String[] args) throws Exception {DistributeServer server = new DistributeServer();/*1-獲取zk連接 */server.getZkConn(); /*2-注冊(cè)監(jiān)聽節(jié)點(diǎn)*/server.registerServer(args[0]); // 傳入服務(wù)器名稱,如 hadoop102 /*3-業(yè)務(wù)邏輯加工*/server.doBusi();}/*** zk客戶端*/private ZooKeeper zkClient; /*** 注冊(cè)服務(wù)器 * @param hostname*/private void registerServer(String hostname) throws KeeperException, InterruptedException {String path = zkClient.create("/servers/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);System.out.println(hostname + "is online"); System.out.printf("path = %s /n", path); } private void doBusi() throws InterruptedException {Thread.sleep(Long.MAX_VALUE); // 進(jìn)程阻塞 }/*** zk server 連接串 */private final static String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";/*** 超時(shí)時(shí)間*/private final static int sessionTimeout = 3000; /*** 0-獲取zk連接 */public ZooKeeper getZkConn() throws IOException {/* 連接zk服務(wù)器 */zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) { // try { // /*3-獲取子節(jié)點(diǎn)并監(jiān)控節(jié)點(diǎn)變化*/ // 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(); // }}});return zkClient; } }step1) 啟動(dòng) DistributeServer,參數(shù)為?? hadoop102
-- log hadoop102 is online path = /servers/server0000000005??step2) 查看?DistributeClient 控制臺(tái)
-- log [hadoop102]step3) 啟動(dòng) DistributeServer,參數(shù)為?? hadoop103
-- loghadoop103 is online path = /servers/server0000000006step4) 查看?DistributeClient 控制臺(tái)
-- log [hadoop103, hadoop102]step5)關(guān)閉?DistributeServer 進(jìn)程(參數(shù)為 hadoop103 的進(jìn)程)
查看?DistributeClient 控制臺(tái)
-- log [hadoop102]step6)關(guān)閉?DistributeServer 進(jìn)程(參數(shù)為 hadoop102 的進(jìn)程)
查看?DistributeClient 控制臺(tái)
-- log []?
總結(jié)
以上是生活随笔為你收集整理的java本地监听zk服务器节点【动态上下线】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪宋 L 新车内饰曝光,有望首搭大尺
- 下一篇: 基于centos8搭建zookeeper