android 使用带 curator 的 zk 客户端连接 zk 获取数据失败,连接丢失

android use zk client with curator to connect zk get data failed with connectionLoss

zk 客户端:android 使用 curator 4.2

zk 服务器:zk 版本 3.5.6

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

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

                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地址)。

我修复了它,只需关闭使用 System.setProperties(ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY, "false");