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

How can I make Jest handle an ElasticSearch server being unavailable?

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

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

如果我的一个 ElasticSearch 服务器离线(例如故障或维护),那么我的 Jest 查询的百分比会失败。 Jest 似乎没有通过 default.It 进行任何类型的智能连接管理,必须通过服务器进行轮询或随机选择服务器。

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

您需要启用发现功能,以便客户端工厂在一台服务器出现故障时找到另一台服务器。应该这样做:

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

您还可以在 JestClientFactoryIntegrationTest.java 中看到他们如何对此进行测试,即他们从一个节点开始,然后再添加 3 个节点,然后关闭一个节点并断言客户端仍然有一个有效的连接到已启动的节点。