第 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 该类的类注释。

相关推荐
G皮T12 小时前
【Elasticsearch】映射:null_value 详解
大数据·elasticsearch·搜索引擎·映射·mappings·null_value
Elasticsearch2 天前
ES8 向量功能窥探系列(二):向量数据的存储与优化
elasticsearch
安审若无2 天前
Elasticsearch中的监控(Monitoring)功能介绍
elasticsearch·搜索引擎·全文检索
leo_hush2 天前
elasticsearch基本操作笔记
elasticsearch
咸鱼求放生2 天前
es在Linux安装
大数据·elasticsearch·搜索引擎
xyhshen2 天前
k8s下离线搭建elasticsearch
elasticsearch·容器·kubernetes
@泽栖2 天前
ES数据聚合
elasticsearch·搜索引擎
張萠飛3 天前
Linux下如何使用shell脚本导出elasticsearch中某一个index的数据为本地csv文件
linux·运维·elasticsearch
LanLance3 天前
ES101系列09 | 运维、监控与性能优化
java·运维·后端·elasticsearch·云原生·性能优化·golang
疯狂的沙粒3 天前
如何通过git命令查看项目连接的仓库地址?
大数据·git·elasticsearch