本文主要介绍ElasticSearch冷热分离架构以及实现。html
冷热分离是目前ES很是火的一个架构,它充分的利用的集群机器的优劣来实现资源的调度分配。ES集群的索引写入及查询速度主要依赖于磁盘的IO速度,冷热数据分离的关键点为使用固态磁盘存储数据。若所有使用固态,成本太高,且存放冷数据较为浪费,于是使用普通机械磁盘与固态磁盘混搭,可作到资源充分利用,性能大幅提高的目标。所以咱们能够将实时数据(5天内)存储到热节点中,历史数据(5天前)的存储到冷节点中,而且能够利用ES自身的特性,根据时间将热节点的数据迁移到冷节点中,这里由于咱们是按天创建索引库,所以数据迁移会更加的方便。java
架构图:
node
使用冷热分离的时候,咱们须要将索引库创建在热节点中,等到必定的时间时间在将该索引库迁移冷节点中。所以这里咱们须要更加热节点的量来进行设置分片数。
好比,咱们拥有6个热节点,9个冷节点,索引库的主分片的数据量500G左右,那么该索引库创建18个分片而且都在在热节点中,此时该索引库的分片的分布是,热节点:18,冷节点0;等到该数据不是热数据以后,将该索引库的分片所有迁移到冷节点中,索引库的分片的分布是, 热节点:0,冷节点18。git
单个索引库热冷节点分片分布示例:github
时间 | 索引库名称 | 热节点分片数量 | 冷节点分片数量 |
---|---|---|---|
20190707 | TEST_20190703 | 18 | 0 |
20190708 | TEST_20190703 | 0 | 18 |
最终实现效果图,这里我用cerebro界面截图来表示
cerebro示例图:
写入ES索引库中,分片分布在热节点中
过了一段时间以后进行了迁移,分片数据迁移到了冷节点:
bootstrap
更多ElasticSearch的相关介绍能够查看个人这篇博文:http://www.javashuo.com/article/p-tehpwwqw-er.html服务器
ElasticSearch冷热分离架构是一种思想,其实现原理是使用ElasticSearch的路由完成,在data节点设置对应的路由,而后在建立索引库时指定分布到那些服务器,过一段时间以后,根据业务要求在将这些索引库的数据进行迁移到其余data节点中。架构
这里须要改变的节点为data节点,其余的节点配置无需更改。这里我就用之前写的ElasticSearch实战系列一: ElasticSearch集群+Kibana安装教程里面的配置进行更改。app
data节点的elasticsearch.yml原配置:elasticsearch
cluster.name: pancm node.name: data1 path.data: /home/elk/datanode/data path.logs: /home/elk/datanode/logs network.host: 0.0.0.0 network.publish_host: 192.169.0.23 transport.tcp.port: 9300 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"] node.master: false node.data: true node.ingest: false index.number_of_shards: 5 index.number_of_replicas: 1 discovery.zen.minimum_master_nodes: 1 bootstrap.memory_lock: true http.max_content_length: 1024mb
相比普通的data节点, 主要是增长了这两个配置:
node.attr.rack: r1 node.attr.box_type: hot
热节点配置示例:
cluster.name: pancm node.name: data1 path.data: /home/elk/datanode/data path.logs: /home/elk/datanode/logs network.host: 0.0.0.0 network.publish_host: 192.169.0.23 transport.tcp.port: 9300 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"] node.master: false node.data: true node.ingest: false index.number_of_shards: 5 index.number_of_replicas: 1 discovery.zen.minimum_master_nodes: 1 bootstrap.memory_lock: true http.max_content_length: 1024mb node.attr.rack: r1 node.attr.box_type: hot
冷节点配置大致相同,就是后面的值进行更改
node.attr.rack: r9 node.attr.box_type: cool
冷节点配置示例:
cluster.name: pancm node.name: data1 path.data: /home/elk/datanode/data path.logs: /home/elk/datanode/logs network.host: 0.0.0.0 network.publish_host: 192.169.0.23 transport.tcp.port: 9300 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.169.0.23:9301","192.169.0.24:9301","192.169.0.25:9301"] node.master: false node.data: true node.ingest: false index.number_of_shards: 5 index.number_of_replicas: 1 discovery.zen.minimum_master_nodes: 1 bootstrap.memory_lock: true http.max_content_length: 1024mb node.attr.rack: r1 node.attr.box_type: hot
在建立索引库的时候须要指定默认索引库的分片归属,若是没有指定,就会根据ElasticSearch默认进行均匀分布。这里咱们将索引库默认建立到hot节点中,知足业务条件以后在使用命令或代码将该索引库设置到冷节点中。
索引示例:
PUT TEST_20190717 { "index":"TEST_20190717", "settings": { "number_of_shards" :18, "number_of_replicas" : 1, "refresh_interval" : "10s", "index.routing.allocation.require.box_type":"hot" }, "mappings": { "mt_task_hh": { "properties": { "accttype": { "type": "byte" }, .... } }
根据业务要求,咱们能够对索引库的数据进行迁移,使用dsl语句在kibana上执行或者使用java代码实现均可以。
dsl语句:
PUT TEST_20190717/_settings { "index.routing.allocation.require.box_type":"cool" }
java代码实现:
public static void setCool(String index) throws IOException { RestClient restClient = null; try { Objects.requireNonNull(index, "index is not null"); restClient = client.getLowLevelClient(); String source = "{\"index.routing.allocation.require.box_type\": \"%s\"}"; source = String.format(source, "cool"); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); restClient.performRequest("PUT", "/" + index + "/_settings", Collections.<String, String>emptyMap(), entity); } catch (IOException e) { throw e; } finally { if (restClient != null) { restClient.close(); } } }
完整代码地址: https://github.com/xuwujing/java-study/tree/master/src/main/java/com/pancm/elasticsearch
其实这篇文章本应来讲在2019年就完成了初稿,可是由于其余的事情一直耽搁,好在查看草稿是发现了,因而便补了。由于时隔过久,细节上相比以前的文章有必定的差距。不过好在是写出来了,之后的话写文章的话仍是尽早,否则后面就忘了。目前ElasticSearch实战系列已经写了10篇了,虽然中间的间隔有点久,后面我会慢慢的更新这个系列,尽可能把本身所学所感悟的写出来,若有写的很差,但愿可以指出讨论。
原创不易,若是感受不错,但愿给个推荐!您的支持是我写做的最大动力!
版权声明:
做者:虚无境
博客园出处:http://www.cnblogs.com/xuwujing
CSDN出处:http://blog.csdn.net/qazwsxpcm
掘金出处:https://juejin.im/user/5ae45d5bf265da0b8a6761e4
我的博客出处:http://www.panchengming.com