zookeeper客户端指令及实现服务器动态上下线功能

命令基本语法 功能描述
help 显示所有操作命令
ls path [watch] 使用 ls 命令来查看当前znode中所包含的内容
ls2 path [watch] 查看当前节点数据并能看到更新次数等数据
create 普通创建,-e 创建短暂节点,-s创建带序号的节点
-s 含有序列
-e 临时(重启或者超时消失)
get path [watch] 获得节点的值
set 设置节点的具体值
stat 查看节点状态
delete 删除节点
rmr 递归删除节点

package com.liang;

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;
/**
 * 服务器端向Zookeeper注册
 * @author Administrator
 *
 */
public class DistributeServer {
	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		DistributeServer server=new DistributeServer();
		
		//1、连接
		server.getConnection();
		//2、注册
		server.registServer(args[0]);
		//3、业务逻辑
		server.business(args[0]);
	}

	private void business(String host) throws InterruptedException {
		System.out.println(host+" is working......");
		Thread.sleep(Long.MAX_VALUE);
	}

	private void registServer(String host) throws KeeperException, InterruptedException {
		String path = zooKeeper.create("/servers/server", host.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println("****"+path);
	}
	

	private String connectString="192.168.56.10:2181";
	private int sessionTimeout=2000;
	private ZooKeeper zooKeeper;

	private void getConnection() throws IOException {
			zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			@Override
			public void process(WatchedEvent event) {
				
			}
		});
	}
}

package com.liang;

import java.io.IOException;
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;

/**
 * 客户端代码
 * @author Administrator
 *
 */
public class DistributeClient {
	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		DistributeClient client=new DistributeClient();
		//1、连接
		client.getConnection();
		//2、获取服务器列表信息
		client.getServerList();
		//3、业务进程启动
		client.business();
	}
	
	private void business() throws InterruptedException {
		System.out.println("client is working.......");
		Thread.sleep(Long.MAX_VALUE);
	}

	private void getServerList() throws KeeperException, InterruptedException {
		List<String> servers=new ArrayList<String>();
		// 获取服务器子节点信息,并且对父节点进行监听
		List<String> children = zooKeeper.getChildren("/servers", true,	null);
		for (String child : children) {
			//遍历所有节点,获取节点中的主机名称信息
			byte[] data = zooKeeper.getData("/servers/"+child, false, null);
			servers.add(new String(data));
		}
		//打印服务器列表信息
		System.out.println(servers);
	}

	private String connectString="192.168.56.10:2181";
	private int sessionTimeout=2000;
	private ZooKeeper zooKeeper;
	private void getConnection() throws IOException {
		zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			@Override
			public void process(WatchedEvent event) {
				// 再次启动监听
				try {
					System.out.println("****************");
					getServerList();
					System.out.println("****************");
				} catch (KeeperException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值