为了深入学习elasticsearch,研究其源码是种有效途径,本文简述了从下载到编译构建再运行起来的全部过程;
请确保JDK和Gradle已安装在Ubuntu电脑上,并且环境变量设置成功;
由于elasticsearch要求以非root身份启动,因此本次实战用的不是root账号,以下命令可用来创建admin账号:
groupadd admin && useradd -d /home/admin -g admin -m admin
为了证明修改的源码可以正常构建和运行,我们来修改一个java文件,在里面打印一些日志;
public static void printTrack(Logger logger, String prefix){
StackTraceElement[] st = Thread.currentThread().getStackTrace();
if(null==st){
logger.info("invalid stack");
return;
}
StringBuffer sbf =new StringBuffer();
for(StackTraceElement e:st){
if(sbf.length()>0){
sbf.append(" <- ");
sbf.append(System.getProperty("line.separator"));
}
sbf.append(java.text.MessageFormat.format("{0}.{1}() {2}"
,e.getClassName()
,e.getMethodName()
,e.getLineNumber()));
}
logger.info(prefix
+ "\n************************************************************\n"
+ sbf.toString()
+ "\n************************************************************");
}
protected Node(final Environment environment, Collection<Class<? extends Plugin>> classpathPlugins) {
final List<Closeable> resourcesToClose = new ArrayList<>(); // register everything we need to release in the case of an error
boolean success = false;
{
// use temp logger just to say we are starting. we can't use it later on because the node name might not be set
Logger logger = Loggers.getLogger(Node.class, NODE_NAME_SETTING.get(environment.settings()));
logger.info("initializing ...");
}
在logger.info(“initializing …”);这一行代码下面增加下面这一行内容,作用是在elasticsearch启动时打印Node类实例的调用堆栈:
printTrack(logger, "Here is stack of Node instance");
保存好再退出,接下来可以编译了;
...
> Task :test:fixtures:old-elasticsearch:javadocJar
Building without git revision id.
> Task :test:fixtures:old-elasticsearch:sourcesJar
Building without git revision id.
BUILD SUCCESSFUL in 11m 3s
473 actionable tasks: 473 executed
./gradlew assemble -Dbuild.snapshot=false
[2019-04-20T04:02:55,785][INFO ][o.e.n.Node ] [] initializing ...
[2019-04-20T04:02:55,789][INFO ][o.e.n.Node ] [] Here is stack of Node instance
************************************************************
java.lang.Thread.getStackTrace() 1,559 <-
org.elasticsearch.node.Node.printTrack() 953 <-
org.elasticsearch.node.Node.<init>() 255 <-
org.elasticsearch.node.Node.<init>() 245 <-
org.elasticsearch.bootstrap.Bootstrap$5.<init>() 212 <-
org.elasticsearch.bootstrap.Bootstrap.setup() 212 <-
org.elasticsearch.bootstrap.Bootstrap.init() 322 <-
org.elasticsearch.bootstrap.Elasticsearch.init() 121 <-
org.elasticsearch.bootstrap.Elasticsearch.execute() 112 <-
org.elasticsearch.cli.EnvironmentAwareCommand.execute() 86 <-
org.elasticsearch.cli.Command.mainWithoutErrorHandling() 124 <-
org.elasticsearch.cli.Command.main() 90 <-
org.elasticsearch.bootstrap.Elasticsearch.main() 92 <-
org.elasticsearch.bootstrap.Elasticsearch.main() 85
************************************************************
至此编译构建elasticsearch6.1.2成功,在您编译构建elasticsearch的时候,希望本文能给您提供参考;
除了修改和编译源码,通过IntelliJ IDEA远程调试elasticsearch也是有效的学习手段,推荐您参考文章《IntelliJ IDEA远程调试Elasticsearch6.1.2》。