1

我试图通过 HighLevelRestClient 访问 2 Elasticsearch Server 实例,但我无法通过这个数组对象

HttpHost[] httpHost = new HttpHost(hostName[i...], Integer.parseInt(hostName[i..]), "http");

在主机名中,我在 restHighLevelClient = new RestHighLevelClient(RestClient.builder(httpHost)); 中有 2 个值

我也无法通过第二个数组实例访问。

如果这样如何创建 HighLevelRestClient 的 2 个实例,我可以有 2 个配置类吗

或者是否有任何可能的方式通过 2 bean 实例,如果这种方式是可能的

因为我们需要有 2 个不同的 restHighLevelClient 实例。

如果需要更多信息,请告诉我。

在此处输入图像描述

代码

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppElasticSearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {

    private static final Logger LOG = LoggerFactory.getLogger(AppElasticSearchConfiguration.class);

    @Value("${application.elasticsearch.host}")
    private String hostName[];

    private RestHighLevelClient restHighLevelClient;

    @Override
    public void destroy() {
        try {
            if (restHighLevelClient != null) {
                restHighLevelClient.close();
            }
        } catch (final Exception e) {
            LOG.error("Error closing ElasticSearch client: ", e);
        }
    }

    @Override
    public Class<RestHighLevelClient> getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public RestHighLevelClient createInstance() {
        return buildClient();
    }

    private RestHighLevelClient buildClient() {
        try {
            HttpHost[] httpHost = null;
            if(hostName!=null) {
                httpHost = new HttpHost[hostName.length];
                for (int i = 0; i < httpHost.length; i++) {
                    httpHost[i] = new HttpHost(hostName[i].split(":")[0], 
                                            Integer.parseInt(hostName[i].split(":")[1]), "http");               
                }
            }
            restHighLevelClient = new RestHighLevelClient( RestClient.builder(httpHost));

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return restHighLevelClient;
    }


    //public RestHighLevelClient getAppRestHighLevelClient() { return restHighLevelClient; }
}
4

1 回答 1

0

您好只需在 HttpHost 的构造函数中传递辅助实例即可。

@Bean(destroyMethod = "close")
public RestHighLevelClient buildClient() {

    RestClientBuilder builder = RestClient.builder(new HttpHost(hostPrimary, Integer.valueOf(portPrimary), "http"),
            new HttpHost(hostSecondary, Integer.valueOf(portSecondary), "http"));
    RestHighLevelClient client = new RestHighLevelClient(builder);
    LOG.info("RestHighLevelClient has been created with:{}", client);

    return client;
}
于 2020-04-09T06:10:41.190 回答