Zookeeper原生API使用

1、jar包引入,演示版本为3.4.6,非maven项目,可以下载jar包导入到项目中

<dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
</dependency>

2、创建zookeeper连接

  ZooKeeper(java.lang.String connectString, int sessionTimeout, org.apache.zookeeper.Watcher watcher) 

  • connectString:zookeeper服务地址,例如“192.168.117.128:2181”
  • sessionTimeout :超时时间,单位为毫秒
  • watcher:实现org.apache.zookeeper.Watcher接口的实现类,需实现process(WatchedEvent watchedEvent) 方法

  ZooKeeper zooKeeper = new ZooKeeper("192.168.117.128:2181",5000, new MyWatcher());

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description: TODO(Java API -> 创建连接 -> 创建一个最基本的ZooKeeper对象实例)
 * User: zhubo
 * Date: 2018-01-09
 * Time: 20:58
 */
public class ZooKeeper_Constructor_Usage_Simple implements Watcher{

    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    public static void main(String[] args) throws Exception{
        ZooKeeper zooKeeper = new ZooKeeper("centos4:2181",5000,new ZooKeeper_Constructor_Usage_Simple());
        System.out.println(zooKeeper.getState());
        try{
            countDownLatch.await();
        }catch (Exception e){
            e.printStackTrace();
        }
        long sessionId = zooKeeper.getSessionId();
        byte[] sessionPasswd = zooKeeper.getSessionPasswd();
        System.out.println(sessionId);
        System.out.println(sessionPasswd);
        System.out.println("ZooKeeper session established.");
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("Receive watched event:" + watchedEvent);
        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            countDownLatch.countDown();
        }
    }
}

ps:此处没有新创建新的java类实现Watcher,而是直接在本类中实现Watcher接口并重写process方法

3、同步创建

create(java.lang.String path, byte[] data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode createMode)

  • path:创建节点路径,需保证父节点已存在
  • data:节点数据
  • acl:权限列表
    • 自定义权限  
    • 提供默认的权限OPEN_ACL_UNSAFE、CREATOR_ALL_ACL、READ_ACL_UNSAFE
      • OPEN_ACL_UNSAFE:完全开放
      • CREATOR_ALL_ACL:创建该znode的连接拥有所有权限
      • READ_ACL_UNSAFE:所有的客户端都可读
      • 自定义权限
        ACL aclIp = new ACL(ZooDefs.Perms.READ,new Id("ip","127.0.0.1"));
                        ACL aclDigest = new ACL(ZooDefs.Perms.READ| ZooDefs.Perms.WRITE,
                                new Id("digest", DigestAuthenticationProvider.generateDigest("id:pass")));

         

      • session设置权限 zk.addAuthInfo("digest", "id:pass".getBytes());

    • createMode:节点类型

      • PERSISTENT:持久化节点
      • PERSISTENT_SEQUENTIAL:持久化有序节点
      • EPHEMERAL:临时节点(连接断开自动删除)
      • EPHEMERAL_SEQUENTIAL:临时有序节点(连接断开自动删除)

示例代码:


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: zhubo
 * Date: 2018-01-09
 * Time: 22:24
 */
public class ZooKeeper_Create_API_Sync_Usage implements Watcher {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws Exception {
        ZooKeeper zookeeper = new ZooKeeper("",5000,new ZooKeeper_Create_API_Sync_Usage());
        countDownLatch.await();
        String path1 = zookeeper.create("/zk-test-ephemeral-","".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("Success create znode: " + path1);
        String path2 = zookeeper.create("/zk-test-ephemeral-","".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.EPHEMERAL);
        System.out.println(""+path2);
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            countDownLatch.countDown();
        }
    }
}

 

 

 

 

 

 

 

转载于:https://my.oschina.net/LucasZhu/blog/1604802

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值