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();
}
}
}