第 28 章 - ES 源码篇 - Elasticsearch 启动与插件加载机制解析

前言

不管是什么框架,启动类里面做的一定是初始化的工作!

启动

ES 节点的启动逻辑,全部都在 org.elasticsearch.bootstrap 包下。

启动类为:Elasticsearch#main(final String[] args)

与大多数框架启动类一致。启动类主要负责的是资源加载、框架初始化。

其主要的启动函数为:Bootstrap#init(final boolean foreground, final Path pidFile, final boolean quiet, final Environment initialEnv)

Bootstrap#init 函数中,会初始化 最核心、最关键的 Node 类

Node 表示集群节点。在该类的构造函数 protected Node(final Environment initialEnvironment,Collection<Class<? extends Plugin>> classpathPlugins, boolean forbidPrivateIndexSettings) 中,会执行节点的初始化,加载配置文件,加载插件。同理,该 org.elasticsearch.node 包下包含了节点相关的类。

Node 在初始化过程中,会创建以下几个模块。

  1. ClusterModule:集群模块,主要负责管理集群元信息
  2. IndicesModule:索引模块
  3. IndexSettingModule:索引配置模块
  4. GatewayModule:网关模块
  5. SettingsModule:配置模块
  6. ActionModule:路由处理器模块
  7. NetworkModule:网络模块

每个模块负责不同的功能,模块内部会关联 n 个插件。

例如,NetworkModule 关联 NetworkPluginActionModule 关联 ActionPlugin

我们重点关注如何加载插件的?用了什么设计模式?做到了高扩展性。

对于加载配置文件、加载插件、加载模块。ES 统一由 PluginsService 处理(面向对象编程)

加载配置:读取 ${es.path.home}/config 目录下的所有文件

加载插件:读取 ${es.path.home}/plugin 目录下的所有文件

加载模块:读取 ${es.path.home}/modules 目录下的所有文件

这些配置文件统一由 Environment 负责管理(面向对象编程)

具体的 PluginsService 如何从解析的,不过多介绍,因为不同的人会有不同的写法,以及不同的解析方式,我们主要学习其思想。其代码入口为 public PluginsService(Settings settings, Path configPath, Path modulesDirectory, Path pluginsDirectory, Collection<Class<? extends Plugin>> classpathPlugins)

其实所谓的可插拔、插件化,无非就是读取指定目录下的文件罢了。这种思想和 SPI 大相径庭。或者说,几乎所有想做到让用户可以自定义某些功能,它的核心本质就是需要让用户在某个指定的目录下按照一定的规范配置文件。

插件的接口定义类均在 org.elasticsearch.plugins 目录下

环境变量相关的配置类均在 org.elasticsearch.env 目录下

到这里,其实就不难看出,ES 源码非常整洁、干净,这样的工程思想非常值得我们学习。

具体的 ES 都加载了哪些 plugin,可以看 Plugin 该类的类注释。

相关推荐
在未来等你19 小时前
Elasticsearch面试精讲 Day 17:查询性能调优实践
大数据·分布式·elasticsearch·搜索引擎·面试
在未来等你1 天前
Elasticsearch面试精讲 Day 18:内存管理与JVM调优
大数据·分布式·elasticsearch·搜索引擎·面试
Elasticsearch1 天前
在 Elastic Observability 中使用 Discover 的追踪获取更深入的应用洞察
elasticsearch
婲落ヽ紅顏誶1 天前
测试es向量检索
大数据·elasticsearch·搜索引擎
咖啡Beans1 天前
Docker安装ELK(Elasticsearch + Logstash + Kibana)
后端·elasticsearch·docker
一勺菠萝丶2 天前
Jenkins 构建 Node 项目报错解析与解决——pnpm lockfile 问题实战
elasticsearch·servlet·jenkins
小花鱼20252 天前
Elasticsearch (ES)相关
大数据·elasticsearch
阿里嘎多哈基米2 天前
ES——(三)DSL高级查询
elasticsearch·搜索引擎·全文检索·kibana·倒排索引
AAA修煤气灶刘哥2 天前
ES 高级玩法大揭秘:从算分骚操作到深度分页踩坑,后端 er 速进!
java·后端·elasticsearch
island13142 天前
【C++框架#5】Elasticsearch 安装和使用
开发语言·c++·elasticsearch