优秀的 Zookeeper Hello world Java 客户端程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33524537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good Zookeeper Hello world Program with Java client
提问by Deepak Singhal
I was trying to use Zookeeper in our project. Could run the server..Even test it using zkcli.sh .. All good.. But couldn't find a good tutorial for me to connect to this server using Java ! All I need in Java API is a method
我试图在我们的项目中使用 Zookeeper。可以运行服务器..甚至使用 zkcli.sh 测试它.. 一切都很好.. 但找不到一个好的教程让我使用 Java 连接到这个服务器!我在 Java API 中需要的只是一个方法
public String getServiceURL ( String serviceName )
I tried https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index--> Not good for me.
我试过https://cwiki.apache.org/confluence/display/ZOOKEEPER/Index--> 对我不好。
http://zookeeper.apache.org/doc/trunk/javaExample.html: Sort of ok; but couldnt understand concepts clearly ! I feel it is not explained well..
http://zookeeper.apache.org/doc/trunk/javaExample.html:还好;但不能清楚地理解概念!感觉解释的不太好。。
采纳答案by Deepak Singhal
Finally, this is the simplest and most basic program I came up with which will help you with ZooKeeper "Getting Started":
最后,这是我想出的最简单和最基本的程序,它将帮助您使用 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);
}
}
回答by Binu George
This blog post, Zookeeper Java API examples, includes some good examples if you are looking for Java examples to start with. Zookeeper also provides a client API library( C and Java) that is very easy to use.
这篇博客文章Zookeeper Java API 示例包含一些很好的示例,如果您正在寻找 Java 示例作为起点。Zookeeper 还提供了一个非常易于使用的客户端 API 库(C 和 Java)。
Zookeeper is one of the best open source server and service that helps to reliably coordinates distributed processes. Zookeeper is a CP system (Refer CAP Theorem) that provides Consistency and Partition tolerance. Replication of Zookeeper state across all the nods makes it an eventually consistent distributed service.
Zookeeper 是最好的开源服务器和服务之一,有助于可靠地协调分布式进程。Zookeeper 是一个 CP 系统(参考 CAP Theorem),提供了 Consistency 和 Partition 容错性。Zookeeper 状态在所有节点上的复制使其成为最终一致的分布式服务。
回答by Dinesh Kumar P
This post has almost all operations required to interact with Zookeeper. https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm
这篇文章几乎包含了与 Zookeeper 交互所需的所有操作。 https://www.tutorialspoint.com/zookeeper/zookeeper_api.htm
- Create ZNode with data
- Delete ZNode
- Get list of ZNodes(Children)
- Check an ZNode exists or not
- Edit the content of a ZNode...
- 使用数据创建 ZNode
- 删除 ZNode
- 获取 ZNodes(Children) 列表
- 检查 ZNode 是否存在
- 编辑 ZNode 的内容...
回答by Deepak Singhal
If you are on AWS; now We can create internal ELB which supports redirection based on URI .. which can really solve this problem with High Availability already baked in.
如果您在 AWS 上;现在我们可以创建支持基于 URI 重定向的内部 ELB .. 它可以真正解决这个问题,并且已经内置了高可用性。
回答by Chris C
This is about as simple as you can get. I am building a tool which will use ZK to lock files that are being processed (hence the class name):
这很简单。我正在构建一个工具,它将使用 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) {
}
}