如何让Jest处理ElasticSearch服务器不可用?

ler*_*ros 2 elasticsearch jest

我目前通过给它一个服务器URI列表来配置Jest.像这样:

 public JestClient jestClient() {
    final JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
            .Builder(esServerUris)
            .build());
    final JestClient jestClient = factory.getObject();
    return jestClient;
}
Run Code Online (Sandbox Code Playgroud)

如果我的一个ElasticSearch服务器脱机(例如故障或维护),那么我的Jest查询的百分比将失败.Jest默认情况下似乎没有进行任何类型的智能连接管理.它必须通过服务器进行循环或随机选择服务器.

有没有更好的方法来处理这个?

Val*_*Val 5

您需要启用发现,以便客户端工厂在发生故障时找到另一台服务器.这样的事情应该做:

public JestClient jestClient() {
    final JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
            .Builder(esServerUris)
            .discoveryEnabled(true)
            .discoveryFrequency(500l, TimeUnit.MILLISECONDS)
            .build());
    final JestClient jestClient = factory.getObject();
    return jestClient;
}
Run Code Online (Sandbox Code Playgroud)

您还可以看到它们是如何测试它的JestClientFactoryIntegrationTest.java,即它们从单个节点开始,然后再添加3个节点,然后关闭一个节点并断言客户端仍然具有到达的节点的有效连接.