4.分布式应用系统服务器上下线动态感知程序开发_

一、功能

     1.服务器数量动态变化

     2.客户端可以动态监测在线服务器

     3.具体思路:

               (1)服务器一启动就到zookeeper上去注册信息(create ),注意注册时用临时节点,不要用永久节点创建 (因为当服务器挂掉的时候,临时节点也会消失)

               (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