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

相关推荐
Dxy12393102161 天前
Elasticsearch 索引与映射:为你的数据打造一个“智能仓库”
大数据·elasticsearch·搜索引擎
倒流时光三十年1 天前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch
星辰_mya1 天前
Elasticsearch更新了分词器之后
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客1 天前
Elasticsearch:Workflows 介绍 - 9.3
大数据·数据库·人工智能·elasticsearch·ai·全文检索
星辰_mya1 天前
Elasticsearch主分片数写入后不能改
大数据·elasticsearch·搜索引擎
春日见2 天前
vscode代码无法跳转
大数据·人工智能·深度学习·elasticsearch·搜索引擎
那起舞的日子2 天前
ElasticSearch系列-3-java端整合CURD
elasticsearch
历程里程碑2 天前
普通数组----最大子数组和
大数据·算法·elasticsearch·搜索引擎·排序算法·哈希算法·散列表
星辰_mya2 天前
Elasticsearch之下
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客2 天前
跳过 MLOps:通过 Cloud Connect 使用 EIS 为自管理 Elasticsearch 提供托管云推理
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索