赞
踩
系列文章:
zookeeper快速入门一:zookeeper安装与启动-CSDN博客
zookeeper快速入门二:zookeeper基本概念-CSDN博客
先启动zookeeper服务端。
在maven引入zookeeper依赖。
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>zookeeper</artifactId> <version>3.3.1</version> </dependency>
org.apache.zookeeper.Zookeeper是客户端入口主类,负责建立与server的会话。它提供了以下 所示几类主要方法。
在java中启动客户端,注册一个watcher监听链接的建立。
- import org.apache.zookeeper.WatchedEvent;
- import org.apache.zookeeper.Watcher;
- import org.apache.zookeeper.ZooKeeper;
-
- public class ZookeeperClient {
- private static final String connectString = "127.0.0.1:2181";
- private static final int sessionTimeout = 2000;
-
- private static ZooKeeper zkClient = null;
-
- public void init() throws Exception {
- zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
- @Override
- public void process(WatchedEvent event) {
- // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
- System.out.println("zookeeper链接建立");
- }
- });
- }
- }
在main方法里测试我们的init方法,用Thread.sleep方法等待zookeeper连接创建,不然在zookeeper客户端建立连接之前主线程就已经退出。
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(5000);
- }
控制台输出:
接下来我们创建一个名为“/java”的节点,节点数据为“data”,ZooDefs.Ids.OPEN_ACL_UNSAFE的意思是不节点能被所有人访问,CreateMode.PERSISTENT:节点的类型为持久节点。
- public void createZnode()throws Exception{
- zkClient.create("/java", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
判断节点是否存在,false代表不注册监听事件,如果是true,则注册我们在new zookeeper方法里面传递的watcher。
- public void testExist() throws Exception{
- Stat stat = zkClient.exists("/java", false);
- System.out.println(stat==null?"节点不存在":"节点存在");
- }
测试一下我们的方法。
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(5000);
- new ZookeeperClient().createZnode();
- new ZookeeperClient().testExist();
- }
获取节点的数据。false和上面exists方法参数含义一样,表示不注册连接建立时的watcher,第三个stat对象则存储了除了节点数据之外的其他信息,如czxid、mzxid等。如果为null则表示不保存节点的这些信息。
- public void getNodeData()throws Exception{
- byte[] res = zkClient.getData("/java",false,new Stat());
- System.out.println(new String(res));
- }
同样测试我们的方法。
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(3000);
- new ZookeeperClient().getNodeData();
- }
获取ACL控制列表
- public void getACl()throws Exception{
- List<ACL> res = zkClient.getACL("/java",new Stat());
- for(ACL acl : res){
- System.out.println(acl.getId().toString()+acl.getPerms());
- }
- }
测试:
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(3000);
- new ZookeeperClient().getACl();
- }
在/java下创建子节点,获取子节点列表。
- public void createChildZnode()throws Exception{
- zkClient.create("/java/child", "child data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- zkClient.create("/java/child2", "child2 data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
- public void getChildNode()throws Exception{
- List<String> res = zkClient.getChildren("/java",false);
- for(String s : res){
- System.out.println(s);
- }
- }
测试:
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(3000);
- new ZookeeperClient().createChildZnode();
- new ZookeeperClient().getChildNode();
- }
全部代码如下:
- import org.apache.zookeeper.*;
- import org.apache.zookeeper.data.ACL;
- import org.apache.zookeeper.data.Stat;
-
- import java.util.List;
-
- public class ZookeeperClient {
- private static final String connectString = "127.0.0.1:2181";
- private static final int sessionTimeout = 2000;
-
- private static ZooKeeper zkClient = null;
-
- public void init() throws Exception {
- zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
- @Override
- public void process(WatchedEvent event) {
- // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
- System.out.println("zookeeper链接建立");
- }
- });
- }
- public void createZnode()throws Exception{
- zkClient.create("/java", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
- public void createChildZnode()throws Exception{
- zkClient.create("/java/child", "child data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- zkClient.create("/java/child2", "child2 data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
- }
- public void getChildNode()throws Exception{
- List<String> res = zkClient.getChildren("/java",false);
- for(String s : res){
- System.out.println(s);
- }
- }
- public void testExist() throws Exception{
- Stat stat = zkClient.exists("/java", false);
- System.out.println(stat==null?"节点不存在":"节点存在");
- }
- public void getNodeData()throws Exception{
- byte[] res = zkClient.getData("/java",false,new Stat());
- System.out.println(new String(res));
- }
-
- public void getACl()throws Exception{
- List<ACL> res = zkClient.getACL("/java",new Stat());
- for(ACL acl : res){
- System.out.println(acl.getId().toString()+acl.getPerms());
- }
- }
- public static void main(String[] args)throws Exception{
- new ZookeeperClient().init();
- Thread.sleep(3000);
- new ZookeeperClient().createChildZnode();
- new ZookeeperClient().getChildNode();
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。