zookeeper的简单使用

zookeeper的数据结构为树形目录结构



每个节点称为znode

znode有两种类型:

临时(ephemeral)断开连接自动删除 没有子节点 

持久(persistent)断开连接不会删除

Znode有四种形式的目录节点(默认是persistent )

PERSISTENT

PERSISTENT_SEQUENTIAL(持久序列 从0开始)

EPHEMERAL

EPHEMERAL_SEQUENTIAL

一、命令行客户端

1,启动客户端

    连接本地

bin/zkCli.sh

    连接远端  -server host:port

bin/zkCli.sh -server vm2:2181

    跳转  connect host:port

[zk: vm2:2181(CONNECTED) 0] connect vm1:2181

2,查看  ls path

[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]

3,创建  create [-e] [-s] path data

[zk: localhost:2181(CONNECTED) 1] create /znode "this is znode"
Created /znode
[zk: localhost:2181(CONNECTED) 2] ls /
[znode, zookeeper]

    命令行客户端写入数据时遇到空格就结束,

4,读取数据  get path

[zk: localhost:2181(CONNECTED) 3] get /znode
"this                                                        数据内容
cZxid = 0x100000012                              创建事务号
ctime = Fri Mar 23 04:52:03 CST 2018 创建时间
mZxid = 0x100000012                  修改事务号
mtime = Fri Mar 23 04:52:03 CST 2018 修改时间
pZxid = 0x100000012                  持久化事务号
cversion = 0                         创建版本号
dataVersion = 0                      数据版本号
aclVersion = 0                       权限版本号
ephemeralOwner = 0x0          临时节点id          
dataLength = 5                       数据长度
numChildren = 0                      子节点数

5、修改数据   set path data

[zk: localhost:2181(CONNECTED) 4] set /znode setData
[zk: localhost:2181(CONNECTED) 5] get /znode    
setData

6、创建监听 (每个监听仅一次有效)

    监听节点数据变化 get path watch

[zk: localhost:2181(CONNECTED) 7] get /znode watch
其他客户端对znode修改时本客户端收到通知
[zk: localhost:2181(CONNECTED) 8] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/znode

    监听子节点变化  ls path watch

[zk: localhost:2181(CONNECTED) 8] ls /znode watch
[]
[zk: localhost:2181(CONNECTED) 9] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/znode

7、退出 quit

二、java客户端

package zk;
import org.apache.zookeeper.CreateMode;
import java.util.List;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

public class ZkClient {
	private static final String connectString = "vm1:2181,vm2:2181,vm3:2181";
	private static final int sessionTimeout = 2000;
	ZooKeeper zkClient = null;

	@Before
	public void init() throws Exception {
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			
			@Override
			public void process(WatchedEvent event) {
				// TODO Auto-generated method stub
				// 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
				System.out.println(event.getType() + "---" + event.getPath());
				try {
					zkClient.getChildren("/", true);
				} catch (Exception e) {
				}
			}
		});

	}


	// 创建数据节点到zk中
	public void testCreate() throws KeeperException, InterruptedException {
		// 参数1:要创建的节点的路径 参数2:节点大数据 参数3:节点的权限 参数4:节点的类型
		String nodeCreated = zkClient.create("/eclipse", "hellozk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		//上传的数据可以是任何类型,但都要转成byte[]
	}

	//判断znode是否存在
	@Test	
	public void testExist() throws Exception{
		Stat stat = zkClient.exists("/eclipse", false);
		System.out.println(stat==null?"not exist":"exist");
	}
	
	// 获取子节点
	@Test
	public void getChildren() throws Exception {
		List<String> children = zkClient.getChildren("/", true);
		for (String child : children) {
			System.out.println(child);
		}
		Thread.sleep(Long.MAX_VALUE);
	}

	//获取znode的数据
	@Test
	public void getData() throws Exception {
		byte[] data = zkClient.getData("/eclipse", false, null);
		System.out.println(new String(data));
	}
	
	//删除znode
	@Test
	public void deleteZnode() throws Exception {
		//参数2:指定要删除的版本,-1表示删除所有版本
		zkClient.delete("/eclipse", -1);
	}
	//更新znode的数据
	@Test
	public void setData() throws Exception {
		zkClient.setData("/app1", "imissyou angelababy".getBytes(), -1);
		byte[] data = zkClient.getData("/app1", false, null);
		System.out.println(new String(data));
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值