springboot es多版本客户端配置

application.propertyies

es207.config=ip1:9200,ip2:9200,ip3:9200
es211.config=ipa:9200,ipb:9200,ipc:9200

 

1:配置 

package com.anyao.voip.config.elasticsearch;

import com.anyao.voip.constants.CommonConstants;
import com.anyao.voip.exception.VoipException;
import com.anyao.voip.helper.LogHelper;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Objects;

/*
*  如果查询的时间超过 5min  则会报错
*  setSocketTimeout(5min)
*  setMaxRetryTimeoutMillis(5min)
* */
@Configuration
public class ElasticsearchConfig {
    private static Logger logger = LoggerFactory.getLogger(ElasticsearchConfig.class);

    private static final int ADDRESS_LENGTH = 2;
    private static final String HTTP_SCHEME = "http";

    @Value("${es.config.207}")
    String[] ipAddress207;
    @Value("${es.config.211}")
    String[] ipAddress211;
//CommonConstants.CLIENT_NAME_211="client211"
    @Bean(autowire = Autowire.BY_NAME, name = CommonConstants.CLIENT_NAME_211)
    public RestHighLevelClient highLevelClient211() {
        RestClientBuilder restClientBuilder211 = restClientBuilder211();
        restClientBuilder211.setMaxRetryTimeoutMillis(1000*60*2);
        return new RestHighLevelClient(restClientBuilder211);
    }
//CommonConstants.CLIENT_NAME_207 ="client207"
    @Bean(autowire = Autowire.BY_NAME, name = CommonConstants.CLIENT_NAME_207)
    public RestHighLevelClient highLevelClient207() {
        RestClientBuilder restClientBuilder207 = restClientBuilder207();
        restClientBuilder207.setMaxRetryTimeoutMillis(1000*60*2);
        return new RestHighLevelClient(restClientBuilder207);
    }

    @Bean(autowire = Autowire.BY_NAME, name = "restClientBuilder207")
    public RestClientBuilder restClientBuilder207() {
       return createRestClientBuilder(ipAddress207);
    }

    @Bean(autowire = Autowire.BY_NAME, name = "restClientBuilder211")
    public RestClientBuilder restClientBuilder211() {
        return createRestClientBuilder(ipAddress211);
    }

    protected  RestClientBuilder createRestClientBuilder(String[] ipAddressAndPort){
        try {
            HttpHost[] hosts = Arrays.stream(ipAddressAndPort)
                    .map(this::makeHttpHost)
                    .filter(Objects::nonNull)
                    .toArray(HttpHost[]::new);
            LogHelper.info(logger,"Conncect to elasticsearch,the es host is:[{}]", hosts);
            return RestClient.builder(hosts).setRequestConfigCallback((RequestConfig.Builder builder)-> builder.setConnectTimeout(5000).setSocketTimeout(1000*60*2));
        }catch (Exception e){
            throw new VoipException("Failed to conncect to elasticsearch", e);
        }
    }

    protected HttpHost makeHttpHost(String s) {
        if(s == null || s.isEmpty()){
            throw new VoipException(s + " is empty");
        }
        String[] address = s.split(":");
        if (address.length == ADDRESS_LENGTH) {
            String ip = address[0];
            int port = Integer.parseInt(address[1]);
            return new HttpHost(ip, port, HTTP_SCHEME);
        } else {
            return null;
        }
    }
}

 

2:使用

@Resource(name = "client207")
RestHighLevelClient client_A;




@Resource(name = "client211") 

RestHighLevelClient client_B;

 

总结:我用的elasticsearch 7.2是  Java High Level REST Client  不是transport 客户端

详情见https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.2/index.html

你可能感兴趣的:(springboot,大数据,ELK,spring,boot,elasticsearch)