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



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

 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似乎不会进行任何类型的智能连接管理。它必须在服务器之间进行类似循环的操作,或者随机选择一个服务器。

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

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

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个节点,然后关闭一个节点,并断言客户端仍然与一个打开的节点有有效连接。

最新更新