Java Client连接Elasticsearch集群,如果用单例模式或者SpringBean(本质也是单例模式),若项目多个模块都要使用Client进行增删改查,就需要进行池化。
利用commons-pool2可以自定义对象池,基于此,可以实现线程池的方式连接Elasticsearch集群
1. commons-pool2
以下博客,对commons-pool2进行介绍,基本上可以理解并使用
2.基于commons-pool2和TransportClient的ElasticSearch连接池
2.1定义TransportClientNodeConfig
定义节点的配置信息
/**
* 节点名称
*/
private String nodeName;
/**
* 节点Host
*/
private String host ;
/**
* 节点端口号
*/
private int port ;
2.2定义TransportClientPoolConfig
定义线程池的配置信息
/**
*连接时间
*/
public static final String CLUSTER_NAME = ConfigUtils.getEsClusterName();
/**
*自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
*/
public static final String CLUSTER_CLIENT_TRANSPORTSNIFF = ConfigUtils.getClientTransportSniff();
/**
*连接时间
*/
private static final String CONNECTTIMEMILLIS=ConfigUtils.getConnectTimeMillis();
/**
*连接池最大值
*/
private static final String MAXTOTAL=ConfigUtils.getMaxTotal();
/**
*最大空闲
*/
private static final String MAXIDLE=ConfigUtils.getMaxIdle();
/**
*最小空闲
*/
private static final String MINIDLE=ConfigUtils.getMinIdle();
/**
* es 集群对应的节点
*/
Set<TransportClientNodeConfig> nodes = getNodesConfig();
2.3定义TransportClientPool
组合节点配置信息和线程池配置信息
/**
* es集群名称
*/
private TransportClientPoolConfig transportClientPoolConfig;
/**
* es 集群对应的节点
*/
private AtomicReference<Set<TransportClientNodeConfig>> nodeSetsReference;
public TransportClientPool(AtomicReference<Set<TransportClientNodeConfig>> nodesReference , TransportClientPoolConfig transportClientPoolConfig) {
super(transportClientPoolConfig, new TransportClientFactory(nodesReference, transportClientPoolConfig));
this.transportClientPoolConfig = transportClientPoolConfig;
this.nodeSetsReference = nodesReference;
}
2.4定义TransportClientFactory
构建节点连接集群信息
/**
* es集群节点设置
*/
private AtomicReference<Set<TransportClientNodeConfig>> nodesReference = new AtomicReference<Set<TransportClientNodeConfig>>();
/**
* es 集群连接池配置
*/
private TransportClientPoolConfig transportClientPoolConfig;
public TransportClientFactory(AtomicReference<Set<TransportClientNodeConfig>> nodesReference, TransportClientPoolConfig transportClientPoolConfig) {
this.nodesReference = nodesReference;
this.transportClientPoolConfig = transportClientPoolConfig;
}
public PooledObject<TransportClient> makeObject() throws ElasticSearchHostException {
HttpHost[] nodes = new HttpHost[nodesReference.get().size()];
Settings settings = Settings.builder()
/*设置ES实例的名称*/
.put("cluster.name", TransportClientPoolConfig.getClusterName())
/*自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中*/
.put("client.transport.sniff", TransportClientPoolConfig.getClusterClientTransportsniff())
.build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
for (TransportClientNodeConfig each : nodesReference.get()) {
try {
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(each.getHost()), each.getPort()));
} catch (UnknownHostException e) {
throw new ElasticSearchHostException("host exception ");
}
}
return new DefaultPooledObject(transportClient);
}
github代码:https://github.com/codersfarm/elasticsearch/tree/master/elasticsearch5.x