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();
static {
poolConfig.setMaxTotal(8);
}
private static EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory();
private static GenericObjectPool<PreBuiltTransportClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,
poolConfig);
public static PreBuiltTransportClient getClient(){
try{
PreBuiltTransportClient client = clientPool.borrowObject();
return client;
}catch (Exception e){
throw new KYException("獲取ES連線失敗");
}
}
public static void returnClient(PreBuiltTransportClient client) {
clientPool.returnObject(client);
}
}