使用Idea本地调试ElasticSearch源码教程

1. 环境准备

操作系统:Windows7

  • JDK版本:Java version "1.8.0_201"
  • Gradle版本:Gradle 3.3
  • ElasticSearch源码版本:Github branch 5.4(5.4分支代码内部实际ES版本为5.4.4)
  • ElasticSearch发行版版本:elasticsearch-5.4.3(ES发行版没有5.4.4版本,此处使用5.4.3)
  • Idea版本:IntelliJ IDEA 2018.3.5 x64

2. 下载和编译ElasticSearch源码

从Github下载ES源码:

#下载指定分支:5.4
$ git clone -b 5.4 https://github.com/elastic/elasticsearch.git

更换Gradle镜像源:使用Gradle原始镜像源,编译过程中下载相应依赖jar包速度太慢,此处我们更换为阿里的源。(对所有项目生效,在USER_HOME/.gradle/下创建init.gradle文件)

allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
		def GRADLE_LOCAL_RELEASE_URL = 'https://repo.gradle.org/gradle/libs-releases-local'
		def ALIYUN_SPRING_RELEASE_URL = 'https://maven.aliyun.com/repository/spring-plugin'
		
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
				if (url.startsWith('http://repo.spring.io/plugins-release')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_SPRING_RELEASE_URL."
                    remove repo
                }
				
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL     
        }
		
		maven {            
            url ALIYUN_JCENTER_URL			
        }
		maven {            
			url ALIYUN_SPRING_RELEASE_URL
	    }
		maven {
			url GRADLE_LOCAL_RELEASE_URL
        }
		
    }
	
 
}


使用Gradle本地编译ES源码

gradlew idea

3. 将工程导入Idea

3.1 导入及编译
  1. idea 中 File -> New Project From Existing Sources 选择你下载的 Elasticsearch 源码路径,然后点 open
  2. 选择 Import project from external model -> Gradle
  3. 勾选 Use auto-import, 然后就可以了。

导入进去后,gradle 又会编译一遍,需要等一会,好了之后如下:
在这里插入图片描述

3.2 相关参数配置
  1. 配置 VM options参数
    点击Edit Configurations,在 VM options 加入如下配置:

    # 注意:
    # 路径为之前下载的ElasticSearch发行版的路径:我的ES发行版路径为(D:\install_band\elasticsearch-5.4.3)
    
    -Des.path.home=D:\install_band\elasticsearch-5.4.3
    -Des.path.conf=D:\install_band\elasticsearch-5.4.3\config
    -Dlog4j2.disable.jmx=true
    
  2. 配置Working directory参数

    # 此处需要配置为:之前下载的ElasticSearch发行版的路径
    
    D:\install_band\elasticsearch-5.4.3
    
  3. 设置use classpath of module

    选择ES模块`core`
    勾选 Include dependencies 那个选项
    

以上即为调试ES源码相关参数配置。

4. 运行及调试

ES源码中入口类路径:D:\code\elasticsearch\core\src\main\java\org\elasticsearch\bootstrap\Elasticsearch.java

shift+F10运行ES源码,报错如下:

[2019-03-29T09:33:58,618][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalArgumentException: plugin [aggs-matrix-stats] is incompatible with version [5.4.4]; was designed for version [5.4.3]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[main/:?]
	at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[main/:?]
	at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67) ~[main/:?]
	at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[main/:?]
	at org.elasticsearch.cli.Command.main(Command.java:88) ~[main/:?]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[main/:?]
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[main/:?]
Caused by: java.lang.IllegalArgumentException: plugin [aggs-matrix-stats] is incompatible with version [5.4.4]; was designed for version [5.4.3]
	at org.elasticsearch.plugins.PluginInfo.readFromProperties(PluginInfo.java:146) ~[main/:?]
	at org.elasticsearch.plugins.PluginsService.getModuleBundles(PluginsService.java:275) ~[main/:?]
	at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:116) ~[main/:?]
	at org.elasticsearch.node.Node.<init>(Node.java:310) ~[main/:?]
	at org.elasticsearch.node.Node.<init>(Node.java:242) ~[main/:?]
	at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:232) ~[main/:?]
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:232) ~[main/:?]
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:350) ~[main/:?]
	at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) ~[main/:?]
	... 6 more

错误原因:
aggs-matrix-stats插件版本冲突。

解决办法:
修改es当前版本
将core模块中的Version.java类由public static final Version CURRENT = V_5_4_4_UNRELEASED;
修改为:public static final Version CURRENT = V_5_4_3;

再次运行:
程序正常启动运行,如下:
在这里插入图片描述