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

相关推荐
Elastic 中国社区官方博客2 小时前
Elasticsearch:加快 HNSW 图的合并速度
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
gqkmiss6 小时前
Git 常用命令集与实际使用 Demo
大数据·git·elasticsearch·git 命令·git 命令大全
Elastic 中国社区官方博客8 小时前
Elasticsearch 向量数据库,原生支持 Google Cloud Vertex AI 平台
大数据·数据库·人工智能·elasticsearch·搜索引擎·语言模型·自然语言处理
老友@20 小时前
Elasticsearch 全面解析
大数据·elasticsearch·搜索引擎
惜鸟1 天前
Elasticsearch文档标签检索方案设计
后端·elasticsearch
程序辕日记2 天前
使用SQL查询ES数据
sql·elasticsearch·jenkins
啥都不懂的小小白2 天前
Elasticsearch入门指南(三) 之 高级篇
大数据·elasticsearch·jenkins
qq_5470261792 天前
Elasticsearch 集群搭建
大数据·elasticsearch·搜索引擎
destinyol2 天前
wsl-docker环境下启动ES报错vm.max_map_count [65530] is too low
elasticsearch·docker·容器
大哥喝阔落2 天前
git操作0409
大数据·git·elasticsearch