我正在检查CuratorFramework实例的连接,当我在调试模式下它正常通过时,但是当我在运行模式下执行时,它会通过验证,并将条件视为false。
curatorFramework.getZookeeperClient().isConnected();
代码有问题吗?这就是我创建实例的方式:
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 1);
CuratorFramework curatorFramework= CuratorFrameworkFactory.newClient(host, retryPolicy);
curatorFramework.start();
答案 0 :(得分:1)
我无法发现任何明显错误的代码,但是,我建议尝试CuratorFrameworkFactory尝试构建您的CuratorFramework实例,如下所示:
String connectionString = ....
CuratorFramework client = CuratorFrameworkFactory.builder()
.namespace("my_namespace")
.connectString(connectionString )
.retryPolicy(new ExponentialBackoffRetry(1000, 1))
.build();
client.start();
我还建议您使用Curator内置的TestingServer来测试您的代码,以确保您目前正在测试的ZK群集不会出现问题。要设置并连接到Curator测试服务器,只需使用以下内容:
TestingServer zkTestServer = new TestingServer(31313);
CuratorFramework client = CuratorFrameworkFactory.builder()
.namespace("my_namespace")
.connectString(zkTestServer.getConnectString())
.retryPolicy(new ExponentialBackoffRetry(1000, 1))
.build();
client.start();
答案 1 :(得分:0)
使用curatorFramework.blockUntilConnected()
启动curatorFramework之后,应该连接到zk,然后获取客户端并检查连接状态。如下所示:
...
curatorFramework.start();
try {
curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
if (curatorFramework.getZookeeperClient().isConnected()) {
System.out.println(curatorFramework.getState());
return;
}
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
...