我尝试在我们的项目中使用 Zookeeper。可以运行服务器..甚至使用 zkcli.sh 测试它..一切都很好.. 但找不到一个好的教程让我使用 Java 连接到该服务器!我在 Java API 中所需要的只是一个方法
public String getServiceURL ( String serviceName )
我尝试过 https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index --> 对我来说不好。
http://zookeeper.apache.org/doc/trunk/javaExample.html:还可以;但无法清楚地理解概念!我感觉没解释清楚..
最后,这是我想出的最简单、最基本的程序,它将帮助您了解 ZooKeeper“入门”:
package core.framework.zookeeper;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZkConnect {
private ZooKeeper zk;
private CountDownLatch connSignal = new CountDownLatch(0);
//host should be 127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002
public ZooKeeper connect(String host) throws Exception {
zk = new ZooKeeper(host, 3000, new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connSignal.countDown();
}
}
});
connSignal.await();
return zk;
}
public void close() throws InterruptedException {
zk.close();
}
public void createNode(String path, byte[] data) throws Exception
{
zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
public void updateNode(String path, byte[] data) throws Exception
{
zk.setData(path, data, zk.exists(path, true).getVersion());
}
public void deleteNode(String path) throws Exception
{
zk.delete(path, zk.exists(path, true).getVersion());
}
public static void main (String args[]) throws Exception
{
ZkConnect connector = new ZkConnect();
ZooKeeper zk = connector.connect("54.169.132.0,52.74.51.0");
String newNode = "/deepakDate"+new Date();
connector.createNode(newNode, new Date().toString().getBytes());
List<String> zNodes = zk.getChildren("/", true);
for (String zNode: zNodes)
{
System.out.println("ChildrenNode " + zNode);
}
byte[] data = zk.getData(newNode, true, zk.exists(newNode, true));
System.out.println("GetData before setting");
for ( byte dataPoint : data)
{
System.out.print ((char)dataPoint);
}
System.out.println("GetData after setting");
connector.updateNode(newNode, "Modified data".getBytes());
data = zk.getData(newNode, true, zk.exists(newNode, true));
for ( byte dataPoint : data)
{
System.out.print ((char)dataPoint);
}
connector.deleteNode(newNode);
}
}
这篇文章几乎包含了与Zookeeper交互所需的所有操作。 https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm
这几乎是最简单的。 我正在构建一个工具,它将使用 ZK 来锁定正在处理的文件(因此是类名):
package mypackage;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher;
public class ZooKeeperFileLock {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
String zkConnString = "<zknode1>:2181,<zknode2>:2181,<zknode3>:2181";
ZooKeeperWatcher zkWatcher = new ZooKeeperWatcher();
ZooKeeper client = new ZooKeeper(zkConnString, 10000, zkWatcher);
List<String> zkNodes = client.getChildren("/", true);
for(String node : zkNodes) {
System.out.println(node);
}
}
public static class ZooKeeperWatcher implements Watcher {
@Override
public void process(WatchedEvent event) {
}
}
这篇博文,Zookeeper Java API 示例(存档版本),如果您正在寻找 Java 示例来开始,其中包含一些很好的示例。 Zookeeper还提供了一个非常易于使用的客户端API库(C和Java)。
Zookeeper 是最好的开源服务器和服务之一,有助于可靠地协调分布式进程。 Zookeeper是一个CP系统(参考CAP定理),提供一致性和分区容错性。在所有节点之间复制 Zookeeper 状态使其成为最终一致的分布式服务。
如果您在 AWS 上;现在我们可以创建内部 ELB,它支持基于 URI 的重定向..这可以真正解决这个问题,并且已经内置了高可用性。