【ZooKeeper】分布式系统服务器上下线自动感知程序开发
来源:互联网 发布:nginx alias用法 编辑:程序博客网 时间:2024/05/21 00:15
需求概述
一个集群来提供服务。集群中的服务器会动态变化,服务器会上线、下线或宕机。此时客户端应该能够知道服务器状态的变化情况以选择一个最好的服务器提供服务。
客户端能获知服务器上下线状态的变化。
解决方案
使用ZooKeeper框架。
服务器启动时先向ZooKeeper集群注册,写入自己的信息。注册的节点是一种临时节点。最好也是序列化的节点
客户端启动时获取服务器父目录下的子节点列表并注册监听,获取到当前在线的服务器列表并根据连接策略选择服务器连接。在收到服务器状态变化的监听信息后重新获取服务器列表并注册监听。
代码实现
分布式服务器 DistributedServer.java
package com.mrbcy.bigdata.zk;import java.io.IOException;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.data.Stat;public class DistributedServer { private static String connectString = "amaster:2181,anode1:2181,anode2:2181"; private static int sessionTimeout = 2000; private ZooKeeper zk = null; private String hostName; private String groupName = "/servers"; public DistributedServer(String hostName) throws Exception{ this.hostName = hostName; this.zk = new ZooKeeper(connectString , sessionTimeout, null); } // 将自己的信息注册到zk集群 public void registToZK() throws Exception { // 判断父目录是否存在,不存在则创建 Stat groupStat = zk.exists(groupName, false); if(groupStat == null){ zk.create(groupName, "distributed server list".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } String registAddr = zk.create(groupName+"/server", hostName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println("Server is starting, reg addr:" + registAddr); } // 下线 public void offline() throws Exception { zk.close(); }}
客户端 DistributedClient.java
package com.mrbcy.bigdata.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.Watcher.Event.EventType;import org.apache.zookeeper.ZooKeeper;public class DistributedClient { private static String connectString = "amaster:2181,anode1:2181,anode2:2181"; private static int sessionTimeout = 2000; private ZooKeeper zk = null; private String groupName = "/servers"; // 开始监听服务器列表变化 public void startListenServerListChange() throws Exception{ this.zk = new ZooKeeper(connectString , sessionTimeout, new Watcher(){ @Override public void process(WatchedEvent event) {// System.out.println(event.toString());// System.out.println(event.getType() + "---" + event.getPath()); // 重新注册监听 try { getServerList(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } private void getServerList() throws Exception { List<String> children = zk.getChildren(groupName, true); List<String> servers = new ArrayList<String>(); for(String child : children) { byte[] data = zk.getData(groupName+"/"+child, null, null); servers.add(new String(data)); } System.out.println("--------------当前服务器列表--------------"); for(String server : servers){ System.out.println(server); } System.out.println("-----------------------------------------"); }}
服务器启动程序 DynamicServerListTest.java
package com.mrbcy.bigdata.zk;import org.junit.Test;public class DynamicServerListTest { @Test public void testServerUp() throws Exception{ DistributedServer server1 = new DistributedServer("server01"); DistributedServer server2 = new DistributedServer("server02"); server1.registToZK(); server2.registToZK(); Thread.sleep(10000); server1.offline(); System.out.println("server01下线..."); Thread.sleep(10000); System.out.println("server02下线..."); }}
客户端启动程序 DynamicClientTest.java
package com.mrbcy.bigdata.zk;import org.junit.Test;public class DynamicClientTest { @Test public void startUpClient() throws Exception{ DistributedClient client = new DistributedClient(); client.startListenServerListChange(); Thread.sleep(1000000); }}
测试结果
服务器端:
Server is starting, reg addr:/servers/server0000000014Server is starting, reg addr:/servers/server0000000015server01下线...server02下线...
客户端:
--------------当前服务器列表---------------------------------------------------------------------当前服务器列表--------------server02server01-------------------------------------------------------当前服务器列表--------------server02-------------------------------------------------------当前服务器列表-------------------------------------------------------
0 0
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发
- ZooKeeper分布式应用系统服务器上下线动态感知简单程序
- zookeeper实现分布式应用系统服务器上下线动态感知程序、监听机制与守护线程
- 分布式应用系统服务器上下线动态感知程序开发学习笔记
- 利用zookeeper实现分布式运用服务器上下线的动态感知-简单程序
- zookeeper 服务器动态上下线感知
- 通过Zookeeper动态感知服务器上下线[案例]
- zookeeper实时感知到主节点服务器的上下线
- 动态感知服务器上下线
- 学习笔记:Zookeeper 应用案例(上下线动态感知)
- Zookeeper应用:服务端上下线
- 大主播 上下线通知 程序
- ZooKeeper: 一个用于分布式系统的分布式协作服务程序
- AP下线数据自动查询程序(三)
- AP下线数据自动查询程序(三)
- 分布式系统协调zookeeper
- 分布式系统协调 ZooKeeper
- 分布式系统协调 ZooKeeper
- 22. Generate Parentheses
- qt 学习笔记6 360 Labe
- 在SSM中使用shiro实现登录验证(附密码加密)
- 23. Merge k Sorted Lists
- 安卓下Glide缓存问题
- 【ZooKeeper】分布式系统服务器上下线自动感知程序开发
- 浅析 UIApplication 生命周期的一些 delegate 方法
- 【PAT】1008. Elevator
- 蓝桥杯 - 算法训练 - 删除数组零元素 - Java
- js树
- 24. Swap Nodes in Pairs
- vue学习 第四节
- 【问题收录】INSTALL_FAILED_NO_MATCHING_ABIS
- 25. Reverse Nodes in k-Group