zookeeper实战
一、zookeeper客户端命令
1、zookeeper命令语法
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path | 使用ls命令来查看当前znode的子节点【可监听】 -w 监听子节点变化,只能监听一次 -s 附加次级信息 |
create | 普通创建 -s 含有序号 -e 临时(重启或者超时消失) |
get path | 获取节点的值【可监听】-w 监听节点内容变化,只能监听一次 -s 附加次级信息 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
deleteall | 递归删除节点 |
2、zookeeper的实操
进入zookeeper的客户端模式(bin/zkCli.sh)
1、查看/里面的数据
ls /zookeeper
ls -w /zkeeper # 监听这个zkeeper节点
2、创建一个znode节点
create /a # 创建一个持久的znode节点a
create -s /a/b # 创建一个持久的带序号的znode节点 输出b1
注意:
1)临时节点不能创建目录,临时节点不能有孩子,临时节点只能作为子节点
2)当创建一个临时节点,重新启动,在创建一个持久序号节点,会从01开始创建这个持久序号节点
3、查看节点的值
get /a/b # 查看节点中的数据
4、查看节点下的目录(子节点)
set /a
5、delete 删除子节点,但是不能递归删除
delete /a/b
6、deleteall
deleleteall /a #删除所有的/a下所有的子节点
二、zookeeper的API操作
1、只用一个main进行运行
package Zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class zookeeper {
@Test
public void testZookper() throws IOException, InterruptedException {
//三个参数
// 1、connectString 指定zookeeper集群位置
// 2、seessionTimeout 客户端的超时时间
// 3、Watcher 默认的监听对象
String connectString = "hadoop:2181,hadoop1:2181,hadoop:2181";
// minSessionTimeout=4000毫秒<=指定的sessionTimeout<=40000毫秒
int sessionTimeout = 10000;
ZooKeeper zookeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// 对时间处理的过程
}
});
System.out.println("zookeeper=" + zookeeper);
zookeeper.close();
}
}
2、使用一个个测试单元进行运行
(一)创建数据
package Zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* 客户端性质开发
* 1、创建客户端对象
* 2、调用API方法
* 3、关闭
*/
public class zookeeper {
// 定义
private String connectString = "hadoop:2181,hadoop1:2181,hadoop:2181";
// minSessionTimeout=4000毫秒<=指定的sessionTimeout<=40000毫秒
private int sessionTimeout = 40000;
ZooKeeper zookeeper;
@Before
public void init() throws IOException {
zookeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
}
// 创建数据
@Test
public void testCreate() throws InterruptedException, KeeperException {
String s=zookeeper.create("/a/b","kuai".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// CreateMode.PERSISTENT表示节点的类型
System.out.println("s="+s);//转换为byte数组
}
@After
public void close() throws InterruptedException {
zookeeper.close();
}
}
(二)获取子节点
查看某个目录下的子节点的信息
@Test
public void testReadZnode() throws InterruptedException, KeeperException {
List<String> children=zookeeper.getChildren("/a", new Watcher() {
// 重写事件监听的方法
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("监听到节点发生了变化");
}
});
System.out.println("childern="+children);
// 保持方法不结束
Thread.sleep(Long.MAX_VALUE);
}
(三)获取子节点的数据
// 获取节点的内容
@Test
public void testGetData() throws InterruptedException, KeeperException {
byte[] data=zookeeper.getData("/a/c",false,getStat("/a/c"));
System.out.println("data="+new String(data));
}// 获取/a/c这个目录下的数据
public Stat getStat(String path) throws InterruptedException, KeeperException {
return zookeeper.exists(path,false);
}
(四)设置节点的数据
// 设置节点内容
@Test
public void setData() throws InterruptedException, KeeperException {
zookeeper.setData("/a/b","dashuju".getBytes(),getStat("/a/b").getVersion());
//version 需要传入版本号,在不知道版本号的时候可以输入-1,表示忽略版本号
// 如果具体的值,跟实际的DataVersion不一致,那么会报错
// verison可以改为getStat("/a/b).getVersion()
}
(五)查看子节点是否存在
// 判断节点是否存在
@Test
public void testExists() throws InterruptedException, KeeperException {
Stat stat=zookeeper.exists("/a",false);
System.out.println("stat="+stat);
}
(六)删除子节点
// 删除数据
@Test
public void testDelete() throws InterruptedException, KeeperException {
zookeeper.delete("/a/b",-1);
}
三、zookeeper设计服务端和客户端
服务端代码 Server.java
package Zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class Server {
private String connectString ="hadoop:2181,hadoop1:2181,hadoop2:2181";
private int setTime=10000; // 设置的时间要在4秒到40秒之间
private ZooKeeper zookeeper;
private String ServerZnodes="/server";
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
Server server=new Server();
//1、 初始化一个zookeeper连接
server.initCommand();
server.regist(args[0]);
//2、在zookeeper里面创建一个临时节点
Thread.sleep(Long.MAX_VALUE);
//3、保持连接状态
}
public void regist(String data) throws InterruptedException, KeeperException {
Stat stat= zookeeper.exists(ServerZnodes,false);
// 判断这个节点是否存在,不存在就创建一个
if (stat==null){
zookeeper.create(ServerZnodes,"server".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
String s = zookeeper.create(ServerZnodes + "/server", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(s+" IS Online!!!!!");
}
public void initCommand() throws IOException {
zookeeper=new ZooKeeper(connectString, setTime, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// 重写监视的方法
}
});
}
}
需要在IDEA中自己传入参数进行设计参数
服务端代码 Client.java
package Zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.List;
public class Client {
private String connectString = "hadoop:2181,hadoop1:2181,hadoop2:2181";
private int setTime = 10000; // 设置的时间要在4秒到40秒之间
private ZooKeeper zookeeper;
private String ServerZnodes = "/server";
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
//1、初始化客户端连接
Client client = new Client();
client.initCommand();
// 2、获取在线的服务器列表
client.ListAllServer();
//3、保持连接
Thread.sleep(Long.MAX_VALUE);
}
public void ListAllServer() throws InterruptedException, KeeperException {
List<String> children = zookeeper.getChildren(ServerZnodes, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
ListAllServer();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}
});
System.out.println("当前在线服务器的为"+children);
}
public void initCommand() throws IOException {
zookeeper = new ZooKeeper(connectString, setTime, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// 重写监视的方法
}
});
}
}
标签:zookeeper,void,Zookeeper,public,API,org,import,节点,客户端
From: https://www.cnblogs.com/zt123456/p/16903338.html