分布式应用系统服务器上下线动态感知程序开发学习笔记
来源:互联网 发布:淘宝led散热片 编辑:程序博客网 时间:2024/06/05 09:59
分布式应用系统服务器上下线动态感知程序
package cn.itcast.bigdata.zkdist;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 DistributedServer { private static final String connectString = "mini1:2181,mini2:2181,mini3:2181"; private static final int sessionTimeout = 2000; private static final String parentNode = "/servers"; private ZooKeeper zk = null; /** * 创建到zk的客户端连接 * * @throws Exception */ public void getConnect() throws Exception { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑) System.out.println(event.getType() + "---" + event.getPath()); try { zk.getChildren("/", true); } catch (Exception e) { } } }); } /** * 向zk集群注册服务器信息 * * @param hostname * @throws Exception */ public void registerServer(String hostname) throws Exception { String create = zk.create(parentNode + "/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostname + "is online.." + create); } /** * 业务功能 * * @throws InterruptedException */ public void handleBussiness(String hostname) throws InterruptedException { System.out.println(hostname + "start working....."); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception { // 获取zk连接 DistributedServer server = new DistributedServer(); server.getConnect(); // 利用zk连接注册服务器信息 server.registerServer(args[0]); // 启动业务功能 server.handleBussiness(args[0]); }}
分布式应用系统客户端上下线动态感知程序
package cn.itcast.bigdata.zkdist;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 DistributedClient { private static final String connectString = "mini1:2181,mini2:2181,mini3:2181"; private static final int sessionTimeout = 2000; private static final String parentNode = "/servers"; // 注意:加volatile的意义何在? private volatile List<String> serverList; private ZooKeeper zk = null; /** * 创建到zk的客户端连接 * * @throws Exception */ public void getConnect() throws Exception { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑) try { //重新更新服务器列表,并且注册了监听 getServerList(); } catch (Exception e) { } } }); } /** * 获取服务器信息列表 * * @throws Exception */ public void getServerList() throws Exception { // 获取服务器子节点信息,并且对父节点进行监听 List<String> children = zk.getChildren(parentNode, true); // 先创建一个局部的list来存服务器信息 List<String> servers = new ArrayList<String>(); for (String child : children) { // child只是子节点的节点名 byte[] data = zk.getData(parentNode + "/" + child, false, null); servers.add(new String(data)); } // 把servers赋值给成员变量serverList,已提供给各业务线程使用 serverList = servers; //打印服务器列表 System.out.println(serverList); } /** * 业务功能 * * @throws InterruptedException */ public void handleBussiness() throws InterruptedException { System.out.println("client start working....."); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception { // 获取zk连接 DistributedClient client = new DistributedClient(); client.getConnect(); // 获取servers的子节点信息(并监听),从中获取服务器信息列表 client.getServerList(); // 业务线程启动 client.handleBussiness(); }}
阅读全文
0 0
- 分布式应用系统服务器上下线动态感知程序开发学习笔记
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发
- ZooKeeper分布式应用系统服务器上下线动态感知简单程序
- zookeeper实现分布式应用系统服务器上下线动态感知程序、监听机制与守护线程
- 学习笔记:Zookeeper 应用案例(上下线动态感知)
- 利用zookeeper实现分布式运用服务器上下线的动态感知-简单程序
- 动态感知服务器上下线
- zookeeper 服务器动态上下线感知
- 通过Zookeeper动态感知服务器上下线[案例]
- zookeeper实时感知到主节点服务器的上下线
- Zookeeper应用:服务端上下线
- 大主播 上下线通知 程序
- 分布式系统学习笔记
- 感知机算法及应用学习笔记
- 分布式系统的工程化开发方法,学习笔记
- 应用服务器学习笔记
- 神经网络学习笔记(九):多层感知机(上)
- 在windows平台上部署伪分布式系统开发hadoop程序
- G
- 基数排序
- MongoDB开发学习(2)索引的基本操作
- HDU6172-Array Challenge
- [py]一步一步实现tornado form验证
- 分布式应用系统服务器上下线动态感知程序开发学习笔记
- not in 和 not exists 区别
- mongodb常用命令
- python中文件基本操作
- keilc 4 编译出现的几个错误解决
- QT5.9 在线程中导出EXCLE报 CoCreateInstance failure (尚未调用 CoInitialize) 解决办法
- CFGym 101002C 状压DP
- oracle内置函数
- python之Flask(一)