码迷,mamicode.com
首页 > 其他好文 > 详细

Hadoop基础(三十六):监听服务器节点动态上下线案例

时间:2020-07-21 14:07:54      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:cli   system   服务器列表   loading   mode   err   案例   创建   long   

1.需求

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

2.需求分析,如图 5-12 所示

技术图片

 

3.具体实现

(0)先在集群上创建/servers 节点
[zk: localhost:2181(CONNECTED) 10] create /servers "servers"
Created /servers
(1)服务器端向 Zookeeper 注册代码
package com.atguigu.zkclient;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;

public class DistributeServer {
    private static final String CONNECTION_STRING = "192.168.1.122:2181,192.168.1.133:2181,192.168.1.144:2128";
    private static final int SESSION_TIMEOUT = 300000;
    private ZooKeeper zk;
    private String parentNode = "/servers";

    // 创建到 zk 的客户端连接
    public void getConnect() throws IOException {
        zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {

            @Override
            public void process(WatchedEvent evemt){
            }
        });
    }
    
    // 注册服务器
    public void registerServer(String hostname) throws Exception {
        String create = zk.create(parentNode+"/server", hostname.getBytes(), 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.registerServer(args[0]);

        // 3 启动业务功能
        server.business(args[0]);
    }
}
(2)客户端代码
package com.atguigu.zkclient;

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

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class DistributeClient {
    private static final String CONNECTION_STRING = "192.168.1.122:2181,192.168.1.133:2181,192.168.1.144:2128";
    private static final int SESSION_TIMEOUT = 300000;
    private ZooKeeper zk;
    private String parentNode = "/servers";

    // 创建到 zk 的客户端连接
    public void getConnect() throws IOException {
        zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {
            public void process(WatchedEvent event){
                try {
                    getServerList();
                } catch (Exception e) {
                    //TODO: handle exception
                    e.printStackTrace();
                }
            }
        });
    }

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

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

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

        // 4 打印服务器列表信息
        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();
    }
}

 

 

Hadoop基础(三十六):监听服务器节点动态上下线案例

标签:cli   system   服务器列表   loading   mode   err   案例   创建   long   

原文地址:https://www.cnblogs.com/qiu-hua/p/13352518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!