Elasticsearch Java Client连接池
按照Elasticsearch API,在Java端使用是ES服务需要创建Java Client,但是每一次连接都实例化一个client,对系统的消耗很大,即使在使用完毕之后将client close掉,由于服务器不能及时回收socket资源,极端情况下会导致服务器达到最大连接数。
为了解决上述问题并提高client利用率,可以参考使用池化技术复用client。
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.Mapping;
import org.elasticsearch.transport.client.PreBuiltTransportClient; import com.chinadigitalvideo.esagent.servlet.WebServiceInit; /**
* Created by tgg on 16-3-17.
*/
public class ClientHelper {
private static String ip;
private static int port; private Settings setting;
private Mapping mapping; private Map<String, Client> clientMap = new ConcurrentHashMap<String, Client>(); private Map<String, Integer> ips = new HashMap<String, Integer>(); // hostname port private String clusterName = WebServiceInit.clusterName; private ClientHelper(String ip,Integer port) {
init(ip,port);
//TO-DO 添加你需要的client到helper
} public static final ClientHelper getInstance(String ipConf ,Integer portConf) {
ip=ipConf;
port=portConf;
return ClientHolder.INSTANCE;
} private static class ClientHolder {
private static final ClientHelper INSTANCE = new ClientHelper(ip,port);
} /**
* 初始化默认的client
*/
public void init(String ip,int port) { ips.put(ip, port);
setting =Settings.builder()
.put("client.transport.sniff",true)
.put("cluster.name",clusterName).build();
addClient(setting, getAllAddress(ips));
} /**
* 获得所有的地址端口
*
* @return
*/
public List<InetSocketTransportAddress> getAllAddress(Map<String, Integer> ips) {
List<InetSocketTransportAddress> addressList = new ArrayList<InetSocketTransportAddress>();
for (String ip : ips.keySet()) {
addressList.add(new InetSocketTransportAddress(new InetSocketAddress(ip, ips.get(ip))));
}
return addressList;
} public Client getClient() {
return getClient(clusterName);
} public Client getClient(String clusterName) {
return clientMap.get(clusterName);//通过集群名称得到一个Client
} public void addClient(Settings setting, List<InetSocketTransportAddress> transportAddress) {
Client client = new PreBuiltTransportClient(setting)
.addTransportAddresses(transportAddress.toArray(new InetSocketTransportAddress[transportAddress.size()])); clientMap.put(setting.get("cluster.name"), client);
}
}
Elasticsearch Java Client连接池的更多相关文章
- Java Mysql连接池配置和案例分析--超时异常和处理
前言: 最近在开发服务的时候, 发现服务只要一段时间不用, 下次首次访问总是失败. 该问题影响虽不大, 但终究影响用户体验. 观察日志后发现, mysql连接因长时间空闲而被关闭, 使用时没有死链检测 ...
- Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...
- java自定义连接池
1.java自定义连接池 1.1连接池的概念: 实际开发中"获取连接"或“释放资源”是非常消耗系统资源的两个过程,为了姐姐此类性能问题,通常情况我们采用连接池技术来贡献连接Conn ...
- MySQL_(Java)【连接池】使用DBCP简单模拟银行转账事物
dbcp下载 传送门 Commons Pool下载 传送门 Commons log下载 传送门 MySQL_(Java)[事物操作]使用JDBC模拟银行转账向数据库发起修改请求 传送门 MySQL_( ...
- MySQL_(Java)【连接池】简单在JDBCUtils.java中创建连接池
MySQL_(Java)[事物操作]使用JDBC模拟银行转账向数据库发起修改请求 传送门 MySQL_(Java)[连接池]使用DBCP简单模拟银行转账事物 传送门 Java应用程序访问数据库的过程: ...
- Redis c/c++, java client连接
Redis 介绍 redis这个想必大家都了解,关于redis的安装參考这里,redis使用文档參见这里,英文文档. Redis Cclient的用法 Redis的cclient Hiredis使用比 ...
- elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...
- elasticsearch Java Client用户指南
这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是5.1.2,否则一些功能可能不支持. 之前介绍过Spring Data Elasticsearch,那里也是使用了本文 ...
- Java c3p0连接池
import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; i ...
随机推荐
- 1, 2, and 4 symbols per clock中数据排列
图片来自High-DenitionMultimedia Interface (HDMI) IP Core User Guide 在自己处理的过程中很多细节的东西必须要清楚. 今天想自己从RGB数据中 ...
- MIT Molecular Biology 笔记5 转录机制
视频 https://www.bilibili.com/video/av7973580?from=search&seid=16993146754254492690 教材 Molecular ...
- java的静态内部类
只是一个简单的记录.因为一直排斥java这个东西.java跟c++比是很不错的一个语言,至少内存管理这么麻烦的东西不用操心了.但是和不断崛起的脚本语言比起来,效率差的太多.无论如何做android还是 ...
- C++之引用和指针
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7050431.html C++之引用和指针 C++引用 引用的基本用法: in ...
- 20155326 2016-2017-2 《Java程序设计》第九周学习总结
20155326 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 1.撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找. 2.JDBC目的 ...
- PAT甲级 1127. ZigZagging on a Tree (30)
1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Hdu3829 Cat VS Dog(最大独立点集)
Cat VS Dog Problem Description The zoo have N cats and M dogs, today there are P children visiting t ...
- [mysql]当mysql查询语句查询的结果为空时,返回query结果是什么类型的呢?
php > $con = mysql_connect('localhost' , 'hnb' , 'alyHnb2015'); php > print_r($con);Resource i ...
- 根据cxgrid的filterControl建立强大灵活的过滤器
- ios 百度地图,火星坐标,地球坐标互转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...