Zookeeper三个监听案例
程序员文章站
2022-08-21 16:32:04
一、监听某一节点内容 二、监听某节点目录的变化 三、Zookeeper当太上下线的感知系统 1.需求:某分布式系统中,主节点有多台,可以进行动态上下限,当有任何一台机器发生了动态的上下线, 任何一台客户端都能感知得到 2.思路: (1)创建客户端与服务端 (2)启动client端 并监听 (3)启动 ......
一、监听某一节点内容
/** * @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(); } } }); } }