如何通过SpringBoot中的RestHighLevelClient访问2个Elasticsearch实例

时间:2019-09-16 06:02:36

标签: spring-boot elasticsearch

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

HttpHost [] httpHost =新的HttpHost(hostName [i ...],Integer.parseInt(hostName [i ..]),“ http”);

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

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

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

或者如果有可能,是否有可能通过2个bean实例

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

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

enter image description here

代码

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

1 个答案:

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