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

目录

需求

 实现

1. 集群

2. 服务器

3.客户端

4. 测试

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

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

 来源:


需求

某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知
到主节点服务器的上下线。
【zookeeper】服务器动态上下线监听案例_第1张图片

 实现

1. 集群

创建 /servers 节点
[zk: localhost:2181(CONNECTED) 10] create /servers "servers"
Created /servers

2. 服务器

import org.apache.zookeeper.*;
import java.io.IOException;

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, KeeperException, InterruptedException {

        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 KeeperException, InterruptedException {
        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) {

            }
        });
    }
}

3.客户端

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 String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zk;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        DistributeClient client = new DistributeClient();

        // 1 获取zk连接
        client.getConnect();

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

        // 3 业务逻辑(睡觉)
        client.business();

    }

    private void business() throws InterruptedException {
        Thread.sleep(Long.MAX_VALUE);
    }

    private void getServerList() throws KeeperException, InterruptedException {
        List children = zk.getChildren("/servers", true);

        ArrayList servers = new ArrayList<>();

        for (String child : children) {

            byte[] data = zk.getData("/servers/" + child, false, null);

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

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

    private void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

                try {
                    getServerList();
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

4. 测试

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

1 )启动 DistributeClient 客户端
(2)在 hadoop102 zk 的客户端 /servers 目录上创建临时带序号节点
[zk: localhost:2181(CONNECTED) 1] create -e -s 
/servers/hadoop102 "hadoop102"
[zk: localhost:2181(CONNECTED) 2] create -e -s 
/servers/hadoop103 "hadoop103"
(3)观察 Idea 控制台变化
(4)执行删除操作
[zk: localhost:2181(CONNECTED) 8] delete /servers/hadoop1020000000000
(5)观察 Idea 控制台变化

Idea 上操作增加减少服务器

1 )启动 DistributeClient 客户端(如果已经启动过,不需要重启)
(2)启动 DistributeServer 服务
【zookeeper】服务器动态上下线监听案例_第2张图片

 

【zookeeper】服务器动态上下线监听案例_第3张图片 

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

【zookeeper】服务器动态上下线监听案例_第4张图片 

 【zookeeper】服务器动态上下线监听案例_第5张图片

 来源:

尚硅谷

 

你可能感兴趣的:(大数据框架,服务器,zookeeper)