zookeeper基本操作

zookeeper基本操作

一、安装zookeeper

启动命令

bin/zookeeper-server-start.sh config/zookeeper.properties
二、Maven依赖
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-client</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.9.0</version>
</dependency>
三、zookeeper基本操作
package zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.ZooKeeper;

import java.nio.charset.Charset;

/**
 * zookeeper 基本操作<br/>
 * Created by zhengyong on 16/11/24.
 */
public class ZookeeperCURD {

    private static final int        sessionTimeout = 15000;
    private static final String     ZK_HOST        = "127.0.0.1:2181";
    private static final String     ZK_PATH        = "/zkPath";
    private static CuratorFramework curatorClient  = null;

    public static void main(String[] args) throws Exception {

        createClient();

        create(ZK_PATH);

        setDataValue(ZK_PATH, "test data");

        getDataValue(ZK_PATH);

        delete(ZK_PATH);

    }

    /**
     * 创建zookeeper访问客户端
     * 
     * @throws Exception
     */
    private static void createClient() throws Exception {

        if (curatorClient == null) {
            synchronized (ZookeeperCURD.class) {
                curatorClient = CuratorFrameworkFactory.builder().connectString(ZK_HOST).sessionTimeoutMs(sessionTimeout).retryPolicy(new ExponentialBackoffRetry(1000,
                                                                                                                                                                  10,
                                                                                                                                                                  5000)).build();
                curatorClient.start();
            }
        }
    }

    /**
     * 创建zookeeper节点信息
     *
     * @throws Exception
     */
    private static void create(String path) throws Exception {
        ZooKeeper zookeeper = new ZooKeeper(ZK_HOST, sessionTimeout, null);
        ZKPaths.mkdirs(zookeeper, path);
        System.out.println(String.format("create path=%s", path));
    }

    /**
     * 设置path node 值
     * 
     * @param path 路径
     * @param data 值
     * @throws Exception
     */
    private static void setDataValue(String path, String data) throws Exception {
        curatorClient.setData().forPath(path, data.getBytes(Charset.forName("UTf-8")));
        System.out.println(String.format("set path=%s, data=%s", path, data));
    }

    /**
     * 获取path node 值
     * 
     * @param path 路径
     * @return 值
     * @throws Exception
     */
    private static String getDataValue(String path) throws Exception {
        byte[] value = curatorClient.getData().forPath(path);
        String result = new String(value, Charset.forName("UTf-8"));
        System.out.println(String.format("get path=%s, data=%s", path, result));
        return result;

    }

    /**
     * 删除节点
     * 
     * @param path 路径
     */
    private static void delete(String path) throws Exception {
        curatorClient.delete().forPath(path);
        System.out.println(String.format("delete path=%s", path));
    }
}

运行结果:

create path=/zkPath
set path=/zkPath, data=test data
get path=/zkPath, data=test data
delete path=/zkPath
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值