Zookeeper 监控服务上下线

package nue.edu.ls;

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;

/**
 * 服务端
 * 1:建立链接
 * 2:注册
 * 3:server端的业务处理
 * @author root
 *
 */
public class DistributedServer {
	private static  final String connectString = "jiqun01:2181,jiqun02:2181,jiqun03:2181";
	private static final int sessionTimeout = 2000;
	private static final String parentNode = "/servers";
	ZooKeeper zkClient = null;
	
	/**
	 * 获取zk连接
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			@Override
			public void process(WatchedEvent event) {
				//收到事件通知后调用的回调函数
				System.out.println(event.getPath()+""+event.getType());
				try {
					zkClient.getChildren("/", true);
				} catch (KeeperException | InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 向zk注册服务器的信息
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	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 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链接向zk进行注册
		server.registerServer("alibaba");
		// 启动业务功能
		server.handleBussiness("alibaba");
	}

}

 

package nue.edu.ls;

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;

/**
 * 服务端
 * 1:建立链接
 * 2:注册
 * 3:server端的业务处理
 * @author root
 *
 */
public class DistributedServer {
	private static  final String connectString = "jiqun01:2181,jiqun02:2181,jiqun03:2181";
	private static final int sessionTimeout = 2000;
	private static final String parentNode = "/servers";
	ZooKeeper zkClient = null;
	
	/**
	 * 获取zk连接
	 * @throws Exception
	 */
	public void getConnect() throws Exception{
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			@Override
			public void process(WatchedEvent event) {
				//收到事件通知后调用的回调函数
				System.out.println(event.getPath()+""+event.getType());
				try {
					zkClient.getChildren("/", true);
				} catch (KeeperException | InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * 向zk注册服务器的信息
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	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 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链接向zk进行注册
		server.registerServer("alibaba");
		// 启动业务功能
		server.handleBussiness("alibaba");
	}

}

pom.xml


	4.0.0
	nuc.edu
	Zookeeper
	0.0.1-SNAPSHOT
	
	
		
			org.apache.zookeeper
			zookeeper
			3.4.6
		
		
		
		
			org.slf4j
			slf4j-api
			1.7.25
		
		
	
    

    log4j
    log4j
    1.2.17

    
	

结果:

Zookeeper 监控服务上下线_第1张图片

Zookeeper 监控服务上下线_第2张图片

关闭服务端后:

Zookeeper 监控服务上下线_第3张图片

 

你可能感兴趣的:(Zookeeper,大数据,一年后毕业,应届生,找工作,Java,HaDoop学习历程)