一.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...");
