一、监听某一节点内容
/** * @author: PrincessHug * @date: 2019/2/25, 14:28 * @Blog: https://www.cnblogs.com/HelloBigTable/ * 监听一个节点内容的变化 */ public class WatchZoneDemo { ZooKeeper zkCli = null; public static void main(String[] args) throws IOException, InterruptedException { WatchZoneDemo wz = new WatchZoneDemo(); wz.getConnection(); Thread.sleep(Long.MAX_VALUE); } private void getConnection() throws IOException { zkCli = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() { public void process(WatchedEvent watchedEvent) { try { byte[] data = zkCli.getData("/Wyh", true, null); System.out.println("监听类型为:" + watchedEvent.getType()); System.out.println("监听路径为:" + watchedEvent.getPath()); System.out.println("数据被修改为:" + new String(data)); System.out.println("======================================="); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
二、监听某节点目录的变化
/** * @author: PrincessHug * @date: 2019/2/25, 14:57 * @Blog: https://www.cnblogs.com/HelloBigTable/ * 监听一个节点的子节点的变化 */ public class WatchChildrenDemo { ZooKeeper zkCli = null; public static void main(String[] args) throws IOException, InterruptedException { WatchChildrenDemo wc = new WatchChildrenDemo(); wc.getConnction(); Thread.sleep(Long.MAX_VALUE); } private void getConnction() throws IOException { zkCli = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() { public void process(WatchedEvent watchedEvent) { ArrayList<String> nodes = new ArrayList<String>(); try { List<String> children = zkCli.getChildren("/", true); for (String c:children){ nodes.add(c); } System.out.println(nodes); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
三、Zookeeper当太上下线的感知系统
1.需求:某分布式系统中,主节点有多台,可以进行动态上下限,当有任何一台机器发生了动态的上下线, 任何一台客户端都能感知得到
2.思路:
(1)创建客户端与服务端
(2)启动client端 并监听
(3)启动server端 并注册
(4)当server端发生上下线
(5)client端都能感知的到
3.代码
public class ZKServer { ZooKeeper zk = null; private String parentNode = "/Servers"; public static void main(String[] args) throws IOException, KeeperException, InterruptedException { String childNode = "hd1-1"; ZKServer zkServer = new ZKServer(); //获取连接 zkServer.getConnection(); //注册信息 zkServer.regist(childNode); //业务逻辑,提示上线 zkServer.build(childNode); } private void build(String hostname) throws InterruptedException { System.out.println(hostname + "上线了!!"); Thread.sleep(Long.MAX_VALUE); } private void regist(String hostname) throws KeeperException, InterruptedException { String path = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(path); } private void getConnection() throws IOException { zk = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() { public void process(WatchedEvent watchedEvent) { } }); } } public class ZKClient { ZooKeeper zk = null; public static void main(String[] args) throws IOException, KeeperException, InterruptedException { ZKClient zkClient = new ZKClient(); zkClient.getConnection(); zkClient.watching(); } private void watching() throws InterruptedException { Thread.sleep(Long.MAX_VALUE); } private void getConnection() throws IOException { zk = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() { public void process(WatchedEvent watchedEvent) { try { List<String> children = zk.getChildren("/Servers", true); ArrayList<String> node = new ArrayList<String>(); for (String c:children){ byte[] data = zk.getData("/Servers/" + c, true, null); node.add(new String(data)); } System.out.println(node); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }