集成zookeeper

依赖
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.6.0</version>
</dependency>
集成
  • 创建ZookeeperWatcher类,实现Watcher类。
package com.ct.zookeeper;

import org.apache.zookeeper.*;


public class ZookeeperWatcher implements Watcher {
    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("connect success");
        }
    }
}
  • 创建client
package com.ct.zookeeper;

import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class ZookeeperUtils {

    public static ZooKeeper getClient(String connectString) {
        try {
            return new ZooKeeper(connectString, 30000, new ZookeeperWatcher());
        } catch (IOException e) {
            //
            System.out.println("create client failed");
        }
        return null;
    }
}

client实现之后,就可以像命令行一样操作服务器。

  • 测试一下
package zookeeper;

import com.ct.zookeeper.ZookeeperUtils;
import com.ct.zookeeper.ZookeeperWatcher;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.util.List;

public class zookeeperTest {

    private static ZooKeeper zooKeeperClient;
    private static final String mockPath = "/testNode";
    private static final String msg = "hello zookeeper";

    static {
        zooKeeperClient = ZookeeperUtils.getClient("192.168.2.113:2181");
    }

    @Test
    public void createTest() throws Exception {
        // exists
        Stat exists = zooKeeperClient.exists(mockPath, new ZookeeperWatcher());
        System.out.println("path=" + exists);

        Stat exists2 = zooKeeperClient.exists("/testNode01", new ZookeeperWatcher());
        System.out.println("path01=" + exists2);

        if (exists == null) {
            //create
            String path = zooKeeperClient.create(mockPath, msg.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(path);

            //get
            byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
            System.out.println(new String(data));

            List<ACL> acl = zooKeeperClient.getACL(mockPath, new Stat());
            for (ACL a : acl) {
                System.out.println(a);
            }
        }

        // get children
        List<String> children = zooKeeperClient.getChildren("/", new ZookeeperWatcher(), new Stat());
        for (String c : children) {
            System.out.println(c);
        }
    }

    @Test
    public void updateTest() throws Exception {
        Stat stat = zooKeeperClient.setData(mockPath, "hello world".getBytes(), -1);
        byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
        System.out.println(stat);
        System.out.println(new String(data));
    }


    @Test
    public void deleteTest() throws Exception {
        Stat exists = zooKeeperClient.exists(mockPath, false);
        if (exists == null) {
           zooKeeperClient.create(mockPath, msg.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        byte[] data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
        System.out.println("old data=" + new String(data));

        zooKeeperClient.delete(mockPath, -1);

        exists = zooKeeperClient.exists(mockPath, false);
        if (exists != null) {
            data = zooKeeperClient.getData(mockPath, new ZookeeperWatcher(), new Stat());
            System.out.println("data=" + new String(data));
        }
    }
}

关于ACL和集群,后面再写,一步一步来。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值