日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java本地监听zk服务器节点【动态上下线】

發布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java本地监听zk服务器节点【动态上下线】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【README】

java本地訪問 zk cluster, refer 2?https://blog.csdn.net/PacosonSWJTU/article/details/111404364?

【1】 客戶端監聽 zk節點變化?

1) zk客戶端代碼——http訪問的客戶端代碼(請求zk獲取 http服務器的上下線信息)

(對于zk來說,http的server或client都是客戶端,這里要能夠理解,本文只是將htpt作為例子以便理解,當然也可以是其他請求協議)?

/*** 分布式客戶端 */ public class DistributeClient {public static void main(String[] args) throws Exception {DistributeClient client = new DistributeClient();/*1-獲取zk連接 */client.getZkConn(); /*2-注冊監聽節點*/ client.registerListener();/*3-業務邏輯加工*/client.doBusi();}/*** zk客戶端*/private ZooKeeper zkClient;/*** 獲取子節點 * @throws KeeperException* @throws InterruptedException */private void registerListener() throws KeeperException, InterruptedException {/* 監聽servers路徑 */List<String> children = zkClient.getChildren("/servers", true);/* 存儲服務器節點主機名稱集合 */ArrayList<String> hosts = new ArrayList<>(); for(String child : children) {byte[] data = zkClient.getData("/servers/" + child, false, null);hosts.add(new String(data)); } /* 將所有主機名稱打印到控制臺 */System.out.println(hosts); }private void doBusi() throws InterruptedException {Thread.sleep(Long.MAX_VALUE); // 進程阻塞 }/*** zk server 連接串 */private final static String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";/*** 超時時間*/private final static int sessionTimeout = 3000; /*** 0-獲取zk連接 */public ZooKeeper getZkConn() throws IOException {/* 連接zk服務器 */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節點操作

[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訪問的服務端代碼(向zk寫入 http服務器上線信息)

/*** 分布式服務器 */ public class DistributeServer {public static void main(String[] args) throws Exception {DistributeServer server = new DistributeServer();/*1-獲取zk連接 */server.getZkConn(); /*2-注冊監聽節點*/server.registerServer(args[0]); // 傳入服務器名稱,如 hadoop102 /*3-業務邏輯加工*/server.doBusi();}/*** zk客戶端*/private ZooKeeper zkClient; /*** 注冊服務器 * @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); // 進程阻塞 }/*** zk server 連接串 */private final static String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";/*** 超時時間*/private final static int sessionTimeout = 3000; /*** 0-獲取zk連接 */public ZooKeeper getZkConn() 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(); // }}});return zkClient; } }

step1) 啟動 DistributeServer,參數為?? hadoop102

-- log hadoop102 is online path = /servers/server0000000005??

step2) 查看?DistributeClient 控制臺

-- log [hadoop102]

step3) 啟動 DistributeServer,參數為?? hadoop103

-- loghadoop103 is online path = /servers/server0000000006

step4) 查看?DistributeClient 控制臺

-- log [hadoop103, hadoop102]

step5)關閉?DistributeServer 進程(參數為 hadoop103 的進程)

查看?DistributeClient 控制臺

-- log [hadoop102]

step6)關閉?DistributeServer 進程(參數為 hadoop102 的進程)

查看?DistributeClient 控制臺

-- log []

?

總結

以上是生活随笔為你收集整理的java本地监听zk服务器节点【动态上下线】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。