Zookeeper---实现服务器动态上下线

该文章介绍了如何利用Zookeeper创建服务器的临时节点来存储服务器信息,当服务器上线或下线时,这些节点会相应地被创建或删除。服务器端在启动时向Zookeeper注册,客户端则负责监听/servers路径下的变化,以便获取服务器列表并处理事件。
摘要由CSDN通过智能技术生成

在zookeeper中创建子节点/servers用于存储服务器端结点信息
在这里插入图片描述
流程:当启动服务器hadoop102时候,会在zookeeper中/servers下创建临时结点:/servers/hadoop102,当hadoop服务器下线时候,临时结点/servers/hadoop102的信息也会随之消失

服务器端

服务器在启动起来的时候,会向zookeeper注解节点信息,方便将来下线时zookeeper通知客户端
代码部分:

package com.atguigu.zk.case1;

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 zooKeeper;

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

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

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

        //3.启动业务逻辑(睡觉)
        server.bussiness();

    }

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

    private void register(String hostname) throws InterruptedException, KeeperException {
        String s = zooKeeper.create("/servers/"+hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname+"已经上线啦!");
    }

    private void getConnection() throws IOException {

        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
}

客户端

客户端从zookeeper获取服务器列表,注册监听信息
代码部分

package com.atguigu.zk.case1;

import jdk.nashorn.internal.ir.CallNode;
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.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class DistributeClient {
    private String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout=200000;
    private ZooKeeper zooKeeper;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        DistributeClient client = new DistributeClient();
        //获取zk连接
        client.getConnection();
        //监听/servers的增删改
        client.superviser();
        //业务逻辑(睡觉)
        client.business();
    }

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

    private void superviser() throws InterruptedException, KeeperException, UnsupportedEncodingException {
    	//用于存储各个节点信息
        ArrayList<String> childInfo = new ArrayList<>();
        //获取子节点
        List<String> children = zooKeeper.getChildren("/servers", true);
        for (String child : children) {
            byte[] data = zooKeeper.getData("/servers/" + child, false, null);
            childInfo.add(new String(data));
        }
        System.out.println(childInfo);
    }

    private void getConnection() throws IOException {
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    superviser();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (KeeperException e) {
                    throw new RuntimeException(e);
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值