标签: apache-curator

如何使用Curator检查Zookeeper中是否存在路径?

我目前处于开发的早期阶段,将Zookeeper的东西集成到我的应用程序中.我正在使用Netflix的Curator作为处理Zookeeper的API.我得到了大部分的要点(比如添加ZNodes和诸如此类的东西).

但是你怎么检查ZNode是否存在?看起来像它的代码是:

client.checkExists().forPath(path);
Run Code Online (Sandbox Code Playgroud)

... ... client的实例在哪里com.netflix.curator.framework.CuratorFramework

但是,此调用返回一个org.apache.zookeeper.data.Stat对象.

使用此对象,如何判断路径是否存在?

谢谢

java apache-zookeeper apache-curator

20
推荐指数
1
解决办法
1万
查看次数

尝试创建zNode时Apache Curator未实现的错误

我正在尝试将Apache Curator与dockerized zookeeper实例一起使用,无论我如何尝试连接,我总是最终得到一个

org.apache.zookeeper.KeeperException $ UnimplementedException:KeeperErrorCode =未实现...

错误.我已经尝试了解文档,但我没有到达任何地方.我已登录zookeeper CLI并确保端口号正确:

snerd@powerglove:~$ docker ps CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS   NAMES 31f1093495ba        compose_zookeeper   "/opt/zookeeper/bin/   3 weeks ago         Up About a minute   0.0.0.0:32770->2181/tcp,
0.0.0.0:32769->2888/tcp, 0.0.0.0:32768->3888/tcp   zookeeper
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试使用的代码:

public class App {
    public static void main( String[] args ) {
        CuratorFramework client = CuratorFrameworkFactory.newClient("0.0.0.0:32770", new RetryUntilElapsed(3000, 1000));
        client.start();

            try {
                client.create().forPath("/larry-smells/foop", "tuna?".getBytes());
            } catch (Exception e) {
                System.out.println(e.toString());
            }

    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,从策展人入门页面来看,这应该可行.我错过了什么?

edit1 刚才发现我能够将数据从动物园管理员集合中拉出来:

System.out.println(new String(curatorFramework.getData().forPath("/larry-smells"))); 
Run Code Online (Sandbox Code Playgroud)

但是create命令仍然在爆炸.

EDIT2

错误的堆栈跟踪:

org.apache.zookeeper.KeeperException $ …

apache apache-zookeeper apache-curator

18
推荐指数
1
解决办法
7531
查看次数

与策展人一起使用ACL

使用CuratorFramework,有人可以解释我怎么做:

  1. 创建一个新路径
  2. 设置此路径的数据
  3. 走这条路

使用用户名foo和密码bar?那些不知道这个用户/通行证的人将无法做任何事情.

出于此问题的目的,我不关心通过明文发送的SSL或密码.

java apache-zookeeper apache-curator

18
推荐指数
1
解决办法
1864
查看次数

如何实现分布式限速器?

比方说,我让P进程在N台物理机上运行一些业务逻辑.例如,这些进程调用了一些Web服务S. 我想确保所有P进程组合的每秒服务S的调用不超过10次.

如何实施这样的解决方案?

Google Guava的速率限制器适用于在单个机箱上运行的进程,但不适用于分布式设置.

JAVA有哪些标准的,随时可用的解决方案?[可能基于zookeeper]

谢谢!

distributed-computing rate-limiting guava apache-zookeeper apache-curator

14
推荐指数
2
解决办法
7162
查看次数

Apache策展人"连接字符串"是什么样的?

给出的连接字符串是CuratorFrameworkFactory#newClient什么样的?到目前为止,我还没有在网上找到任何信息,JavaDoc也没有告诉我正确的格式.

apache-curator

13
推荐指数
1
解决办法
5839
查看次数

如何在重新获得连接时避免双重连接?

在这些情况下:

  1. 客户端失去了与zk的网络连接.
  2. 一分钟过去了.
  3. 客户端重新获得与zk的网络连接.

我有以下恐慌:

panic: close of closed channel

goroutine 2849 [running]:
github.com/samuel/go-zookeeper/zk.(*Conn).Close(0xc420795180)
  github.com/samuel/go-zookeeper/zk/conn.go:253 47
github.com/curator-go/curator.(*handleHolder).internalClose(0xc4203058f0, 0xc420302470, 0x0)
  github.com/curator-go/curator/state.go:136 +0x8d
github.com/curator-go/curator.(*handleHolder).closeAndReset(0xc4203058f0, 0xc42587cd00, 0x1e)
  github.com/curator-go/curator/state.go:122 +0x2f
github.com/curator-go/curator.(*connectionState).reset(0xc420302420, 0x1b71d87, 0xf)
  github.com/curator-go/curator/state.go:234 +0x55
github.com/curator-go/curator.(*connectionState).handleExpiredSession(0xc420302420)
  github.com/curator-go/curator/state.go:351 +0xd9
github.com/curator-go/curator.(*connectionState).checkState(0xc420302420, 0xffffff90, 0x0, 0x0, 0xc425ed2600, 0xed0e5250a)
  github.com/curator-go/curator/state.go:318 +0x9c
github.com/curator-go/curator.(*connectionState).process(0xc420302420, 0xc425ed2680)
  github.com/curator-go/curator/state.go:299 +0x16d
created by github.com/curator-go/curator.(*Watchers).Fire
  github.com/curator-go/curator/watcher.go:64 +0x96
Run Code Online (Sandbox Code Playgroud)

这是详细的事件序列:

  1. 客户端失去了与zk的网络连接.
  2. 一分钟过去了.
  3. 客户端重新获得与zk的网络连接.
  4. goroutine A调用s.ReregisterAll()- > Conn()- > checkTimeout()- > reset(bc已过去1分钟) - > closeAndReset()- > conn.Close() 可以阻止一秒钟
  5. goroutine B句柄zk.StateExpired(zk集群发送此bc它认为这个客户端已经死了,因为它在2期间没有ping.) - …

go apache-zookeeper apache-curator

12
推荐指数
1
解决办法
562
查看次数

11
推荐指数
2
解决办法
4559
查看次数

如何使用Curator for Zookeeper高效地使用LeaderElection配方?

我正在使用Apache Curator库在Zookeeper上进行领导选举.我将我的应用程序代码部署在各种机器上,我只需要从一台机器执行我的代码,这就是为什么我在动物园管理员上进行领导选举,以便我可以检查我是否是领导者,然后执行此代码.

下面是我的LeaderElectionExecutor类,它确保我每个应用程序都有一个Curator实例

public class LeaderElectionExecutor {

    private ZookeeperClient zookClient;

    private static final String LEADER_NODE = "/testleader";

    private static class Holder {
        static final LeaderElectionExecutor INSTANCE = new LeaderElectionExecutor();
    }

    public static LeaderElectionExecutor getInstance() {
        return Holder.INSTANCE;
    }

    private LeaderElectionExecutor() {
        try {
            String hostname = Utils.getHostName();

            String nodes = "host1:2181,host2:2181;

            zookClient = new ZookeeperClient(nodes, LEADER_NODE, hostname);
            zookClient.start();

            // added sleep specifically for the leader to get selected
            // since I cannot call isLeader method immediately after starting …
Run Code Online (Sandbox Code Playgroud)

java distributed apache-zookeeper apache-curator

11
推荐指数
1
解决办法
4075
查看次数

如何使用kazoo客户端进行领导者选举?

这是kazoo readthedocs上提到的代码

election=zk.Election("/electionpath", "my-identifier")
Run Code Online (Sandbox Code Playgroud)

将特定节点作为领导者传递的输入参数是什么?(即/ electionpath和my-identifier在这里指的是什么?)

python kazoo apache-zookeeper apache-curator

10
推荐指数
1
解决办法
1281
查看次数

如何使用curator监视ZooKeeper中后代节点上的事件?

我正在开发一个项目,我需要在节点上维护一个监视器,并且节点也是子节点.我尝试过使用PathCache,但我不确定如何在这里观看儿童的孩子?

这里我的根节点是 - "/my/test"我正在使用下面的代码监视该节点.我想做的是,让手表保持在"/my/test"znode上.假设这些节点被添加到我的根节点 -

"/my/test/test1"
"/my/test/test2"
"/my/test/test3"
Run Code Online (Sandbox Code Playgroud)

然后,我应该得到通知(直到这部分我能使其工作),但如果有新的节点被添加,更新或删除来"/my/test/test1","/my/test/test2""/my/test/test3"那我也应该得到通知,这是我无法理解如何将部分让它起作用.

每当我加入任何新的节点"/my/test",例如"/my/test/test1","/my/test/test2","/my/test/test3"那么手表被触发同用下面的代码.但是,如果我要添加任何新节点,"/my/test/test1"或者"/my/test/test2"没有手表被触发,我不知道如何添加代码呢?有什么想法可以做到这一点?

可能如果有人在过去这样做了..所以任何一个例子对我都有很大的帮助..

以下是我的代码,适用于"/my/test"儿童,但不适用于儿童"/my/test/test1"等等.

private static final String PATH = "/my/test";

public static void main(String[] args) {
    CuratorFramework client = null;
    PathChildrenCache cache = null;
    try {
        client = CuratorClient.createSimple("localhost:2181");
        client.start();

        // in this example we will cache data. Notice that this is optional.
        cache = new …
Run Code Online (Sandbox Code Playgroud)

java apache-zookeeper apache-curator

9
推荐指数
2
解决办法
7450
查看次数