1. 程式人生 > >zookeeper動態上下線推送

zookeeper動態上下線推送

package com.gcx.zookeeper2;

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

public class DistributedServer {
	private static final String connectString ="192.168.48.156:2181,192.168.53.141:2181,192.168.48.154:2181";
	private static final int sessionTimeout=60000;
	private static final String parentNode = "/servers";

	private ZooKeeper zk = null;

	/**
	 * 建立到zk的客戶端連線
	 * 
	 * @throws Exception
	 */
	public void getConnect() throws Exception {

		zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				// 收到事件通知後的回撥函式(應該是我們自己的事件處理邏輯)
				System.out.println(event.getType() + "---" + event.getPath());
				try {
					zk.getChildren("/", true);
				} catch (Exception e) {
				}
			}
		});

	}

	/**
	 * 向zk叢集註冊伺服器資訊
	 * 
	 * @param hostname
	 * @throws Exception
	 */
	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);

	}

	/**
	 * 業務功能
	 * 
	 * @throws InterruptedException
	 */
	public void handleBussiness(String hostname) throws InterruptedException {
		System.out.println(hostname + "start working.....");
		Thread.sleep(Long.MAX_VALUE);
	}

	public static void main(String[] args) throws Exception {

		// 獲取zk連線
		DistributedServer server = new DistributedServer();
		server.getConnect();

		// 利用zk連線註冊伺服器資訊
		server.registerServer(args[0]);

		// 啟動業務功能
		server.handleBussiness(args[0]);

	}

}

package com.gcx.zookeeper2;

import java.util.ArrayList;
import java.util.List;

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

public class DistributedClient {

	private static final String connectString ="192.168.48.156:2181,192.168.53.141:2181,192.168.48.154:2181";
	private static final int sessionTimeout=60000;
	private static final String parentNode = "/servers";
	// 注意:加volatile的意義何在?
	private volatile List<String> serverList;
	private ZooKeeper zk = null;

	/**
	 * 建立到zk的客戶端連線
	 * 
	 * @throws Exception
	 */
	public void getConnect() throws Exception {

		zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				// 收到事件通知後的回撥函式(應該是我們自己的事件處理邏輯)
				try {
					//重新更新伺服器列表,並且註冊了監聽
					getServerList();

				} catch (Exception e) {
				}
			}
		});

	}

	/**
	 * 獲取伺服器資訊列表
	 * 
	 * @throws Exception
	 */
	public void getServerList() throws Exception {

		// 獲取伺服器子節點資訊,並且對父節點進行監聽
		List<String> children = zk.getChildren(parentNode, true);

		// 先建立一個區域性的list來存伺服器資訊
		List<String> servers = new ArrayList<String>();
		for (String child : children) {
			// child只是子節點的節點名
			byte[] data = zk.getData(parentNode + "/" + child, false, null);
			servers.add(new String(data));
		}
		// 把servers賦值給成員變數serverList,已提供給各業務執行緒使用
		serverList = servers;
		
		//列印伺服器列表
		System.out.println(serverList);
		
	}

	/**
	 * 業務功能
	 * 
	 * @throws InterruptedException
	 */
	public void handleBussiness() throws InterruptedException {
		System.out.println("client start working.....");
		Thread.sleep(Long.MAX_VALUE);
	}
	
	
	
	
	public static void main(String[] args) throws Exception {

		// 獲取zk連線
		DistributedClient client = new DistributedClient();
		client.getConnect();
		// 獲取servers的子節點資訊(並監聽),從中獲取伺服器資訊列表
		client.getServerList();

		// 業務執行緒啟動
		client.handleBussiness();
		
	}

}