ElasticSearch連線池建立

itdaima部落格發表於2020-12-18

ElasticSearch連線池建立

建立ElasticSearch連線

package com.kingyea.common.data.utils;

import com.kingyea.boot.exception.KYException;
import com.kingyea.common.data.config.ConfigIni;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;

public class EsClientPoolFactory implements PooledObjectFactory<PreBuiltTransportClient>{

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

    @Override
    public void activateObject(PooledObject<PreBuiltTransportClient> arg0) {
        System.out.println("activateObject");
    }
    /**
     * 銷燬物件
     */
    @Override
    public void destroyObject(PooledObject<PreBuiltTransportClient> pooledObject){
        try{
            PreBuiltTransportClient preBuiltTransportClient = pooledObject.getObject();
            preBuiltTransportClient.close();
        }catch (Exception e){}
    }
    /**
     * 生產物件
     */
    @Override
    public PooledObject<PreBuiltTransportClient> makeObject(){

        PreBuiltTransportClient client;
        try {
            String ip = ConfigIni.getEsProperty("ip");
            String clusterName = ConfigIni.getEsProperty("cluster.name");
            int port = Integer.parseInt(ConfigIni.getEsProperty("port"));
            LOG.info("clusterName {}", clusterName);
            Settings settings = Settings.builder()
                    .put("cluster.name", clusterName)
                    .put("client.transport.sniff", true)
                    .build();
            client = new PreBuiltTransportClient(settings);
            String[] esIps = ip.split(",");
            client.addTransportAddresses(new TransportAddress(InetAddress.getByName(esIps[0]), port));
            LOG.info("client {}", client);
        } catch (Exception e) {
            LOG.info("ES連線錯誤,請檢查配置", e);
            throw new KYException("ES連線錯誤,請檢查配置");
        }
        return new DefaultPooledObject<PreBuiltTransportClient>(client);
    }

    @Override
    public void passivateObject(PooledObject<PreBuiltTransportClient> arg0){
        LOG.info("passivateObject");
     }

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

}

ElasticSearch連線池物件配置

package com.kingyea.common.data.utils;

import com.kingyea.boot.exception.KYException;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticSearchPoolUtil {

    // 物件池配置類,不寫也可以,採用預設配置
    private static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

    // 採用預設配置maxTotal是8,池中有8個client
    static {
        poolConfig.setMaxTotal(8);
    }
    // 要池化的物件的工廠類,這個是我們要實現的類
    private static EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory();
    // 利用物件工廠類和配置類生成物件池
    private static GenericObjectPool<PreBuiltTransportClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,
            poolConfig);

    /**
     * 獲得物件
     *
     * @return
     * @throws Exception
     */
    public static PreBuiltTransportClient getClient(){
        try{
            // 從池中取一個物件
            PreBuiltTransportClient client = clientPool.borrowObject();
            return client;
        }catch (Exception e){
            throw new KYException("獲取ES連線失敗");
        }
    }

    /**
     * 歸還物件
     *
     * @param client
     */
    public static void returnClient(PreBuiltTransportClient client) {
        // 使用完畢之後,歸還物件
        clientPool.returnObject(client);
    }

}

相關文章