In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to read configuration information through monitoring in zk. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Import com.alibaba.fastjson.JSON;import com.google.common.collect.Maps;import lombok.extern.slf4j.Slf4j;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;import java.io.IOException;import java.util.Map;import java.util.concurrent.ConcurrentMap;import java.util.concurrent.CountDownLatch;import java.util.concurrent.atomic.AtomicInteger @ Slf4jpublic class ZkClientWatcher implements Watcher {private static final String CONNECT_ADDRESS = "10.10.136.114 private static final int SESSION_TIMEOUT 2181; private static final int SESSION_TIMEOUT = 2000; public static final int MAX = 3; private static String CONFIG_PATH =" / application-config "; private static ZooKeeper zk; private static CountDownLatch countDownLatch = new CountDownLatch (1); private static ConcurrentMap oldConfig = Maps.newConcurrentMap (); private static ConcurrentMap newConfig = Maps.newConcurrentMap () Private static AtomicInteger count = new AtomicInteger (1); public void createConnection (String connectAddres, int sessionTimeOut) throws InterruptedException {try {zk = new ZooKeeper (connectAddres, sessionTimeOut, this); log.info ("zk connecting...");} catch (IOException e) {e.printStackTrace () } finally {} public boolean createPath (String path, String data) {try {if (null = = zk.exists (path, true)) {zk.create (path, data.getBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); log.info ("node create success") }} catch (Exception e) {e.printStackTrace (); return false;} return true;} @ Override public void process (WatchedEvent event) {Event.KeeperState keeperState = event.getState (); Event.EventType eventType = event.getType (); String path = event.getPath () Log.info ("process: {}, keeperState: {}, eventType: {}, path: {}", keeperState, eventType, path); if (Event.KeeperState.SyncConnected = = keeperState) {if (Event.EventType.None = = eventType) {log.info ("now status is None") } else if (Event.EventType.NodeCreated = = eventType) {log.info ("now status is nodeCreated,path: {}", path);} else if (Event.EventType.NodeDataChanged = = eventType) {log.info ("now status is nodedataChanged: {}", path) Try {newConfig = JSON.parseObject (zk.getData (path, true, new Stat ()), ConcurrentMap.class); log.info ("newConfig is {}", newConfig); count.getAndIncrement (); if (count.get () > MAX) {countDownLatch.countDown () } catch (Exception e) {log.error ("get data from zk occur exception", e);}} else if (Event.EventType.NodeDeleted = = eventType) {log.info ("now status is node deleted: {}", path) }} public static void main (String [] args) throws Exception {ZkClientWatcher watcher = new ZkClientWatcher (); watcher.createConnection (CONNECT_ADDRESS, SESSION_TIMEOUT); createPathAndInitData (watcher); getOldConfig (); countDownLatch.await (); log.info ("test completeness config has changed 3 times") } / * get configuration information for the first time * * @ throws Exception * / private static void getOldConfig () throws Exception {oldConfig = JSON.parseObject (zk.getData (CONFIG_PATH, true, new Stat (), ConcurrentMap.class); log.info ("oldConfig is {}", oldConfig) } / * create path settings initial value * * @ param watcher * / private static void createPathAndInitData (ZkClientWatcher watcher) {Map maps = Maps.newConcurrentMap (); maps.put ("spring.data.status.start", "true"); maps.put ("switch", "on"); maps.put ("startTime", "2019-10-24 12:00:00") Maps.put ("coder", "1024"); watcher.createPath (CONFIG_PATH, JSON.toJSONString (maps));}} this is how the zk shared by the editor reads configuration information through monitoring. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.