zk客户端:android使用curator 4.2

zk服务器:zk版本3.5.6

问题:当我连接zk从“ /”获取数据时,失败

org.apache.zookeeper.KeeperException $ ConnectionLossException:KeeperErrorCode = /的ConnectionLoss

                class MyThread implements Runnable {
                    @Override
                    public void run() {
                        try {
                            String ip = "192.168.0.111";
                            System.out.println("start new");
                            ExponentialBackoffRetry retry = new ExponentialBackoffRetry(10000, 3);
                            System.out.println("end new");
                            CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(ip, retry );
                            System.out.println("end new2");

                            curatorFramework.start();

                            Stat nodeStat = new Stat();
                            byte[] nodeInfo = curatorFramework.getData()
                                    .storingStatIn(nodeStat)
                                    .forPath("/");
                            System.out.println("===node info is:" + new String(nodeInfo));

                            curatorFramework.create().creatingParentContainersIfNeeded()
                                    .withMode(CreateMode.PERSISTENT)
                                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                                    .forPath("/nodeTest", "testData".getBytes());
                            System.out.println("wtf");
                            button1.setText("nihaoya");
                        } catch (Exception e) {
                            System.out.println( e.toString());
                        }
                    }
                }

最佳答案

ZooKeeper中的连接字符串不仅是IP地址。您还必须包括客户端端口(通常为2181)。另外,您应该将所有服务器都包含在集合中(我假设您有3个)。因此,常见的连接字符串是111.111.111.111:2181,222.222.222.222:2181,333.333.333.333:2181(当然,请使用您的IP地址)。

10-08 17:57