RestHighLevelClient工厂方法

RestHighLevelClient工厂方法

虽然最终被弃用,但是记录下留个纪念

ElasticSearchPoolUtil

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * ElasticSearch 连接池工具类
 */
@Slf4j
@Component
public class ElasticSearchPoolUtil {
    @Autowired
    ElasticsearchClientPoolFactory esClientPoolFactory;

    private static GenericObjectPool<RestHighLevelClient> clientPool;

    @PostConstruct
    public void init() {
        // 对象池配置类,不写也可以,采用默认配置
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        // 利用对象工厂类和配置类生成对象池
        clientPool = new GenericObjectPool<>(esClientPoolFactory, poolConfig);
        clientPool.setMaxTotal(10);
    }

    /**
     * 获得对象
     *
     * @return
     * @throws Exception
     */
    public static RestHighLevelClient getClient() throws Exception {
        // 从池中取一个对象
        RestHighLevelClient client = clientPool.borrowObject();
        return client;
    }

    /**
     * 归还对象
     *
     * @param client
     */
    public static void returnClient(RestHighLevelClient client) {
        // 使用完毕之后,归还对象
        clientPool.returnObject(client);
    }

    /**
     * 销毁对象
     *
     * @param client
     */
    public static void destroyClient(RestHighLevelClient client) {
        try {
            // 发生异常之后,销毁对象
            clientPool.invalidateObject(client);
        } catch (Exception e) {
            log.error("ElasticSearchPoolUtil.destroyClient error:", e);
        }
    }
}

ElasticsearchClientPoolFactory

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * EliasticSearch连接池工厂对象
 *
 */
@Slf4j
@Component
public class ElasticsearchClientPoolFactory implements PooledObjectFactory<RestHighLevelClient> {
    @Autowired
    private ElasticsearchMonitorProperties esProperties;

    @Override
    public void activateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {

    }

    /**
     * 销毁对象
     */
    @Override
    public void destroyObject(PooledObject<RestHighLevelClient> pooledObject) throws Exception {
        RestHighLevelClient highLevelClient = pooledObject.getObject();
        highLevelClient.close();
    }

    /**
     * 生产对象
     */
    @Override
    public PooledObject<RestHighLevelClient> makeObject() {
        RestHighLevelClient restHighLevelClient = null;
        try {
            String[] urlArr = esProperties.getEsAddress().split(",");
            HttpHost[] httpPostArr = new HttpHost[urlArr.length];
            for (int i = 0; i < urlArr.length; i++) {
                HttpHost httpHost = new HttpHost(urlArr[i].split(":")[0].trim(),
                        Integer.parseInt(urlArr[i].split(":")[1].trim()), "http");
                httpPostArr[i] = httpHost;
            }
            RestClientBuilder builder = RestClient.builder(httpPostArr);

            // 异步httpclient连接延时配置
            builder.setRequestConfigCallback(requestConfigBuilder -> {
                requestConfigBuilder.setConnectTimeout(esProperties.getConnectTimeOut());
                requestConfigBuilder.setSocketTimeout(esProperties.getSocketTimeOut());
                requestConfigBuilder.setConnectionRequestTimeout(esProperties.getConnectionRequestTimeOut());
                return requestConfigBuilder;
            });

            // 异步httpclient连接数配置
            builder.setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.setMaxConnTotal(esProperties.getMaxConnectNum());
                httpClientBuilder.setMaxConnPerRoute(esProperties.getMaxConnectPerRoute());
                return httpClientBuilder;
            });
            restHighLevelClient = new RestHighLevelClient(builder);
        } catch (Exception e) {
            log.error("ElasticsearchClientPoolFactory.makeObject error:", e);
        }
        return new DefaultPooledObject<RestHighLevelClient>(restHighLevelClient);
    }

    @Override
    public void passivateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
    }

    @Override
    public boolean validateObject(PooledObject<RestHighLevelClient> arg0) {
        return true;
    }
}

你可能感兴趣的:(elasticsearch,elasticsearch)