Zookeeper当中客户端得知服务端上下线信息案例

本文介绍了使用ApacheZooKeeper实现的分布式服务器注册过程,包括获取zk连接、在zk集群中创建节点以及客户端如何监听节点并执行对应业务。
摘要由CSDN通过智能技术生成

总体预览

服务器注册

1.获取zk连接

2.注册服务器到zk集群(其实就是在对应的节点下面创建节点)

3.启动业务逻辑(这里是睡觉)

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 = null;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        //创建对象
        DistributeServer ds = new DistributeServer();
        //1.获取zk连接
        ds.getConnect();
        //2.注册服务器到zk集群
        ds.registServer(args[0]);
        //3.启动业务逻辑(这里是睡觉)
        ds.business(args[0]);
    }

    //1.获取zk连接
    public void getConnect() throws IOException {
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    //2.注册服务器
    //其实就是在对应的节点下面创建节点
    public void registServer(String hostname) throws InterruptedException, KeeperException {
        //创建节点
        zooKeeper.create("/servers/" + hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname + " is working . . .");
    }

    //3.业务执行
    public void business(String hostname) throws InterruptedException {
        System.out.println(hostname + " is working . . .");
        Thread.sleep(Long.MAX_VALUE);
    }
}

客户端监听

1.建立连接

2.对节点进行监听

3.执行对应业务

package com.atguigu.zk.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;

public class DistributeClient {

    private ZooKeeper zk = null;
    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        DistributeClient dc = new DistributeClient();
        //1.建立连接
        dc.getConnect();
        //2.对节点进行监听
        dc.getServerList();
        //3.执行业务
        dc.business();
    }
    //业务流程
    private void business() throws InterruptedException {
        System.out.println("client is working . . .");
        Thread.sleep(Long.MAX_VALUE);
    }

    //监听节点
    private void getServerList() throws InterruptedException, KeeperException {
        //1.对父节点进行监听,并获取子节点
        List<String> children = zk.getChildren("/servers", true);
        ArrayList<String> hostnames = new ArrayList<>();
        //2.遍历所有节点,获取主机名称信息,并添加到数组中
        for (String child : children) {
            byte[] data = zk.getData("/servers/" + child, false, null);
            hostnames.add(new String(data));
        }
        System.out.println(hostnames);
    }

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值