1. 程式人生 > 其它 >監聽伺服器節點動態上下線案例分析

監聽伺服器節點動態上下線案例分析

1.需求

某分散式系統中,主節點可以有多臺,可以動態上下線,任意一臺客戶端都能實時感知到主節點伺服器的上下線。

2.需求分析,如圖所示

3.具體實現

(0)先在叢集上建立/servers節點

[zk: localhost:2181(CONNECTED) 10] create /servers “servers”

Created /servers

(1)伺服器端向Zookeeper註冊程式碼

package com.atguigu.zkcase;

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 String connectString = “hadoop102:2181,hadoop103:2181,hadoop104:2181”;

private static int sessionTimeout = 2000;

private ZooKeeper zk = null;

private String parentNode = “/servers”;

// 建立到zk的客戶端連線

public void getConnect() throws IOException{

zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

@Override

public void process(WatchedEvent event) {

}

});

}

// 註冊伺服器

public void registServer(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.registServer(args[0]);

// 3 啟動業務功能

server.business(args[0]);

}

}

(2)客戶端程式碼

package com.atguigu.zkcase;

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 String connectString = “hadoop102:2181,hadoop103:2181,hadoop104:2181”;

private static int sessionTimeout = 2000;

private ZooKeeper zk = null;

private String parentNode = “/servers”;

// 建立到zk的客戶端連線

public void getConnect() throws IOException {

zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

@Override

public void process(WatchedEvent event) {

// 再次啟動監聽

try {

getServerList();

} catch (Exception e) {

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

}

}

瞭解大資料培訓開發技術知識,關注我,有更多精彩內容與您分享!

文章轉載連結:http://www.atguigu.com/jsfx/13029.html