オフラインダイナミックセンシング分散型アプリケーションプログラム開発システムのサーバ4 _

まず、機能

     サーバーの数の1動的な変更

     2.クライアントは、動的にオンラインサーバーを監視することができます

     3.具体的なアイデア:

               飼育係の登録情報パーマネント・ノードを作成しない、一時的なノードの登録に、注意を払うを(作成)(サーバが一時的にノードが消えたときので、ハングアップ)上の(1)サーバーが起動します

               (2)クライアント---- GetChildrenメソッド()、取得したノード情報、およびサーバーをオフライン、情報へのモニターへのアクセスがあった場合には、リスナーを登録した後、サーバーのリストを取得し、リスナーを登録します。

第二に、特定のコード

1.クライアントコード

package cn.itcast.bigdata.zkDis;

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 DistributeClient {
	private ZooKeeper zkClient = null;
	private static final String connectString="10.177.21.1:2181,10.177.21.2:2181,10.177.21.4:2181";
	private static final int sessionTimeout = 50000;
	private static final String parentNode="/servers";
	//volatile:保证多线程中数据一致
	private volatile List<String> serverList;
	/**
	 * 
	 * 获取zk服务器集群连接
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			//客户端可以根据zookeeper的通知作出自己的反应
			public void process(WatchedEvent event) {
				
				//重新更新服务器列表,并注册监听
				try {
					getServerList();
				} catch (Exception e) {
					
				}
			}
		});
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
	}
	
	/**
	 * 获取服务器信息列表
	 */
	
	public void getServerList() throws KeeperException, InterruptedException{
		//获取服务器子节点信息,并对父节点监听
		List<String> children = zkClient.getChildren(parentNode, true);
		List<String> servers = new ArrayList<String>();
		for(String child:children){
			//child只是子节点名
			byte[] data = zkClient.getData(parentNode+"/"+child, false, null);
			servers.add(new String(data));
		}
		serverList = servers;
		//打印一下服务器列表
		System.out.println(serverList);
	}
	
	/**
	 * 业务功能
	 * @throws InterruptedException 
	 */
	public void handleBusiness() throws InterruptedException{
		System.out.println("client is working...");
		Thread.sleep(Long.MAX_VALUE);
	}
	
	
	public static void main(String[] args) throws Exception {
		
		//获取链接
		DistributeClient client =new DistributeClient();
		client.getConnect();
		
		//获取server的子节点信息(并监听),从中获取服务器信息列表
		client.getServerList();
		//业务线程启动
		client.handleBusiness();
		
		
	}
	
}

2.サーバー

package cn.itcast.bigdata.zkDis;

import java.io.IOException;

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

/**
 * 
 * 分布式应用系统服务器上下线动态感知程序开发_服务端
 * 
 * @author Administrator
 *
 */



public class DistributedServer {
	
	private ZooKeeper zkClient = null;
	private static final String connectString="10.177.21.1:2181,10.177.21.2:2181,10.177.21.4:2181";
	private static final int sessionTimeout = 50000;
	private static final String parentNode="/servers";
	/**
	 * 
	 * 获取zk服务器集群连接
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			//客户端可以根据zookeeper的通知作出自己的反应
			public void process(WatchedEvent event) {
				//收到事件通知后的回调函数
				System.out.println(event.getType()+ "----"+event.getPath());
				//因为监听器只监听一次,所以在此调用重新new一个监听器
				try {
					zkClient.getChildren("/", true);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		});
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
		Thread.sleep(5000);
		System.out.println(zkClient.getState());
	}
	
	/**
	 * 向zk服务器注册信息
	 * @param hostname
	 */
	
	public void registerServer(String hostname) throws KeeperException, InterruptedException{
		String create = zkClient.create(parentNode+"/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println(hostname+"is online.."+create);
	}
	
	/**
	 * 业务功能
	 * @throws InterruptedException 
	 */
	public void handleBusiness(String hostname) throws InterruptedException{
		System.out.println(hostname+"is 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.handleBusiness(args[0]);
	}
	
	
}

 

リリース7件のオリジナルの記事 ウォンの賞賛0 ビュー27

おすすめ

転載: blog.csdn.net/m0_38013741/article/details/89072441