在平川直至山脉|

逆十字

园龄:3年4个月粉丝:3关注:3

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

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

需求

某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知 到主节点服务器的上下线。

需求分析

具体实现

  • 先在集群上创建/servers节点

    [zk: localhost:2181(CONNECTED) 10] create /servers "servers" Created /servers
    
  • IDEA中创建包明:com.atguigu.case1

  • 服务器端向Zookeeper注册代码

    package com.atguigu.case1;
    
    import org.apache.zookeeper.*;
    
    import java.io.IOException;
    
    /**
     * @author: fxl
     * @Description:
     * @Data:Create in  2022-02-05
     * @Modified By:
     */
    public class DistributeServer {
        private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        private int sessionTimeout = 2000;
        private ZooKeeper zk;
    
        public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
            DistributeServer server = new DistributeServer();
            //1. 获取zk链接
            server.getConnect();
            //2. 注册服务器zk集群
            server.regist(args[0]);
            //3. 启动业务逻辑(休眠)
            server.business();
        }
    
        private void business() throws InterruptedException {
            Thread.sleep(Long.MAX_VALUE);
        }
    
        private void regist(String hostname) throws InterruptedException, KeeperException {
            String create = zk.create("/servers/"+hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println(hostname + "is online");
        }
    
    
        private void getConnect() throws IOException {
            zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
    
                }
            });
        }
    }
    
  • 客户端代码

    package com.atguigu.case1;
    
    import org.apache.zookeeper.KeeperException;
    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;
    
    /**
     * @author: fxl
     * @Description:
     * @Data:Create in  2022-02-05
     * @Modified By:
     */
    public class DistributeClient {
        private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        private int sessionTimeout = 2000;
        private ZooKeeper zk;
    
        public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
            DistributeClient client = new DistributeClient();
            //1. 获取zk连接
            client.getConnnect();
            //2.监听/servers下面子节点的增加和删除
            client.getServerList();
            //3.业务逻辑(休眠)
            client.business();
        }
    
        private void business() throws InterruptedException {
            Thread.sleep(Long.MAX_VALUE);
        }
    
        private void getServerList() throws InterruptedException, KeeperException {
            List<String> children = zk.getChildren("/servers"true);
            ArrayList<String> servers = new ArrayList<>();
            for (String child : children) {
                byte[] data = zk.getData("/servers/" + child, falsenull);
                servers.add(new String(data));
            }
            //打印
            System.out.println(servers);
        }
    
        private void getConnnect() throws IOException {
            zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    try {
                        getServerList();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    

测试

  • Linux命令行上操作增加减少服务器

    • 启动DistributeClient客户端

      • hadoop102zk客户端/servers目录上创建临时带序号节点

        [zk: localhost:2181(CONNECTED) 1] create -e -s 
        /servers/hadoop102 "hadoop102"
        [zk: localhost:2181(CONNECTED) 2] create -e -s 
        /servers/hadoop103 "hadoop103"
        
      • 观察IDEA控制台变化

        hadoop102, hadoop103]

      • 执行删除操作

        [zk: localhost:2181(CONNECTED) 8] delete /servers/hadoop1020000000000
        
      • 观察 Idea 控制台变化

        [hadoop103]

  • 在 Idea 上操作增加减少服务器

    • 启动DistributeClient客户端(如果已经启动过,不需要重启)

    • 启动 DistributeServer服务

      • ①点击 Edit Configurations

      • 在弹出的窗口中(Program arguments)输入想启动的主机,例如,hadoop102

      • 回到 DistributeServermain 方 法 , 右 键 , 在 弹 出 的 窗 口 中 点 击 Run “DistributeServer.main()”

      • 观察 DistributeServer 控制台,提示 hadoop102 is working

      • 观察 DistributeClient控制台,提示hadoop102 已经上线

本文作者:逆十字

本文链接:https://www.cnblogs.com/fengxiaolong/p/15864452.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   逆十字  阅读(95)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开
  1. 1 MELANCHOLY White Cherry
MELANCHOLY - White Cherry
00:00 / 00:00
An audio error has occurred.