客户端操作
- bin/zkServer.sh start
开启zookeeper - bin/zkServer.sh status
查看状态 - ls /
查看根下的节点 - ls /zhiye
查看节点下的节点 - create /temp “one”
新建一个节点 - create /temp/ttemp “two”
在节点下新建一个节点 - get /temp
获取节点的信息。名称与时间等 - create -e /tem “one”
创建临时的节点。重启zkClient会消失 - create -s /app2/aa 888
创建带编号的节点,编号用来被客户端识别,推断信息 - delete /app1/bb
删除节点 - rmr /app2
递归删除节点 - set /app1 999
给节点设置值 - get /app1 watch
监听节点的路径变化。创建了子节点会通知。
监听只可以使用一次。api的应用
- 主要是回调函数中客户端的使用。此处用@before做模拟
可以在控制台查看节点情况
- 主要是回调函数中客户端的使用。此处用@before做模拟
package com.uu.zookeeper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* Created by Administrator on 2019/10/26.
*/
public class MyZooKeeper {
ZooKeeper zooKeeper;
@Before
public void main() throws Exception {
//设置客户端参数
String st = "hadoop101:2181,hadoop102:2181,hadoop103:2181";
int i = 1000;
//获取客户端
zooKeeper = new ZooKeeper(st, i, new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println("执行了回调方法");
try {
List<String> children = zooKeeper.getChildren("/", true);
System.out.println("有:"+children.size()+"个节点");
for (String child : children) {
System.out.println(child);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Test
public void test() throws Exception {
List<String> children = zooKeeper.getChildren("/", true);
System.out.println("有:"+children.size()+"个节点");
for (String child : children) {
System.out.println(child);
}
Thread.sleep(Long.MAX_VALUE);
}
}
模拟客户端的注册监控
客户端注册为临时节点,不可以创建子节点。此处开启了监控为了模拟监控的方法。
注册
创建子节点
监听
获取节点的信息。
package com.uu.zookeeper;
import org.apache.zookeeper.*;
import org.jboss.netty.channel.ChildChannelStateEvent;
import java.io.IOException;
import java.util.List;
/**
* Created by Administrator on 2019/10/27.
*/
public class Service {
String connectString = "hadoop101:2181,hadoop102:2181,hadoop103:2181";
int sessionTimeout = 2000;
ZooKeeper zooKeeper ;
//获取连接对象
public void getConnectionObj() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println("开始了监听:当前节点:"+watchedEvent.toString());
try {
List<String> children = zooKeeper.getChildren("/tmp", true);
for (String child : children) {
System.out.println(child);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
//登记信息,实际就是创建自己的节点在集群上面
public void register() throws Exception {
zooKeeper.create("/tmp","ont".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("上线啦");
}
public void jianTing() throws Exception {
zooKeeper.getChildren("/tmp",true);
}
public static void main(String[] args) throws Exception {
Service service = new Service();
//获取连接对象
service.getConnectionObj();
//登记
service.register();
//开启监听
service.jianTing();
Thread.sleep(Long.MAX_VALUE);
}
}