zookeeper-客户端命令和API应用

客户端命令行操作

命令基本语法

功能描述

help

显示所有操作命令

ls path [watch]

使用 ls 命令来查看当前znode中所包含的内容

ls2 path [watch]

查看当前节点数据并能看到更新次数等数据

create

普通创建

-s  含有序列

-e  临时(重启或者超时消失)

get path [watch]

获得节点的值

set

设置节点的具体值

stat

查看节点状态

delete

删除节点

rmr

递归删除节点

启动客户端

bin/zkCli.sh

 API应用

Eclipse环境搭建

1.创建一个Maven工程

2.添加pom文件


		
			junit
			junit
			RELEASE
		
		
			org.apache.logging.log4j
			log4j-core
			2.8.2
		
		
		
			org.apache.zookeeper
			zookeeper
			3.4.10
		

3.需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。

log4j.rootLogger=INFO, stdout  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
log4j.appender.logfile=org.apache.log4j.FileAppender  
log4j.appender.logfile.File=target/spring.log  
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

创建ZooKeeper客户端

	private String connectString = "hadoop101:2181,hadoop102:2181,hadoop103:2181";
	private int sessionTimeout = 2000;
	private ZooKeeper zkClient;

	//创建ZooKeeper客户端
	@Before
	public void init() throws IOException {
		
		zkClient = new ZooKeeper(connectString, sessionTimeout ,new Watcher() {
			
			public void process(WatchedEvent event) {
				
				// 收到事件通知后的回调函数(用户的业务逻辑)
				System.out.println(event.getType() + "--" + event.getPath());

				// 再次启动监听
				try {
					zkClient.getChildren("/", true);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}

创建子节点

//1 创建节点
	@Test
	public void createNode() throws KeeperException, InterruptedException {
		
		zkClient.create("/liun", "1221".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
	}

获取子节点并监听节点变化

//2 获取子节点 并监控数据的变化
	@Test
	public void getDataAndWatch() throws KeeperException, InterruptedException {
		
		List children = zkClient.getChildren("/", true);
		for (String child : children) {
			System.out.println(child);
		}
		// 延时阻塞
		Thread.sleep(Long.MAX_VALUE);

	}

判断Znode是否存在

// 3 判断节点是否存在
	@Test
	public void exist() throws KeeperException, InterruptedException {
		
		Stat stat = zkClient.exists("/liun", false);
		
		System.out.println(stat==null? "not exist":"exsit");
	}

监听服务器节点动态上下线案例

需求

某分布式系统中,主节点可以有多台,可以动态上下线,任意一台客户端都能实时感知到主节点服务器的上下线。

先在集群上创建/servers节点

create /servers "servers"

(1)服务器端向Zookeeper注册代码

package com.liu.zookeeper;

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;

public class DistributeServer {
	
	private String connectString = "hadoop101:2181,hadoop102:2181,hadoop103:2181";
	private int sessionTimeout = 2000;
	private ZooKeeper zkClient;

	public static void main(String[] args) throws Exception {
		
		DistributeServer server = new DistributeServer();
		
		// 1 连接zookeeper集群
		server.getConnect();
		
		// 2 注册节点
		server.regist(args[0]);
		
		// 3 业务逻辑处理
		server.business(args[0]);
	}

	//业务功能
	private void business(String hostname) throws Exception {

		System.out.println(hostname +"is working");
		Thread.sleep(Long.MAX_VALUE);
	}

	// 注册服务器
	private void regist(String hostname) throws KeeperException, InterruptedException {
		
		String path = zkClient.create("/servers/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
		System.out.println(hostname + "is online");
	}

	// 创建到zk的客户端连接
	private void getConnect() throws IOException{
		
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			public void process(WatchedEvent event) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}

(2)客户端代码

package com.liu.zookeeper;

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;

public class DistributeClient {
	
	private String connectString = "hadoop101:2181,hadoop102:2181,hadoop103:2181";
	private int sessionTimeout = 2000;
	private ZooKeeper zkClient;

	public static void main(String[] args) throws Exception {
		
		DistributeClient client = new DistributeClient();
		
		// 1获取zookeeper集群连接
		client.getConnect();
		
		// 2注册监听
		client.getChildren();
		
		// 3业务逻辑处理
		client.business();
	}

	private void business() throws Exception {
		
		Thread.sleep(Long.MAX_VALUE);
	}

	private void getChildren() throws KeeperException, InterruptedException {
		
		List children = zkClient.getChildren("/servers", true);
		
		//存储服务器节点主机名称集合
		ArrayList hosts = new ArrayList<>();
		
		for (String child : children) {
			
			byte[] data = zkClient.getData("/servers"+child, false, null);
			
			hosts.add(new String(data));
		}
		
		// 将所有在线主机名称打印到控制台
		System.out.println(hosts);
	}

	private void getConnect() throws IOException {
		
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			public void process(WatchedEvent event) {
				
				try {
					getChildren();
				} catch (KeeperException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
}

 

 

 

 

 

你可能感兴趣的:(笔记-zookeeper)