zookeeper的客户端与api操作

客户端操作

  • 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做模拟
      可以在控制台查看节点情况
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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值