如果Zookeeper正在运行,如何以编程方式检查?

时间:2014-10-31 10:19:01

标签: java apache-zookeeper apache-curator

我正在使用CuratorFramework(我还是新手)才能连接到Zookeeper实例。我想导入配置但在此之前我想测试我的程序能够连接到Zookeeper。到目前为止,我有类似的东西:

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

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

我已经在我的本地计算机上启动了ZooKeeper,并且我检查了与zkCli的连接,并且客户端能够连接到它。 zookeeperCon变量设置为“127.0.0.1:2181”(我也尝试使用localhost:2181)。问题是尽管zkServer正在运行,但上述方法总是返回false。最有可能的是,语法不正确但我无法在线找到解决方案。能不能帮我解释为什么上面的代码找不到启动并运行的zkServer?

3 个答案:

答案 0 :(得分: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);
  }
});
}

答案 1 :(得分:0)

获得zkClient后,您应该首先连接到zookeeper,如果成功,则检查isConnected状态。下面的演示代码(参考:here):

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);
}

答案 2 :(得分: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();
        }
    }
}