elasticsearch 클라이언트 탐지기

5971 단어 elasticsearch
POM
6.2.3



    org.elasticsearch
    elasticsearch
    ${elasticsearch.version}


    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    ${elasticsearch.version}



    org.elasticsearch.client
    elasticsearch-rest-client
    ${elasticsearch.version}



    org.elasticsearch.client
    elasticsearch-rest-client-sniffer
    ${elasticsearch.version}

클라이언트 탐지기 코드
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.sniff.SniffOnFailureListener;
import org.elasticsearch.client.sniff.Sniffer;
public class EsClientFactory {
    private  String host = "127.0.0.1";
    private  String scheme="http";
    private  int port=9200;
    private  int maxRetryTimeout=30000;
    private  int connectTimeOut = 2000;
    private  int socketTimeOut = 15000;
    private  int connectionRequestTimeOut = 500;
    private  int maxConnectNum = 100;
    private  int maxConnectPerRoute = 100;
    private  HttpHost[] httpHosts;
    private  boolean uniqueConnectTimeConfig = true;
    private  boolean uniqueConnectNumConfig = true;
    private boolean  enableSniff=true;
    private  RestClientBuilder builder;
    private Sniffer sniffer = null;
    private RestClient restClient = null;
    private volatile RestHighLevelClient highClient;
    private static volatile EsClientFactory esClientFactory;

    private EsClientFactory() {

    }

    public static EsClientFactory getInstance() {
        if (esClientFactory == null) {
            synchronized (EsClientFactory.class) {
                if (esClientFactory == null) {
                    esClientFactory = new EsClientFactory();
                    esClientFactory.init();
                }
            }
        }
        return esClientFactory;
    }

    /**
     *  builder
     * @return
     */
    public RestClientBuilder init() {
        String[] hosts = host.split(",");
        httpHosts = new HttpHost[hosts.length];
        for (int i = 0; i < hosts.length; i++) {
            httpHosts[i] = new HttpHost(hosts[i], port, scheme);
        }

        builder = RestClient.builder(httpHosts);

        Header[] defaultHeaders = new Header[] {
                new BasicHeader("Content-Type", "application/json")
        };
        builder.setDefaultHeaders(defaultHeaders);
        builder.setMaxRetryTimeoutMillis(maxRetryTimeout);

        if (uniqueConnectTimeConfig) {
            setConnectTimeOutConfig();
        }

        if (uniqueConnectNumConfig) {
            setMutiConnectConfig();
        }

        restClient = builder.build();

        // 
        if(enableSniff){
            SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
            builder.setFailureListener(sniffOnFailureListener);
            sniffer = Sniffer.builder(restClient).setSniffIntervalMillis(60000).setSniffAfterFailureDelayMillis(30000).build();
            sniffOnFailureListener.setSniffer(sniffer);
        }
        return builder;
    }

    /**
     *  api
     * @return
     */
    public RestHighLevelClient getHighLevelClient() {
        if (highClient == null) {
            synchronized (EsClientFactory.class) {
                if (highClient == null) {
                    highClient = new RestHighLevelClient(builder);
                }
            }
        }
        return highClient;
    }

    /**
     *  api
     * @return
     */
    public RestClient getLowLevelClient() {
        return restClient;
    }

    /**
     *  httpclient 
     */
    public void setConnectTimeOutConfig() {
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });
    }


    /**
     *  httpclient 
     */
    public void setMutiConnectConfig() {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
    }

    /**
     *  
     */
    public void close() {
        IOUtils.closeQuietly(restClient);
        IOUtils.closeQuietly(highClient);
    }

ES 고급 및 하위 클라이언트 가져오기
/**
 *  api
 * @return
 */
public static RestHighLevelClient getEsHighClient() {
    return EsClientFactory.getInstance().getHighLevelClient();
}

/**
 *  api
 * @return
 */
public static RestClient getEsLowClient() {
    return EsClientFactory.getInstance().getLowLevelClient();
}

좋은 웹페이지 즐겨찾기