zookeeper与java交互小结实现示例

TheDisguiser 2020-07-14 16:14:02 java常见问答 7984

对于学习java的小伙伴们来说,zookeeper一定是不陌生的,你们知道要如何在java中与zookeeper交互小结吗?这次小编就通过一个示例来带大家了解一下吧。

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
 * @author likexin
 * @date 2019/07/01
 */
public class ZKHelper implements Watcher
{
    public static ZooKeeper zk = null;
    private CountDownLatch countDownLatch = new CountDownLatch(1);
    public void process(WatchedEvent watchedEvent)
    {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
        {
            countDownLatch.countDown();
        }
    }
    /**
     * 创建连接
     * @param host
     */
    public void connectZookeeper(String host)
    {
        try
        {
            zk = new ZooKeeper(host, 2000, this);
            countDownLatch.await();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 注册节点
     * @param path 节点的路径
     */
    public void regist(String path)
    {
        try
        {
            zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        }
        catch (KeeperException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 修改节点的内容
     * @param path 节点路径
     * @param data
     */
    public void updateNodeData(String path, String data)
    {
        try
        {
            zk.setData(path, data.getBytes(), -1);
        }
        catch (KeeperException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 获取数据
     * @param path 节点路径
     * @return
     */
    public String getData(String path)
    {
        Stat stat = new Stat();
        String data = null;
        try
        {
            data = new String(zk.getData(path, true, stat));
        }
        catch (KeeperException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return data;
    }
    /**
     * 关闭连接
     */
    public void close()
    {
        try
        {
            zk.close();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        ZKHelper zkHelper = new ZKHelper();
        zkHelper.connectZookeeper("172.0.0.1");
        zkHelper.regist("/PATH/DATA");
        zkHelper.updateNodeData("/PATH/DATA", "Love");
        String data = zkHelper.getData("/PATH/DATA");
        System.out.println(data);
        zkHelper.close();
    }
}

以上就是本篇文章的所有内容,你应该知道java与zookeeper小结的方式了吧。还有疑问并且想了解更多java面试常见问题的小伙伴,就请关注我们的网站了解具体吧。

推荐阅读:

zookeeper集群由哪些角色组成的呢?

zookeeper实现分布式锁怎么实现?步骤有哪些?

zookeeper配置如何进行?zookeeper有什么作用?