Zookeeper的调用-javaApi

32 篇文章 0 订阅
28 篇文章 0 订阅

使用Java API调用Zookeeper

    一,概述:

        我们知道可以通过命令来操作Zookeper的客户端和服务端并进行相应的操作,但是有时候需要使用java在程序中对其进行操作.
即,通过API(JAVA)来操作Zookeeper.

    二,依赖:

        
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>

    三,操作:

        首先我们需要如下的基本信息:
            
    //zookeeper
    public static String APPLICATION_MASTER_NAME = "realtime-master";
    public static String APPLICATION_SLAVE_NAME_PREFIX = "realtime-slave-";
    public static String HA_ROOT_PATH = "/realtimeApp/ha";

    private ZooKeeper zk;
    private static final int SESSION_TIMEOUT = 5000;

    public ZkService(String host) throws IOException, InterruptedException {
        zk = new ZooKeeper(host, SESSION_TIMEOUT, (watchedEvent) -> {});
    }

        1,创建节点:

            
	//创建rootNode
    public void createRootNode() throws KeeperException, InterruptedException {
        if (!isExists("/realtimeApp")) {
            zk.create("/realtimeApp", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (!isExists(Constants.HA_ROOT_PATH)) {
            zk.create(Constants.HA_ROOT_PATH, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        LOG.info("Success to create root Node : " + Constants.HA_ROOT_PATH);
    }

    //创建masterNode
        public void addMasterNode(String rootPath, String masterName) throws KeeperException, InterruptedException {
        String path = rootPath + "/" + masterName;
        String createPath = zk.create(path, masterName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        LOG.info("Success to create Master ephemeral Node : " + createPath);

    }

    //创建slaveNode
        public void addSlaveNode(String rootPath, String slaveName) throws KeeperException, InterruptedException {
        String path = rootPath + "/" + slaveName;
        String createPath = zk.create(path, slaveName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        LOG.info("Success to create Slave ephemeral Node : " + createPath);
    }

        2,删除节点:

        
	//删除单个节点:
    public void deleteNode(String nodePath) throws KeeperException, InterruptedException {
        zk.delete(nodePath, -1);
    }

    //删除所有节点:
    public void deleteAllSlaves(String groupName) throws Exception {
        String path = "/" + groupName;
        try {
            List<String> nodes = zk.getChildren(path, false);
            for (String node : nodes) {
                zk.delete(path + "/" + node, -1);
            }
            zk.delete(path, -1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    

        3,获取节点:

            
    public void listNodes(String path) throws KeeperException, InterruptedException {
        List<String> nodes = zk.getChildren(path, false);
        if (nodes.isEmpty())
            LOG.info("path : " + path + " no any child node.");
        for (String node : nodes) {
            LOG.info(node);
        }

    }

        4,判断某个节点是否存在:

            
    public boolean isExists(String path) throws KeeperException, InterruptedException {
        Stat stat = zk.exists(path, false);
        return stat == null ? false : true;
    }    

        5,判断slave节点是否在线:

            
    public boolean hadSlaveOnline(String rootPath) throws KeeperException, InterruptedException {
        List<String> nodes = zk.getChildren(rootPath, false);
        if (nodes == null || nodes.size() == 0) {
            return false;
        }
        for (String node : nodes) {
            if (node.startsWith(Constants.APPLICATION_SLAVE_NAME_PREFIX)) {
                LOG.info("Find an slave {} online.", node);
                return true;
            }
        }
        return false;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值