zookeeper 服务器动态上下线感知
来源:互联网 发布:2016年房地产投资数据 编辑:程序博客网 时间:2024/05/16 02:38
服务端
package com.asin.zk;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;public class DistributeServer {private static final String HOSTS = "h1:2181,h2:2181,h3:2181";private static final String parentPath = "/servers";private static final int session_time_out = 2000;private static ZooKeeper zk = null;public static void main(String[] args) throws Exception {connect();registServer(args[0]);opearte();}public static void connect() throws Exception {zk = new ZooKeeper(HOSTS, session_time_out, new Watcher() {public void process(WatchedEvent event) {System.out.println(event.getType() + "---" + event.getPath());try {zk.getChildren("/", true);} catch (Exception e) {e.printStackTrace();}}});}public static void registServer(String hostname) throws Exception {String create = zk.create(parentPath + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);System.out.println(create);}public static void opearte() throws Exception {System.out.println("Server处理中。。。");Thread.sleep(Integer.MAX_VALUE);}}
客户端
package com.asin.zk;import java.util.ArrayList;import java.util.List;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;public class DistributeClient {private static final String HOSTS = "h1:2181,h2:2181,h3:2181";private static final String parentPath = "/servers";private static final int session_time_out = 2000;private static ZooKeeper zk = null;public static void main(String[] args) throws Exception {connect();opearte();}public static void connect() throws Exception {zk = new ZooKeeper(HOSTS, session_time_out, new Watcher() {public void process(WatchedEvent event) {System.out.println(event.getType() + "---" + event.getPath());try {getServerList(); //获取有哪些节点} catch (Exception e) {e.printStackTrace();}}});}public static void getServerList() throws Exception {ArrayList<String> list = new ArrayList<String>();List<String> children = zk.getChildren(parentPath, true);for (String child : children) {byte[] data = zk.getData(parentPath + "/" + child, false, null);list.add(new String(data));}System.out.println(list);}public static void opearte() throws Exception {System.out.println("Client处理中。。。");Thread.sleep(Integer.MAX_VALUE);}}
效果就在客户端获取事件通知后,再次获取目录,可以列出最新的节点。
阅读全文
0 0
- zookeeper 服务器动态上下线感知
- 通过Zookeeper动态感知服务器上下线[案例]
- 动态感知服务器上下线
- ZooKeeper分布式应用系统服务器上下线动态感知简单程序
- zookeeper实现分布式应用系统服务器上下线动态感知程序、监听机制与守护线程
- 利用zookeeper实现分布式运用服务器上下线的动态感知-简单程序
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发
- zookeeper实时感知到主节点服务器的上下线
- 学习笔记:Zookeeper 应用案例(上下线动态感知)
- 分布式应用系统服务器上下线动态感知程序开发学习笔记
- Zookeeper应用:服务端上下线
- Zookeeper实现服务上下线监控服务列表
- 动态上下线datanode节点及副本均衡机制
- 基于Nginx dyups模块的站点动态上下线
- 【Mrpc】Demo2 基于Zookeeper的服务器感知及负载均衡
- Zookeeper动态更新服务器地址
- Zookeeper动态更新服务器列表
- zookeeper深入学习(2)---客户端监控服务器端的上下线
- 只有一个窗口的政务服务中心
- ListView中嵌套ListView
- 数组中集合对象种字段的增加和删除
- 执行Docker images命令,出现Docker deamon at unix:///var/run/docker.sock.Is the docker deamon running?
- Blast结果的详细解析
- zookeeper 服务器动态上下线感知
- H5图片上传
- TextInputLayout
- ubuntu下安装zshell
- 【OpenCV3图像处理】图像特征点检测
- 对文本分类的函数
- ubuntu安装sshd错误
- AI³驱动,全联接牵引:实现向智能制造升级
- Linux-----Linux源码安装软件