日期作者版本备注
2022-10-03dingbinthu@163.comv1.0

本文讲述在centos 7操作系统上从无到有实现IDEA+Kibana+elasticsearch本地调试elaticsearch源码的操作过程。

1. Git

稳当起见,降级git到比拟新的版本,如git version 2.24.1。

在linux执行git clone github上开源代码会产生卡死或半天没反馈等景象,个别设置如下即可:

git config --global --unset https.proxy

git config --global --unset http.proxy

git config --global http.sslVerify "false"

git config --global http.postBuffer 524288000 #最管用的

2. 下载Elasticsearch源码

本文调试的是Elasticsearch 7.10.2版本,留神后文kibana抉择肯定要与此版本完全一致才行。

mkdir es-source-dir

cd es-source-dir

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

cd elasticsearch-7.10.git

git branch -a | grep 7.

git tag | grep 7.10

git checkout v7.10.2

源码门路根门路记作$es_code_root,不便下文应用

3.设置 Jdk

cd $es_code_root

vim CONTRIBUTING.md 浏览得悉此版本的elasticsearch运行须要jdk14

vim ~/.bashrc

export JAVA_HOME=/path/to/jdk-14.0.2

稳当起见,咱们将 jdk1.8也装置上,后续测试可能用的着。

4. 筹备Maven

wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz --no-check-certificate

export M2_HOME=/opt/chilly/app/apache-maven-3.8.6

mvn --version

settings.xml 文件个别存在于两个地位:

全局配置: ${maven.home}/conf/settings.xml
用户配置: ${user.home}/.m2/settings.xml
留神:用户配置优先于全局配置。${user.home} 和所有其余零碎属性只能在 3.0+版本上应用。请留神 windows 和 Linux 应用变量的区别。

配置优先级:部分配置优先于全局配置。

配置优先级从高到低:pom.xml > user settings > global settings

如果这些文件同时存在,在利用配置时,会合并它们的内容,如果有反复的配置,优先级高的配置会笼罩优先级低的。

1) vim ${M2_HOME}/conf/settings.xml

Default: ${user.home}/.m2/repository

<localRepository>/path/to/local/repo</localRepository>

2) vim ~/.m2/settings.xml

3) project pom.xml

settings.xml 重点要留神的是批改localRepository 为理论心愿下载存储jar包的中央。

<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">    <!-- 默认的值是${user.home}/.m2/repository -->    <localRepository>E:\apache\repository</localRepository>    <!-- 如果Maven要试图与用户交互来失去输出就设置为true,否则就设置为false,默认为true。 -->    <interactiveMode>true</interactiveMode>    <!-- 如果Maven应用${user.home}/.m2/plugin-registry.xml来治理plugin的版本,就设置为true,默认为false。 -->    <usePluginRegistry>false</usePluginRegistry>    <!-- 如果构建零碎要在离线模式下工作,设置为true,默认为false。 如果构建服务器因为网络故障或者平安问题不能与近程仓库相连,那么这个设置是十分有用的。 -->    <offline>false</offline>    <mirrors>        <mirror>            <id>aliyunmaven</id>            <mirrorOf>*</mirrorOf><!--*代表所有的jar包都到阿里云下载-->            <!--<mirrorOf>central</mirrorOf>--><!--central代表只有地方仓库的jar包才到阿里云下载-->            <name>阿里云公共仓库</name>            <url>https://maven.aliyun.com/repository/public</url>        </mirror>        <!--老版阿里云公共仓库-->        <!--        <mirror>            <id>nexus-aliyun</id>            <mirrorOf>central</mirrorOf>            <name>Nexus aliyun</name>            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>        </mirror>        -->                <!-- 地方仓库在中国的镜像 -->        <mirror>            <id>maven.net.cn</id>            <name>Mirror from Maven in china</name>            <url>http://maven.net.cn/content/groups/public/</url>            <mirrorOf>central</mirrorOf>        </mirror>    </mirrors>    <!-- settings.xml中的profile是pom.xml中的profile的简洁模式。 它蕴含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。         profile元素仅蕴含这四个元素是因为他们波及到整个的构建零碎,而不是个别的POM配置。 如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。 -->    <profiles>        <profile>            <id>default</id>            <activation>                <activeByDefault>true</activeByDefault>                <jdk>1.8</jdk>            </activation>            <!-- 近程仓库-->            <repositories>                <!-- 阿里云近程仓库-->                <repository>                    <id>aliyun</id>                    <name>aliyun maven Repository</name>                    <url>https://maven.aliyun.com/repository/public</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>false</enabled>                    </snapshots>                </repository>                <repository>                    <id>spring-milestone</id>                    <name>Spring Milestone Repository</name>                    <url>http://repo.spring.io/milestone</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>false</enabled>                    </snapshots>                    <layout>default</layout>                </repository>                <repository>                    <id>spring-snapshot</id>                    <name>Spring Snapshot Repository</name>                    <url>http://repo.spring.io/snapshot</url>                    <releases>                        <enabled>false</enabled>                    </releases>                    <snapshots>                        <enabled>true</enabled>                    </snapshots>                    <layout>default</layout>                </repository>            </repositories>            <!-- 镜像,增加了会先从阿里云仓库下载-->            <pluginRepositories>                <pluginRepository>                    <id>aliyun-plugin</id>                    <url>https://maven.aliyun.com/repository/public</url>                    <releases>                        <enabled>true</enabled>                    </releases>                    <snapshots>                        <enabled>false</enabled>                    </snapshots>                </pluginRepository>            </pluginRepositories>        </profile>    </profiles>    <!-- activations是profile的要害,就像POM中的profiles,profile的能力在于它在特定状况下能够批改一些值。         而这些状况是通过activation来指定的。 -->    <!-- <activeProfiles/> --></settings>
另外须留神:在idea中配置maven配置文件和指定本地依赖jar包存储仓库门路十分重要,尤其留神,强烈建议将多个我的项目的local repository 设置到同一个门路,以反复利用下载的依赖jar包。如下图所示:

5. 筹备Gradle

越来越多的java我的项目应用gradle来治理和构建。gradle是比maven更高级的源码构建和治理计划,它不仅仅用于java,设置c++/python等多种语言都能够用来构建。装置完gradle后在命令行执行gradle init就会交互式提醒用户抉择语言和我的项目类型等帮忙用户创立一个指定语言和指定类型的我的项目工程雏形。(有趣味的读者能够试试,很好玩)

cd $es_root_dir

vim gradle/wrapper/gradle-wrapper.properties 发现elasticsearch源码工程用的gradle版本6.6.1

还发现配置文件里配置了:distributionBase=GRADLE_USER_HOME 和zipStoreBase=GRADLE_USER_HOME 都表明 elasticsearch 编译过程依赖的jar包都会下载到 GRADLE_USER_HOME环境变量值对应的目录下。(如果该变量未定义,gradle会默认将jar包放到 ~/.gradle下。)

5.1 原生gradle

wget https://downloads.gradle-dn.com/distributions/gradle-6.8.3-all.zip

找gradle命令是通过~/.bashrc 中export GRADLE_HOME=/path/to/gradle

而gradle 的任何命令的执行都依赖于环境变量GRADLE_USER_HOME的值,该变量值含意为gradle 配置和缓存下载所有的根目录(caches子目录)。

而后首次执行gradle -v 会发现~/.gradle目录被生成了,该目录下还有几个子目录。

此时删除.gradle目录,同时export GRADLE_USER_HOME=~/temp

再执行gradle -v,会发现~/.gradle目录不再生成,在GRADLE_USER_HOME环境变量值目录下对应生成了相干子目录。

至于gradle的配置文件都是在GRADLE_USER_HOME目录下init.gradle 以及子目录init.d下的*.gradle(按文件名字母程序顺次读取加载)。比方批改国内源就是在这些中央的配置文件里增加(本我的项目没有批改源)。下载的依赖jar包等都被放在GRADLE_USER_HOME下caches子目录下。

因而,强烈建议在~/.bashrc中 export GRADLE_USER_HOME=/path/to/gradle/user/home来配置gradle下载依赖包的门路。

5.2 Gradle wrapper

有了gradle,然而不同我的项目须要的gradle版本可能不一样。因而有必要将特定版本的gradle与特定的我的项目绑定起来,这就用到了gradle wrapper。

执行gradle init 或gradle wrapper 会生成如下的目录构造:

简略解释一下: gradew和gradle.bat 别离是linux和windows下的gradle wrapper 命令名称。比方gradle -v 由gradlew -v代替。

gradle/wrapper目录下保障gradle wrapper个性能正确工作的数据组成。一个典型的gradle/wrapper/gradle-wrapper.properies内容组成如下:

distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zipzipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/dists

当执行gradlew脚本时会首先去读取当前目录下gradle/wrapper/gradle-wrapper.properties文件,确定要下载的gradle的url,下载的zip压缩包的根目录,解压后的目录,gradle配置的根目录即distributionBase。而后才是去读取settings.gradle和build.gradle来执行定义的工作。gradle clean/build/tasks是常见的命令。

6. 编译打包elasticsearch工程

cd $es_code_root

export GRADLE_USER_HOME=~/data/gradle_user_home 这一句如果在~/.bashrc中export设置过了就不必再设置了。

执行./gradlew localDistro 编译打包es,显示如下表明编译胜利。

BUILD SUCCESSFUL in 5m 41s

当前目录下的build/distribution/local/elasticsearch-7.10.2-SNAPSHOT 即时生成的发行包(大略500多MB),拷贝走改改配置能够间接启动es服务端。

留神es源码中蕴含lucene源码的,调试es源码就蕴含了调试lucene源码,不须要再独自下载lucene源码了。

7. 命令行启动/敞开elasticsearch服务

mkdir es-run-dir 记为 $es_run_dir

间接将编译打包好的build/distribution/local/elasticsearch-7.10.2-SNAPSHOT 目录拷贝到$es_run_dir

cd $es_run_dir

简略配置下eleasticsearch服务的配置文件config/elasticsearch.yml,如下:

[chilly@cent7ax config]$ diff elasticsearch.yml.old elasticsearch.yml.modified23c23< #node.name: node-1---> node.name: node-155c55< #network.host: 192.168.0.1---> network.host: 0.0.0.068c68< #discovery.seed_hosts: ["host1", "host2"]---> discovery.seed_hosts: ["192.168.0.210"]72c72< #cluster.initial_master_nodes: ["node-1", "node-2"]---> cluster.initial_master_nodes: ["node-1"]88a89,95> #> #增加如下内容> http.cors.enabled: true> http.cors.allow-origin: "*"> http.cors.allow-headers: Authorization> xpack.security.enabled: true> xpack.security.transport.ssl.enabled: true

变动阐明如下:

放开node.name的正文,并在文件最初加上几行用于设置elastisearch服务拜访明码认证的设置。留神如果想在外网拜访elasticsearch服务,还须要配置一下elastcisearch.yml中的network.host 为本机理论ip或0.0.0.0,同时把discovery.seed_hosts设置为本机理论ip,设置cluster.initial_master_nodes。留神本机防火墙是否屏蔽了9200和9300等若干elasticsearch服务端口。

最初在命令上执行命令: ./bin/elasticsearch 或 ./bin/elasticsearch -d -p pid 启动elasticsearch服务。

此时如果间接在浏览器输出 127.0.0.1:9200 会发现提醒要输出户名和明码。

此时留神是在elasticsearch服务启动的前提下,在命令行执行elasticsearch-setup-passwords interactive 以命令行交互方式设置明码。之后登陆时个别以用户名elastic用户登录执行查问。

命令行敞开elasticsearch服务命令为:pkill -F pid。

以下是命令行常见curl操作elasticsearch示例展现:

curl http://127.0.0.1:9200/_cat/health?v --user elastic:elasticcurl http://127.0.0.1:9200/_cat/nodes?v --user elastic:elasticcurl http://127.0.0.1:9200/_cat/indices?v --user elastic:elasticcurl -XPUT http://127.0.0.1:9200/position?pretty --user elastic:elasticcurl -XDELETE http://127.0.0.1:9200/position?pretty --user elastic:elasticcurl -XGET http://127.0.0.1:9200/_all/_mapping?pretty --user elastic:elasticcurl -XGET http://127.0.0.1:9200/position/_mapping?pretty --user elastic:elasticcurl -H "Content-Type:application/json" -XPOST 'http://localhost:9200/_all/_search?pretty' -d ' { "query":{ "match_all":{} } }' --user elastic:elasticcurl -H "Content-Type:application/json" -XPOST 'http://localhost:9200/_all/_search?pretty' -d ' { "query":{ "term":{"title":"小明"} } }' --user elastic:elasticcurl -H "Content-Type:application/json"  -XPOST http://127.0.0.1:9200/position1/_doc?pretty -d ' { "title": "小", "price":10.1, "desc":"" }' --user elastic:elasticcurl  -XPUT http://127.0.0.1:9200/position/?pretty  -H 'content-Type:application/json'  -d  ' {    "mappings":{        "properties":{            "title":{                "type":"text"            },            "description":{                "type":"text"            },            "price":{                "type":"double"            },            "onSale":{                "type":"boolean"            },            "type":{                "type":"integer"            },            "createDate":{                "type":"date"            }        }    }}'  --user elastic:elasticcurl  -XPUT http://127.0.0.1:9200/position2/_mappings?pretty  -H 'content-Type:application/json'  -d  ' {    "properties":{        "title":{            "type":"text"        },        "description":{            "type":"text"        },        "price":{            "type":"double"        },        "onSale":{            "type":"boolean"        },        "type":{            "type":"integer"        },        "createDate":{            "type":"date"        }    }}' --user elastic:elastic

留神: 后续下面这些用法都将在kibana dev-tool控制台以更精炼的形式应用,有主动提醒等性能,

因而在kibana dev-tool开发管制台上执行下面curl语句是更举荐的形式。

8. 下载和装置、启动kibana

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.10.2-linux-x86_64.tar.gz 留神:kibana版本必须与elasticsearch版本统一。

vim config/kibana.yml

--- kibana.yml  2021-01-13 10:07:15.000000000 +0800+++ kibana.yml.modified 2022-12-19 16:17:03.079294242 +0800@@ -4,7 +4,7 @@ # Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values. # The default is 'localhost', which usually means remote machines will not be able to connect. # To allow connections from remote users, set this parameter to a non-loopback address.-#server.host: "localhost"+server.host: "0.0.0.0" # Enables you to specify a path to mount Kibana at if you are running behind a proxy. # Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath@@ -38,8 +38,8 @@ # the username and password that the Kibana server uses to perform maintenance on the Kibana # index at startup. Your Kibana users still need to authenticate with Elasticsearch, which # is proxied through the Kibana server.-#elasticsearch.username: "kibana_system"-#elasticsearch.password: "pass"+elasticsearch.username: "elastic"+elasticsearch.password: "elastic" # Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively. # These settings enable SSL for outgoing requests from the Kibana server to the browser.

启动 kibana : ./bin/kibana

留神: 拜访kibana主页:192.168.0.210:5601时,常常遇到一种状况,输出用户名和明码 总是提醒不胜利,一种常常的起因是:elasticsearch 过程的磁盘空间有余,导致elasticsearch主动切换成了只读模式。

拜访http://192.168.0.210:5601/app/dev_tools#/console

9. 下载和配置idea

举荐应用经典版本2020版linux idea,地址是链接:https://pan.baidu.com/s/1JYHt... 。间接解压后执行./bin/idea.sh即可启动,第一次启动后倒入配置文件(次要是设置一些快捷键等):https://pan.baidu.com/s/1hgAx... 。留神如果要从新设置导入idea的配置文件,可删除 ~/.config/JetBrains即可。链接:https://pan.baidu.com/s/1Owuy... 可帮忙以本地磁盘上插件形式导入。

留神有的时候,留神String,Path等这种jdk根底类都飘红,可能是因为jdk版本过低等造成,换一个jdk个别就能解决。

启动后比拟重要的是设置好jdk和maven、gradle的配置文件和仓库等。

10. 在idea中启动elasticsearch debug模式

10.1 导入es源码到idea

启动./bin/idea.sh 导入elasticsearch工程源码,即前文下载的v7.10.2版本。配置好须要的jdk14,配置好maven和gradle。

10.2 运行es源码

idea中点击$es_code_root/server/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java,找到main函数,右键点击Run 'Elasticsearch main()',会运行谬误,而后点击Run/Edit Configurations...,在VM options中填入以下配置,切记将$es_run_root批改为前文第7节中es服务运行的根门路:

Run/Edit Configurations...,在VM options填入以下值:

-Dlog4j2.disable.jmx=true-Des.path.home=$es_run_root-Des.path.conf=$es_run_root/config-Djava.security.policy=$es_run_root/config/java.policy-Xms2g-Xmx2g

而后在$es_run_root/config目录下创立文件java.policy,并写入以下配置:

grant {    permission javax.management.MBeanServerPermission "createMBeanServer";    permission java.lang.RuntimePermission "createClassLoader";    permission java.lang.RuntimePermission "getClassLoader";    permission java.lang.RuntimePermission "setContextClassLoader";    permission org.elasticsearch.secure_sm.ThreadPermission        "modifyArbitraryThread";    permission org.elasticsearch.secure_sm.ThreadPermission        "modifyArbitraryThreadGroup";};

$es_run_root/config/elasticsearch.yml的配置形式保留前文第7节的对该文件的配置即可。

如果启动过程报错呈现如下,你须要在Run/Edit Configurations...,在VM options填入-Xms2g -Xmx2g。

[1] bootstrap checks failed[1]: initial heap size [327155712] not equal to maximum heap size [5217714176]; this can cause resize pauses

而后,运行Elasticsearch即可,待启动后,在浏览器输出http://localhost:9200/,显示相似第四步骤中的信息则证实启动胜利:

如果出现异常信息“java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ExtendedPluginsClassLoader”,则从新关上Run Configuration,勾选 include dependency with provided scope后,从新运行即可。如果还是报该异样,则可尝试批改server模块下的build.gradle中的compileOnly project(':libs:plugin-classloader')为compile project(':libs:plugin-classloader') 。

接着同时启动kibana后,在浏览器输出http://localhost:5601即可通过kibana给idea debug启动的es服务发送查问了。