ES学习笔记之---从源码启动ES

开发中须要用到ES的插件, 开发ES插件须要了解ES的内部结构, 因而再次开始学习ES的源码。一方面了解插件开发的套路,一方面了解get接口的实现细节。git

了解ES的原理,源码是文档最好的补充。源码甚至比文档更有助于了解ES的内部核心。github

首先从git上clone下源码:编程

git clone https://github.com/elastic/elasticsearch.git

cd elasticsearch

git tag -l

git checkout v2.4.5

sh run.sh

若是使用run.sh没有成功,再试一次, 有多是maven的jar包没有下载到。
这里使用v2.4.5是因为在编译es的过程当中会用到相关的jar包,而https://oss.sonatype.org/content/repositories/snapshots/org/elasticsearch/rest-api-spec/并非全部版本的jar包都有, 因此从中选取了v2.4.5, 这跟手机选号同样,纯属我的主观。api

这里JDK要换成1.8, 1.7的jdk maven会报protocol_verson错误。
编译成功后,就会生成elasticsearch的zip包, 须要解压,由于源码中会用到conf文件。curl

cd /home/shgy/es_workspace/elasticsearch/distribution/zip/target/releases/
unzip elasticsearch-2.4.5-SNAPSHOT.zip 
mv elasticsearch-2.4.5-SNAPSHOT /opt/

编译完成后, 将源码import到intellij中, intellij的启动参数elasticsearch

vm options : 

-Des.path.home=/opt/elasticsearch-2.4.5-SNAPSHOT

Program arguments: 

start

启动成功后使用maven

curl http://localhost:9200

便可看到经典的ide

{
  "name" : "Ruckus",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "ZIl7g86YRiGv8Dqz4DCoAQ",
  "version" : {
    "number" : "2.4.5",
    "build_hash" : "c849dd13904f53e63e88efc33b2ceeda0b6a1276",
    "build_timestamp" : "2018-08-12T01:30:55Z",
    "build_snapshot" : true,
    "lucene_version" : "5.5.4"
  },
  "tagline" : "You Know, for Search"
}

从源码启动成功后, 能够作的事情就多了。 好比看看You Know, for Search是怎么来的;看看ES内部的index/get/search等接口内部是如何运行的。
更重要的是, 能够将相关接口的逻辑套用, 依样画葫芦开发plugin实现本身的业务逻辑。学习

以debug的方式启动es后, 第一个断点能够打在org.elasticsearch.http.netty.HttpRequestHandler.messageReceived(),这是netty的编程模式。ui

好比You Know, for Search, 经过debug, 能够了解到其调用链为:
HttpRequestHandler.messageReceived() --- RestMainAction.handleRequest()

Rest_xxx_Action是es全部http接口通用的套路。好比:

RestSearchAction    _search
RestGetAction    /{index}/{type}/{id}
RestIndexAction    /{index}/{type}/{id} 
......

能够说, Rest_xxx_Action是es的外壳, 整个ES的结构大体以下图所示:
ES学习笔记之---从源码启动ES

相关文章
相关标签/搜索