服务器节点动态上下线案例全部代码实现|学习笔记

开发者学堂课程【分布式协调系统 Zookeeper 快速入门:服务器节点动态上下线案例全部代码实现】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/97/detail/1597


服务器节点动态上下线案例全部代码实现


服务端代码相当于把服务器注册到集群中,有几个节点就有几个服务器

客户端代码

package com. atquiqu. zkcase;

import java. io. IOException;

import java.util. Arraylist;

import java.util.List;

import org. apache . zookeeper . Wa tchedEvent;

.import org. apache . zookeeper . Watcher;

import orq. apache . zookeeper . ZooKeeper;

public class Distribrteclient ​​

private​​     ​​static​​     ​​String​​     ​​connectString

"hadoop102: 2181, hadoop103:2181, hadoop104:2181";-private static int sessionTimeout = 2000;

private ZooKeeper zk = null;

private String parentNode = " servers";

main 方法中还要注意几步

DistributeClient client = new DistributeClient();

11 1 获取 zookeeper 集群连接

client. getConnect();l

// 2注册监听

client. getchlidren();

11 3 业务逻辑处理

private void getChlidren() throws KeeperException, InterruptedException (

List<string> children = zkClient. getChildren(" /servers", true);

//存储服务器节点主机名称集合

ArrayList<string> hosts = new ArrayList<>();

for (String child : children) (

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

hosts . add(new String(data))​​;

11 将所有在线主机名称打印到控制台

System.out.println(hosts);

上一篇:Flume Agent 内部原理|学习笔记


下一篇:jQuery_DOM 属性下|学习笔记