Zookeeper实战案例(1)

前置知识:

Zookeeper学习笔记(1)------ 基础知识-CSDN博客

Zookeeper学习笔记(2)------ Zookeeper API简单操作-CSDN博客

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

需求分析

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

具体实现

首先创建节点servers:create /servers "servers"

服务器向zookeeper注册的代码

java 复制代码
package com.why.zkCase;

import org.apache.zookeeper.*;
import org.junit.Before;

import java.io.IOException;

//服务端向zookeeper注册
public class DistributeServer {
    private static String connetString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; //客户端连接ip
    private static int sessionTimeout = 2000; //超时时间
    private ZooKeeper zkClient = null; //客户端对象
    private String parentNode = "/servers"; //父节点路径

    @Before
    public void getConnect() throws IOException {
        zkClient = new ZooKeeper(connetString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                //收到事件通知后的回调函数
                System.out.println("事件类型:" + watchedEvent.getType());
                System.out.println("事件路径:" + watchedEvent.getPath());
            }
        });
    }

    //注册服务器
    public void registServer(String hostname) throws InterruptedException, KeeperException {
        String create = zkClient.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname + "is online" + create);
    }

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

    public static void main(String[] args) throws Exception {
        // 1 获取 zk 连接
        DistributeServer server = new DistributeServer();
        server.getConnect();
        // 2 利用 zk 连接注册服务器信息
        server.registServer(args[0]);
        // 3 启动业务功能
        server.business(args[0]);
    }
}

客户端代码

java 复制代码
package com.why.zkCase;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//客户端
public class DistributeClient {
    private static String connetString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; //客户端连接ip
    private static int sessionTimeout = 2000; //超时时间
    private ZooKeeper zkClient = null; //客户端对象
    private String parentNode = "/servers"; //父节点路径

    //创建到zk的客户端连接
    @Before
    public void getConnect() throws IOException {
        zkClient = new ZooKeeper(connetString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                //收到事件通知后的回调函数
                System.out.println("事件类型:" + watchedEvent.getType());
                System.out.println("事件路径:" + watchedEvent.getPath());
                // 再次启动监听
                try {
                    getServerList();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //获取服务器列表信息
    public void getServerList() throws InterruptedException, KeeperException {
        //获取服务器子节点信息,并对父节点进行监听
        List<String> children = zkClient.getChildren(parentNode, true);

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

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

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

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

    public static void main(String[] args) throws Exception {
        // 1 获取 zk 连接
        DistributeClient client = new DistributeClient();

        client.getConnect();
        // 2 获取 servers 的子节点信息,从中获取服务器信息列表
        client.getServerList();
        // 3 业务进程启动
        client.business();
    }
}

测试

命令行操作

启动DistributeClient客户端

在zk的命令行中新建节点:create -e -s /servers/hadoop103 "hadoop103"

在idea的控制台可以看到:

删除hadoop103:delete /servers/hadoop1030000000001

可以看到:

idea操作

启动 DistributeClient 客户端

启动 DistributeServer 服务:

添加参数:

然后启动;

可以看到:

同时client也可以监听到服务器上线通知:

相关推荐
harmony&20 小时前
Docker 镜像完全指南:从最小镜像到企业级私有仓库 Harbor
云原生·eureka
DolitD21 小时前
云流技术深度剖析:单服务器下如何实现3D应用的多实例并发?
java·服务器·前端·3d·云原生·云计算
2601_9621756621 小时前
RabbitMQ 的工作模式
分布式·rabbitmq
头茬韭菜1 天前
图解 Fluss(四):分布式协调 —— 选举、副本状态机与
分布式·fluss
CJY6211 天前
k8spod管理
云原生·容器·kubernetes
2601_962218471 天前
万象生鲜系统区块链溯源技术帮助生鲜企业搭建食品安全数字化体系
大数据·运维·微服务·云原生·架构
Henry-SAP1 天前
SAP独立需求与相关需求的区别解析
人工智能·云原生·sap·erp
GGG7662 天前
Python 运维自动化:psutil+IPy 系统与网络信息采集深度实战
运维·python·云原生·pip
w200512242 天前
Pod管理
linux·服务器·云原生·容器·k8s·podman
sryyd_022 天前
Kubernetes Pod 管理从入门到实战:命令、YAML 与生命周期全解析
云原生·容器·kubernetes