依赖
<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和集群,后面再写,一步一步来。