获取源码
从https://github.com/elastic/elasticsearch 获取源代码,切换到要读取的分支。
导入idea
由于现在elasticsearch使用gradle进行代码管理(吐槽下,为啥从maven改成gradle,超级难用),所以需要提前安装gradle。
gradle安装完成后,将代码导入idea,这个过程需要你指定gradle路径。
编译Elasticsearch源码
如果执行gradle build.gradle会编译失败,不知道是什么鬼。
直接运行:gradle idea
漫长下载的等待过程。。。。
编译成功后,文件目录
Elasticsearch核心包
启动Elasticsearch
其中bootstrap包是elasticsearch系统启动包,下面的elasticsearch.java是启动入口,直接运行main函数。会发现控制台报错
<pre>
Exception in thread "main" java.lang.IllegalStateException: path.home is not configured
at org.elasticsearch.env.Environment.<init>(Environment.java:115)
at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:93)
at org.elasticsearch.bootstrap.Bootstrap.initialEnvironment(Bootstrap.java:257)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:297)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114)
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122)
at org.elasticsearch.cli.Command.main(Command.java:88)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Refer to the log for complete error details.
</pre>
这个错误是没有指定配置文件目录,我们知道在安装elasticsearch时候,在${ES_HOME}/config下面有elasticsearch.yml、jvm.options、log4j2.properties配置文件,而我们要指定的就是这些配置文件。elasticsearch源码目录下的distribution/resources目录下就是自带了这些配置文件,可以直接指定:
点击运行继续报错:
<pre>
2017-03-08 11:06:11,447 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.logging.log4j.core.jmx.Server.register(Server.java:389)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:167)
at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:140)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:541)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:258)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:206)
at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:220)
at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:197)
at org.elasticsearch.common.logging.LogConfigurator.configureStatusLogger(LogConfigurator.java:125)
at org.elasticsearch.common.logging.LogConfigurator.configureWithoutConfig(LogConfigurator.java:67)
at org.elasticsearch.cli.Command.main(Command.java:85)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
</pre>
这个问题在https://github.com/elastic/elasticsearch/issues/21932 看到是权限问题,但是发现对我们这样启动并不起作用。
可以创建一个elasticsearch.policy文件,然后在里面写入:
<pre>
grant {
permission javax.management.MBeanTrustPermission "register";
permission javax.management.MBeanServerPermission "createMBeanServer";
};
</pre>
然后放倒distribution/src/main/resources/config下,然后再通过jvm参数指定:
点击运行继续报错:
<pre>
[2017-03-08T10:51:22,451][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: jar hell!
class: jdk.packager.services.UserJvmOptionsService
jar1: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/ant-javafx.jar
jar2: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/lib/packager.jar
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[main/:?]
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[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/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ~[idea_rt.jar:?]
</pre>
这个是由于Elasticsearch中的一个UserJvmOptionsService类和jdk类冲突,现在先解决的一个方法是将ant-javafx.jar去掉,前提是你不使用该JavaFX功能。可以将其重名为ant-javafx.jar.back。这种方式不是最好的,可以寻求更好的解决方式。
继续报错误:
<pre>
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: Unsupported transport.type []
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[main/:?]
at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[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/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) ~[idea_rt.jar:?]
Caused by: java.lang.IllegalStateException: Unsupported transport.type []
at org.elasticsearch.common.network.NetworkModule.getTransportSupplier(NetworkModule.java:214) ~[main/:?]
at org.elasticsearch.node.Node.<init>(Node.java:401) ~[main/:?]
at org.elasticsearch.node.Node.<init>(Node.java:232) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap$6.<init>(Bootstrap.java:241) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:241) ~[main/:?]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:333) ~[main/:?]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) ~[main/:?]
... 11 more
</pre>
这个是由于依赖的transport等jar并没有找到,可以在项目根目录找到models模块,然后将下面目录打包,然后copy到distribution/src/main/models目录下,也可以直接去官网下载zip包,解压后直接copy。
注意:注意copy的版本,如果版本不一致,有可能出现版本冲突。
ok启动成功
<pre>
[2017-03-08T14:23:00,982][INFO ][o.e.n.Node ] [] initializing ...
[2017-03-08T14:23:01,074][INFO ][o.e.e.NodeEnvironment ] [JotwXSM] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [115.5gb], net total_space [177.8gb], spins? [unknown], types [hfs]
[2017-03-08T14:23:01,074][INFO ][o.e.e.NodeEnvironment ] [JotwXSM] heap size [3.5gb], compressed ordinary object pointers [true]
[2017-03-08T14:23:01,076][INFO ][o.e.n.Node ] node name [JotwXSM] derived from node ID [JotwXSM9TR23cYhO1CstiQ]; set [node.name] to override
[2017-03-08T14:23:01,077][INFO ][o.e.n.Node ] version[5.2.1-SNAPSHOT], pid[7688], build[Unknown/Unknown], OS[Mac OS X/10.11.6/x86_64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_91/25.91-b14]
[2017-03-08T14:23:01,077][WARN ][o.e.n.Node ] version [5.2.1-SNAPSHOT] is a pre-release version of Elasticsearch and is not suitable for production
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [aggs-matrix-stats]
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [ingest-common]
[2017-03-08T14:23:04,574][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-expression]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-groovy]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-mustache]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [lang-painless]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [percolator]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [reindex]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [transport-netty3]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] loaded module [transport-netty4]
[2017-03-08T14:23:04,575][INFO ][o.e.p.PluginsService ] [JotwXSM] no plugins loaded
[2017-03-08T14:23:07,038][INFO ][o.e.n.Node ] initialized
[2017-03-08T14:23:07,038][INFO ][o.e.n.Node ] [JotwXSM] starting ...
[2017-03-08T14:23:07,076][INFO ][i.n.u.i.PlatformDependent] Your platform does not provide complete low-level API for accessing direct buffers reliably. Unless explicitly requested, heap buffer will always be preferred to avoid potential system unstability.
[2017-03-08T14:23:07,213][INFO ][o.e.t.TransportService ] [JotwXSM] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300}
[2017-03-08T14:23:07,221][WARN ][o.e.b.BootstrapChecks ] [JotwXSM] initial heap size [268435456] not equal to maximum heap size [4294967296]; this can cause resize pauses and prevents mlockall from locking the entire heap
[2017-03-08T14:23:10,287][INFO ][o.e.c.s.ClusterService ] [JotwXSM] new_master {JotwXSM}{JotwXSM9TR23cYhO1CstiQ}{otfcgw_KR1eU81nblu5T9w}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2017-03-08T14:23:10,307][INFO ][o.e.h.HttpServer ] [JotwXSM] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200}
[2017-03-08T14:23:10,308][INFO ][o.e.n.Node ] [JotwXSM] started
[2017-03-08T14:23:10,313][INFO ][o.e.g.GatewayService ] [JotwXSM] recovered [0] indices into cluster_state
</pre>
关注我
欢迎关注我的公众号,会定期推送优质技术文章,让我们一起进步、一起成长!
公众号搜索:data_tc
或直接扫码:🔽