ZooKeeper
**启动**
./ZKServer start stop status 启动 关闭 状态
./ZKCli.sh -server linux201:2181,linux202:2181,linux203:2181 shell客户端连接ZK
**命令**
ls / 查看/下信息
get /a 得到/a的值
set /a 设置/a的值
delete /a 删除空节点
rmr /a 删除节点(空或不空)
create -s -e /hh hh 创建/hh有序临时节点 -s(有序) -e(临时)
临时节点在客户端断开时,会自动消失
watch 监控
ls / watch 当子节点发生变化,会马上告诉当前客户端(一次)
get /a watch 当/a节点的数据发生变化, 会马上告诉当前客户端(一次)
**java连接ZooKeeper**
ZooKeeper zk = new ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
connectString 连接的字符串"linux201:2181" sessionTimeout: 超时时间 watcher:监听器
Zk.getChildren("/", null)
获取指定节点下的所有子节点
参数1 路径节点名 以/开始
参数2 是否监控器(new Watcher{},null)
**创建新的节点**
zk.create("/teacher", "胡冠雨".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
参数1 节点的名称 /开头
参数2 节点的值 字节数组 ZK底层的值是字节
参数3 权限
参数4 节点的类型
EPHEMERAL 临时的 session级别的临时 客户端断开以后 节点会被删除
EPHEMERA_SEQUENTIAL 临时有序
PERSISTENT 永久
PERSISIENT_SEQUENTIAL 永久有序
**删除节点 只能删除空节点**
zk.delete("/teacher", -1)
参数1 节点名称
参数2 版本号 (-1为不知道)
**删除节点 递归删除节点**
rmr(String path){
//遍历节点 是否存在子节点 参数1 节点路径 参数2 是否监控
list<String> list = zk.getChildren(path, null);
//如果存在 调用自己删除
if(list != null&& list.size()>0){
for(String node :list){
rmr(path+"/"+node);
}
}
zk.delete(path,-1) 删除节点
参数1:节点路径 参数2:版本号
**获取节点的值** zk.getData("/", null, null)
参数1 节点路径
参数2 监控器
参数3 状态
**设置节点值** zk.setData("/", "abc".getBytes(), -1)
参数1 节点路径
参数2 节点值.getBytes()
参数3 版本号
**获取ZK客户端对象**
new ZooKeeper("linux201:2181,linux202:2181,linux203:2181", 2000, new Wather(){ })
未对节点做真正的监控啊
参数1 ZooKeeper集群的位置
参数2 超时时间
参数3 连接成功以后的监控 初始化的工作
**监控某个节点的子节点变化**
参数1 监控的节点
参数2 当前节点的子节点个数发生变化时会调用process 做监控逻辑
zk.getChildren("/", new Watcher(){
//只监控一次
process(WatchedEvent event){//获取监控事件的类型和监控的路径
syso(event.getPath()+"发生了"+ event.getType())
//再次注册
try zk.getChildren("/", this) 参数1 节点路径 参数2: watcher
}
});
Thread.sleep(Integer.MAX_VALUE);
**监控获取子节点变化时data数据**
zk.getChildren("/", new Watcher(){
process(WatchedEvent event){
List children = zk.getChildren("/", this)
for(String child: children){//参数1 节点路径 参数2 监控 参数3 状态
syso(zk.getData("/"+child, null, null))
}
})
**监控节点数据的变化**
zk.getData("/servers", new Wather(){ //参数1 节点 参数2 监听 参数3 状态
process(WatchedEvent event){//节点数据发生变化会执行一次
byte[] data1 = zk.getData("/servers", this, null)//再次注册
}
},null) Thread.sleep(Integer.MAX_VALUE)
**服务器上下线动态感知案例**
启动以后注册到ZK集群
start(){new Zookeeper("linux201:2181", 2000, null)}
register(String hostname){zk.create("/datanodes/datanode", hostname.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMER_SEQUENTIAL)}
service(String hostname){sout(hostname+"节点开始工作");Thread.sleep(Integer.MAX_VALUE)}
main(String[]args){
if args.length==1 hostname = args[0] dn = new DataNode()
dn.start(hostname); dn.register(hostname); dn.service(hostname);
}
监控节点下的所有子节点
start(){new Zookeeper("linux201:2181", 2000, new Watcher{ syso("namenode上线了")})}
listDatanode(){
zk.getChildren("/", new Watcher(){
process(WatchedEvent event){
zk.getChildren("/datanodes", this)
list ls = new ArrayList()
for(String child:children){
byte[] data = zk.getData("/datanodes/"+child, null, null)
String hosts = new String(data) ls.add(hosts) sout(ls)
}} }) }
service(){sout("namenode在服务......");Thread.sleep(Integer.MAX_VALUE)}
main() nn=new NameNode() nn.start() nn.listDatanode() nn.service()