獲取源碼
從https://github.com/elastic/elasticsearch 獲取源代碼,切換到要讀取的分支。
導(dǎo)入idea
由于現(xiàn)在elasticsearch使用gradle進(jìn)行代碼管理(吐槽下,為啥從maven改成gradle,超級(jí)難用),所以需要提前安裝gradle。
gradle安裝完成后,將代碼導(dǎo)入idea,這個(gè)過(guò)程需要你指定gradle路徑。

編譯Elasticsearch源碼
如果執(zhí)行g(shù)radle build.gradle會(huì)編譯失敗,不知道是什么鬼。
直接運(yùn)行:gradle idea
漫長(zhǎng)下載的等待過(guò)程。。。。
編譯成功后,文件目錄

Elasticsearch核心包

啟動(dòng)Elasticsearch
其中bootstrap包是elasticsearch系統(tǒng)啟動(dòng)包,下面的elasticsearch.java是啟動(dòng)入口,直接運(yùn)行main函數(shù)。會(huì)發(fā)現(xiàn)控制臺(tái)報(bào)錯(cuò)
<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>
這個(gè)錯(cuò)誤是沒(méi)有指定配置文件目錄,我們知道在安裝elasticsearch時(shí)候,在${ES_HOME}/config下面有elasticsearch.yml、jvm.options、log4j2.properties配置文件,而我們要指定的就是這些配置文件。elasticsearch源碼目錄下的distribution/resources目錄下就是自帶了這些配置文件,可以直接指定:

點(diǎn)擊運(yùn)行繼續(xù)報(bào)錯(cuò):
<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>
這個(gè)問(wèn)題在https://github.com/elastic/elasticsearch/issues/21932 看到是權(quán)限問(wèn)題,但是發(fā)現(xiàn)對(duì)我們這樣啟動(dòng)并不起作用。
可以創(chuàng)建一個(gè)elasticsearch.policy文件,然后在里面寫(xiě)入:
<pre>
grant {
permission javax.management.MBeanTrustPermission "register";
permission javax.management.MBeanServerPermission "createMBeanServer";
};
</pre>
然后放倒distribution/src/main/resources/config下,然后再通過(guò)jvm參數(shù)指定:

點(diǎn)擊運(yùn)行繼續(xù)報(bào)錯(cuò):
<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>
這個(gè)是由于Elasticsearch中的一個(gè)UserJvmOptionsService類(lèi)和jdk類(lèi)沖突,現(xiàn)在先解決的一個(gè)方法是將ant-javafx.jar去掉,前提是你不使用該JavaFX功能??梢詫⑵渲孛麨閍nt-javafx.jar.back。這種方式不是最好的,可以尋求更好的解決方式。
繼續(xù)報(bào)錯(cuò)誤:
<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>
這個(gè)是由于依賴的transport等jar并沒(méi)有找到,可以在項(xiàng)目根目錄找到models模塊,然后將下面目錄打包,然后copy到distribution/src/main/models目錄下,也可以直接去官網(wǎng)下載zip包,解壓后直接copy。
注意:注意copy的版本,如果版本不一致,有可能出現(xiàn)版本沖突。
ok啟動(dòng)成功
<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>
關(guān)注我
歡迎關(guān)注我的公眾號(hào),會(huì)定期推送優(yōu)質(zhì)技術(shù)文章,讓我們一起進(jìn)步、一起成長(zhǎng)!
公眾號(hào)搜索:data_tc
或直接掃碼:??
