Skip to content

ElasticSearch 源码调试

封面图

背景

在做Es 二次开发或学习源码的时候可能会需要本地下载并根据源码启动进行调试;本文使用的是8.13.0 这个版本

注: 我本地使用的是jdk17

步骤

Step1. 下载源码

shell
// 从es 官方git 仓库clone 代码到本地
git clone git@github.com:elastic/elasticsearch.git

Step2. 修改Gradle 路径

//先根据这个下载地址将gradle 下载到本地, 然后再修改为本地的路径,否则之后的每次构建都会从该地址下载编译很慢;
// 文件位置/Users/guimu/IdeaProjects/elasticsearch/gradle/wrapper/gradle-wrapper.properties
修改前:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
修改后:
distributionUrl=file\:///Users/guimu/Downloads/gradle-8.6-all.zip

Step3. 修改build.gradle 文件

// 修改该文件,将其中的
//  nativeBundle("org.elasticsearch.ml:ml-cpp:${project.version}:deps@zip") {
//    changing = true
//  }
//  nativeBundle("org.elasticsearch.ml:ml-cpp:${project.version}:nodeps@zip") {
//    changing = true
//  }
进行注释, 否则无法构建,原因是对应版本的依赖不存在;
/Users/guimu/IdeaProjects/elasticsearch/x-pack/plugin/ml/build.gradle

依赖不存在

Step4. 下载license.key构建文件

shell
// 使用该命令下载GPG-KEY-elasticsearch 文件, 在之后构建的时候会用到;否则会提示license.key 不存在
curl -O https://artifacts.elastic.co/GPG-KEY-elasticsearch

Step5. 开始根据源码进行构建

shell
// 在当前ElasticSearch 的主目录下使用gradle 进行构建,构建命令如下(macOS 为例,linux 平台为linux-tar,其他平台具体看CONTRIBUTING.md)
// -Dbuild.docker=false 跳过 Docker 相关的构建步骤,提高构建效率(根据自身需求确定是否需要该参数)
// -Dbuild.snapshot=false 构建时禁用快照版本标识,从而生成一个正式版本
// -Dlicense.key=licenses/GPG-KEY-elasticsearch 指定步骤4 下载的license.key文件
// -x :x-pack:plugin:ml:generateNotice 排除该模块; 这是机器学习相关的;根据情况自身情况而定是否需要排除;
./gradlew :distribution:archives:darwin-tar:assemble -Dbuild.docker=false -Dbuild.snapshot=false -Dlicense.key=licenses/GPG-KEY-elasticsearch -x :x-pack:plugin:ml:generateNotice

Step6. 构建结果并启动

shell
// 执行步骤5 后会在该目录下得到构建产物,即: elasticsearch-8.13.0-darwin-x86_64.tar.gz
// 注意这个路径和上述构建指定的平台有关系如,我这里指定的是darwin-tar, 所以会到darwin-tar 路径下;
/Users/guimu/IdeaProjects/elasticsearch/distribution/archives/darwin-tar/build/distributions/elasticsearch-8.13.0-darwin-x86_64.tar.gz
// 进入到该目录,找到该文件并对其进行解压
tar -xvzf elasticsearch-8.13.0-darwin-x86_64.tar.gz
// 解压后会得到elasticsearch-8.13.0 目录, 进入到该目录下的config目录下
// 修改elasticsearch.yml 配置文件关闭以下几个配置(根据自身需求)
xpack.security.enabled: false
xpack.security.enrollment.enabled: false
xpack.ml.enabled: false

//在jvm.options文件中添加以下配置

# Enable remote debugging
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

// 启动elsaticsearch
// 进入到elasticsearch-8.13.0/bin 目录下, 执行以下命令即可
./elasticsearch

最终elasticsearch.yml 配置如下

yaml
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
# node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false

#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 20-11-2024 07:54:34
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.ml.enabled: false
xpack.security.enabled: false

xpack.security.enrollment.enabled: false

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: false
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
# cluster.initial_master_nodes: ["xxx"]

# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0

# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

ElasticSearch运行成功:

启动成功!

Step7. Idea 配置远程Debug

在Idea 上添加如下远程配置

Idea Remote Debug

在Idea 上点击启动远程Debug后再进行Postman 访问

Postman 测试请求访问

Idea Debug

对应的curl 请求代码:

shell
curl --location 'http://127.0.0.1:9200/_cat/indices?pretty=null' \
--header 'Content-Type: application/json'

即可捕获到对应的请求, Rest 请求的代码入口为:

org.elasticsearch.rest.BaseRestHandler#handleRequest

至此完成!

Powered by VitePress