Zookeeper:JavaApi创建节点
一.pom文件和log4j.properties
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wj</groupId> <artifactId>zookeeperApi</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!-- https://mvnrepository.com/artifact/jline/jline --> <dependency> <groupId>jline</groupId> <artifactId>jline</artifactId> <version>0.9.94</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.6.1</version> </dependency> </dependencies> </project>
# Define some default values that can be overridden by system properties log4j.rootLogger=INFO, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=INFO log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p - %m%n
二.Java连接Zookeeper
public static void main(String[] args) throws Exception { //计数器对象 final CountDownLatch latch = new CountDownLatch(1); // * @param sessionTimeout // * session timeout in milliseconds // * @param watcher // * a watcher object which will be notified of state changes, may // * also be notified for node events ZooKeeper zooKeeper = new ZooKeeper("192.168.10.132:2181", 500, new Watcher() { public void process(WatchedEvent event) { if(event.getState() ==Watcher.Event.KeeperState.SyncConnected){ System.out.println("连接创建成功"); latch.countDown(); } } }); //主线程阻塞等待连接对象的创建成功 latch.await(); System.out.println("----"+zooKeeper.getSessionId()+"----"); zooKeeper.close(); }
控制台打印情况:
三.创建节点
//同步方式 create(final String path, byte data[], List<ACL> acl,CreateMode createMode) //异步方式 create(final String path, byte data[], List<ACL> acl,CreateMode createMode, StringCallback cb, Object ctx)
参数说明:
path:-znode路径
data:节点数据内容
acl:访问控制列表
createMode:节点的类型,枚举类
cb:异步回调接口
ctx:传递上下文参数
//节点路径 /create //节点数据 create //权限列表: world:anyone:adrwa //节点类型:持久化节点 zooKeeper.create("/create","create".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
world授权:
List<ACL> acls = new ArrayList<ACL>(); Id id = new Id("world", "anyone"); acls.add(new ACL(ZooDefs.Perms.READ,id)); acls.add(new ACL(ZooDefs.Perms.WRITE,id)); zooKeeper.create("/create/node2","node2".getBytes(), acls, CreateMode.PERSISTENT);
ip授权:
List<ACL> acls = new ArrayList<ACL>(); Id id = new Id("ip", "192.168.10.132"); acls.add(new ACL(ZooDefs.Perms.ALL,id)); zooKeeper.create("/create/node3","node3".getBytes(), acls, CreateMode.PERSISTENT);
auth授权:
//添加授权用户 zooKeeper.addAuthInfo("digest","admin:admin".getBytes());
//给予所有权限 zooKeeper.create("/create/node4","node4".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
digest授权:
//授权模式和授权对象 Id id = new Id("digest", "wj:64ibjjwm94195LPhuzhUdkIjOl0="); //所有权限 acls.add(new ACL(ZooDefs.Perms.ALL,id)); zooKeeper.create("/create/node5","node5".getBytes(), acls, CreateMode.PERSISTENT);
创建持久化有序节点:
输出结果为节点路径
String s = zooKeeper.create("/create/node6","node6".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println(s);
CreateMode枚举类介绍:
异步方式创建节点:
//异步方式创建节点 zooKeeper.create("/create/node6", "node6".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() { public void processResult(int rc, String path, Object ctx, String name) { System.out.println(rc+"=="+path+"=="+ctx+"=="+name); } },"context...."); Thread.sleep(5000); System.out.println("end...");