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

服务器动态上下线监听案例,第1张

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

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

文章目录

1.需求2.需求分析3.编程实现

1.先在集群上创建/servers节点2.服务器端向Zookeeper注册代码(Server)3.客户端代码(Client) 4.测试

1.在Linux命令行上 *** 作增加减少服务器2.在IDEA上 *** 增加减少服务器

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

3.编程实现 1.先在集群上创建/servers节点

进入客户端后

create /servers "servers"

2.服务器端向Zookeeper注册代码(Server)
package com.yingzi.zkcase1;

import org.apache.zookeeper.*;

import java.io.IOException;


public class DistributeServer {
    private static int sessionTimeout = 2000;
    private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private ZooKeeper zk = null;
    private String parentNode = "/servers";



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

        DistributeServer server = new DistributeServer();
        //1.获取zk连接
        server.getConnect();
        //2.注册服务器到zk集群
        server.regist(args[0]);
        //启动业务逻辑
        server.business(args[0]);
    }

    //1.获取zk连接
    private void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {

            }
        });

    }

    //2.注册服务器到zk集群
    private void regist(String hostname) throws InterruptedException, KeeperException {
        String create = zk.create(parentNode + "/server",hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname + "is online" + create);
    }

    //3.启动业务逻辑
    private void business(String hostname) throws InterruptedException {
        System.out.println(hostname + " is working ...");

        Thread.sleep(Long.MAX_VALUE);
    }
}
3.客户端代码(Client)
package com.yingzi.zkcase1;

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;


public class DistributeClient {

    private static String getConnectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private static int sessionTimeout = 2000;
    private ZooKeeper zk = null;
    private String parentNode = "/servers";

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

        DistributeClient client = new DistributeClient();
        //1.获取zk连接
        client.getConnect();
        //2.监听/servers下面子节点的增加和删除
        client.getServerList();
        //3.业务逻辑(睡觉)
        client.business();
    }

    //获取zk连接
    private void getConnect() throws IOException {
        zk = new ZooKeeper(getConnectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    getServerList();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (KeeperException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    //获取服务器列表信息
    public void getServerList() throws InterruptedException, KeeperException {

        //1.获取服务器子节点信息,并且对父节点进行监听
        List children = zk.getChildren(parentNode, true);

        //2.存储服务器信息列表
        ArrayList servers = new ArrayList<>();

        //3.遍历所有节点,获取节点中的主机名称信息
        for (String child : children) {
            byte[] data = zk.getData(parentNode + "/" + child, false, null);

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

        //4.打印服务器列表信息
        System.out.println(servers);
    }

    //业务逻辑
    public void business() throws InterruptedException {
        System.out.println("client is working ...");
        Thread.sleep(Long.MAX_VALUE);
    }

}
4.测试 1.在Linux命令行上 *** 作增加减少服务器

(1)启动DistributeClient客户端

(2)在 hadoop102 上 zk 的客户端/servers 目录上创建临时带序号节点

(3)观察Idea控制台变化

(4)执行删除 *** 作


(5)观察Idea控制台变化

2.在IDEA上 *** 增加减少服务器

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

(2)启动 DistributeServer 服务

①点击 Edit Configurations…

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

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

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5708082.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存