【常用功能】common-pool2自定义ES连接池

背景:使用common-pool2框架手写连接池(例如es连接池),提升创建es连接的性能

以es连接池为例,代码见 上传资源->util工具类->es连接池 文件夹

一、maven引入common-pool2依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>

二、创建一个池类,这个池通过依赖的方式引入commons-pool2中的GenericObjectPool。在这个类中,我们定义了如何从池中借对象和返回对象

Pool.java

public T getResource(){
    try {
        return internalPool.borrowObject();
    } catch (Exception e) {
        log.info("Could not get a resource from the pool", e);
    }
    return null;
}


public void returnResource(final T resource){
    if (resource != null) {
        returnResourceObject(resource);
    }
}

三、创建ES的连接池,继承池类

ElasticSearchPool.java

private String clusterName;
private HostAndPort hostAndPort;

public ElasticSearchPool(ElasticSearchPoolConfig config,HostAndPort hostAndPort){
    super(config, new ElasticSearchClientFactory(config.getClusterName(), hostAndPort));
    this.clusterName = config.getClusterName();
    this.hostAndPort = hostAndPort;
}

四、定义了一个ES的连接池配置类用于配置连接池的属性,在apache提供的commons-pool2中已经提供了一个池基本属性配置的类GenericObjectPoolConfig,我们可以直接继承此类

ElasticSearchPoolConfig.java

/**
*有很多配置项,这里只选择两种进行展示
*/
//超时时间
private long connectTimeMillis;
//集群名称
private String clusterName;

五、ES连接属性配置类

HostAndPort.java

//ip
private String host ;
//端口
private int port ;
//连接协议
private String schema;

public HostAndPort(String host, int port, String schema) {
    this.host = host;
    this.port = port;
    this.schema = schema;
}

//使用如下
HostAndPort hostAndPort = new HostAndPort("172.31.133.21",19200,"http");

六、最后,我们还需要做的是给这个池提供一个工厂类,用于创建池中的对象和回收对象,我们只要实现PooledObjectFactory接口并实现接口中的makeObject()和destroyObject()方法即可

ElasticSearchClientFactory.java

private HostAndPort hostAndPort;

private String clusterName;

public ElasticSearchClientFactory(String clusterName, HostAndPort hostAndPort){
    this.clusterName = clusterName;
    this.hostAndPort = hostAndPort;
}

@Override
public PooledObject<RestClient> makeObject() throws Exception {
    RestClient client = RestClient.builder(new HttpHost(hostAndPort.getHost(),hostAndPort.getPort(),hostAndPort.getSchema())).build();
    return new DefaultPooledObject<>(client);
}

@Override
public void destroyObject(PooledObject<RestClient> pooledObject) throws Exception {
    RestClient client = pooledObject.getObject();
    if(client!=null){
        try {
            client.close();
        }catch (Exception e){
            //ignore
        }
    }
}

七、验证


public class ElasticSearchPoolTest {
 
    public static void main(String[] args) throws Exception {
        HostAndPort hostAndPort = new HostAndPort("172.31.133.21",19200,"http");
        ElasticSearchPoolConfig config = new ElasticSearchPoolConfig();
        //超时时间
        config.setConnectTimeMillis(8000);
        //最大连接
        config.setMaxTotal(10);
        //集群名称
        config.setClusterName("idc_es");
        ElasticSearchPool pool = new ElasticSearchPool(config,hostAndPort);
        
        for(int i=0;i<100;i++){
            RestClient client = (RestClient)pool.getResource();
            System.out.println(client.toString());
            pool.returnResource(client);
        }
    }
 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值