使用java创建ES(ElasticSearch)连接池

1.首先要有一个创建连接的工厂类

package com.aly.util;


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.RestHighLevelClient;

/**
 * EliasticSearch连接池工厂对象
 * @author 00000
 *
 */
public class EsClientPoolFactory implements PooledObjectFactory<RestHighLevelClient>{

	@Override
	public void activateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
		System.out.println("activateObject");
		
	}
	
	/**
	 * 销毁对象
	 */
	@Override
	public void destroyObject(PooledObject<RestHighLevelClient> pooledObject) throws Exception {
		RestHighLevelClient highLevelClient = pooledObject.getObject();
		highLevelClient.close();
	}
	
	/**
	 * 生产对象
	 */
//	@SuppressWarnings({ "resource" })
	@Override
	public PooledObject<RestHighLevelClient> makeObject() throws Exception {
//		Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
		RestHighLevelClient client = null;
		try {
			/*client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));*/
			client = new RestHighLevelClient(RestClient.builder(
					new HttpHost("192.168.1.121", 9200, "http"), new HttpHost("192.168.1.122", 9200, "http"),
					new HttpHost("192.168.1.123", 9200, "http"), new HttpHost("192.168.1.125", 9200, "http"),
					new HttpHost("192.168.1.126", 9200, "http"), new HttpHost("192.168.1.127", 9200, "http")));

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DefaultPooledObject<RestHighLevelClient>(client);
	}

	@Override
	public void passivateObject(PooledObject<RestHighLevelClient> arg0) throws Exception {
		System.out.println("passivateObject");
	}

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

2.然后再写我们的连接池工具类

package com.aly.util;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * ElasticSearch 连接池工具类
 * 
 * @author 00000
 *
 */
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<RestHighLevelClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,
			poolConfig);

	/**
	 * 获得对象
	 * 
	 * @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);
	}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于在Spring Boot中使用Elasticsearch连接池,你可以使用Spring Data Elasticsearch提供的自动配置功能来实现。 首先,确保在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 接下来,在`application.properties`或`application.yml`文件中配置Elasticsearch连接信息,例如: ```properties spring.data.elasticsearch.cluster-nodes=localhost:9200 spring.data.elasticsearch.cluster-name=my-cluster ``` 然后,创建一个Elasticsearch配置类,例如`ElasticsearchConfig.java`: ```java import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; @Configuration public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { @Value("${spring.data.elasticsearch.cluster-nodes}") private String clusterNodes; @Override @Bean public RestHighLevelClient elasticsearchClient() { final ClientConfiguration clientConfiguration = ClientConfiguration.builder() .connectedTo(clusterNodes) .build(); return RestClients.create(clientConfiguration).rest(); } } ``` 最后,你可以在你的服务类或控制器中注入`RestHighLevelClient`来使用Elasticsearch连接池,例如: ```java import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final RestHighLevelClient elasticsearchClient; @Autowired public MyService(RestHighLevelClient elasticsearchClient) { this.elasticsearchClient = elasticsearchClient; } // 使用elasticsearchClient进行Elasticsearch操作 } ``` 这样就完成了Spring Boot与Elasticsearch连接池配置使用。你可以根据自己的需求进行进一步的配置和操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值