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/server0000000006step4) 查看?DistributeClient 控制臺
-- log [hadoop103, hadoop102]step5)關閉?DistributeServer 進程(參數為 hadoop103 的進程)
查看?DistributeClient 控制臺
-- log [hadoop102]step6)關閉?DistributeServer 進程(參數為 hadoop102 的進程)
查看?DistributeClient 控制臺
-- log []?
總結
以上是生活随笔為你收集整理的java本地监听zk服务器节点【动态上下线】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比亚迪宋 L 新车内饰曝光,有望首搭大尺
- 下一篇: Meta 首席技术官:在竞争对手面前并未