1

I am using CuratorFramework (I'm still a newbie) in order to connect to a Zookeeper instance. I would like to import a configuration but before that I would like to test that my program is able to connect to Zookeeper. So far I have something like that:

public Boolean zookeeperRunning() {
    CuratorFramework curatorFramework = 
            CuratorFrameworkFactory.newClient(zookeeperConn, new RetryOneTime(1));
    curatorFramework.start();

    CuratorZookeeperClient zkClient = curatorFramework.getZookeeperClient();
    return zkClient.isConnected();
}

I've already started ZooKeeper on my local machine and I checked the connection with zkCli and the client is able to connect to it. The zookeeperCon variable is set to "127.0.0.1:2181" (I tried with localhost:2181 as well). The problem is that the above method always returns false despite the fact that zkServer is up n running. Most probably, the syntax is not correct but I could not find a solution online. Could you please help me with why the above code cannot find the zkServer which is up and running?

4

3 回答 3

2

您可以使用构建器创建配置的客户端并设置侦听器来监控 zk 实例的状态:

// start client
client = CuratorFrameworkFactory.builder()
    .connectString("localhost:2181")
    .retryPolicy(new ExponentialBackoffRetry(1000, 3))
    .namespace("heavenize")
    .build();

client.getConnectionStateListenable().addListener(new ConnectionStateListener() {      
  @Override
  public void stateChanged(CuratorFramework client, ConnectionState newState)
  {
    log.info("State changed to: "+newState);
  }
});
}
于 2014-10-31T10:31:43.507 回答
1

获取成功后应该先连接zookeeper zkClient,如果成功,再检查isConnected状态。下面的演示代码(参考:这里):

private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
于 2019-02-18T02:55:15.743 回答
0

你应该连接到zookeeper服务器然后检查它。例如:

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.test.TestingServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

public class ZkClientTest {
    TestingServer zkServer;

    @Before
    public void startZookeeper() throws Exception {
        zkServer = new TestingServer(2181);
        zkServer.start();
    }

    @After
    public void stopZookeeper() throws IOException {
        zkServer.stop();
    }

    @Test
    public void should_connect_to_zookeeper_server_when_config_use_default_localhost_2181()
        throws InterruptedException {
        CuratorFramework client = ZkClient.getInstance().getClient();
        try {
            client.blockUntilConnected(3, TimeUnit.SECONDS);
            assertTrue(ZkClient.getInstance().getClient().getZookeeperClient().isConnected());
        } finally {
            ZkClient.getInstance().close();
        }
    }
}
于 2020-07-30T13:43:13.107 回答