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

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

需求:某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。
在这里插入图片描述

实现:

先在集群上创建/servers 节点

[zk: localhost:2181(CONNECTED) 10] create /servers "servers"
Created /servers

客户端代码如下

public class DistributeClient {

    // 注意逗号左右不能有空格
    private String connectString = "hadoop113:2181,hadoop114:2181,hadoop115:2181";
    private int sessionTimeout = 2000;
    ZooKeeper zkClient;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        // 1、获取zk连接
        DistributeClient client = new DistributeClient();
        client.getConnect();

        // 2、监听/servers下面节点的增加和删除
        client.getServerList();

        // 3、业务逻辑(sleep)
        client.business();
    }

    private void business() throws InterruptedException {

        Thread.sleep(Long.MAX_VALUE);
    }

    private void getServerList() throws KeeperException, InterruptedException {

        List<String> children = zkClient.getChildren("/servers", true);

        List<String> servers = new ArrayList<String>();

        for (String child : children) {
            byte[] data = zkClient.getData("/servers/" + child, false, null);

            servers.add(new String(data));
        }

        // 打印
        System.out.println(servers);
    }

    private void getConnect() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                // 再次监听
                try {
                    getServerList();
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }
        });
    }
}

服务器代码如下

public class DistributeServer {

    // 注意逗号左右不能有空格
    private String connectString = "hadoop113:2181,hadoop114:2181,hadoop115:2181";
    private int sessionTimeout = 2000;
    ZooKeeper zkClient;


    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        // 1、获取zk连接
        DistributeServer server = new DistributeServer();
        server.getConnect();

        // 2、注册服务器到zk集群
//        server.register(args[0]);
        server.register("hadoop113");

        // 3、启动业务逻辑(sleep)
        server.bussiness();

    }

    private void bussiness() throws InterruptedException {

        Thread.sleep(Long.MAX_VALUE);
    }

    private void register(String hostname) throws KeeperException, InterruptedException {

        zkClient.create("/servers/" + hostname, hostname.getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

        // 创建模式是-e -s即临时的有序号的

        System.out.println(hostname + " is online");
    }

    void getConnect() throws IOException {

        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
}

测试

1)在 Linux 命令行上操作增加减少服务器

(1)启动 DistributeClient 客户端

(2)在 hadoop113 上 zk 的客户端/servers 目录上创建临时带序号节点
[zk: localhost:2181(CONNECTED) 0] create -e -s /servers/hadoop113 "hadoop113"
Created /servers/hadoop1130000000000
[zk: localhost:2181(CONNECTED) 1] create -e -s /servers/hadoop114 "hadoop114"
Created /servers/hadoop1140000000001

(3)观察 Idea 控制台变化
[hadoop113, hadoop114]

(4)执行删除操作
delete /servers/hadoop1140000000001
delete /servers/hadoop1130000000000

(5)观察 Idea 控制台变化
[]

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

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

(2)启动 DistributeServer 服务

(3)观察 Idea 控制台变化
server
hadoop113 is online

client
[hadoop113]

(4)关闭server,过一段时间后,查看client
[]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值