1. 程式人生 > 實用技巧 >Hadoop基礎(三十六):監聽伺服器節點動態上下線案例

Hadoop基礎(三十六):監聽伺服器節點動態上下線案例

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();
    }
}