服务器动态上下线监听案例

服务器动态上下线监听案例

需求

某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。其实本质就是节点的创建和移除。
在这里插入图片描述

具体实现

1、先在集群上创建/servers 节点
在这里插入图片描述
2、编写服务端java代码

package dynamicSartedAndStop; import org.apache.zookeeper.*; import java.io.IOException;  public class DistributeServer { private static String connectString = "192.168.80.18:2181,192.168.80.19:2181,192.168.80.20:2181"; private static int sessionTimeout = 200000; private ZooKeeper zk = null; private String parentNode = "/servers";  public void getConnect() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { public void process(WatchedEvent watchedEvent) { } }); }  public void registServer(String hostname) throws Exception { String create = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostname + " is online " + create); }  public void business(String hostname) throws Exception {  System.out.println(hostname + " is working ..."); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception {  DistributeServer server = new DistributeServer(); server.getConnect();  server.registServer(args[0]);  server.business(args[0]); } } 

3、编写客户端java代码

package dynamicSartedAndStop; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.ArrayList; import java.util.List;  public class DistributeClient { private static String connectString = "192.168.80.18:2181,192.168.80.19:2181,192.168.80.20:2181";  private static int sessionTimeout = 200000; private ZooKeeper zk = null; private String parentNode = "/servers";  public void getConnect() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { public void process(WatchedEvent event) {  try { getServerList(); } catch (Exception e) { e.printStackTrace(); } } }); }  public void getServerList() throws Exception {  List<String> children = zk.getChildren(parentNode, true);  ArrayList<String> servers = new ArrayList<>();  for (String child : children) { byte[] data = zk.getData(parentNode + "/" + child, false, null); servers.add(new String(data)); }  System.out.println(servers); }  public void business() throws Exception { System.out.println("client is working ..."); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws Exception {  DistributeClient client = new DistributeClient(); client.getConnect();  client.getServerList();  client.business(); } } 

测试

1、在 Linux 命令行上操作增加减少服务器
在这里插入图片描述
2、在 Idea 上操作增加减少服务器
(1)启动 DistributeClient 客户端(如果已经启动过,不需要重启)
(2)启动 DistributeServer 服务
①点击 Edit Configurations
在这里插入图片描述
②运行服务端代码

在这里插入图片描述
可以看到此时客户端监听到了我们指定的机器上线~

原文链接:https://blog.csdn.net/qq_21040559/article/details/120943285

原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/8114

(0)
上一篇 2022年9月4日
下一篇 2022年9月4日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

优速盾注册领取大礼包www.cdnb.net
/sitemap.xml