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

相关推荐
markuszhang14 小时前
Elasticsearch 官网阅读之 Term-level Queries
大数据·elasticsearch·搜索引擎
帝锦_li15 小时前
ElasticSearch
elasticsearch·搜索引擎
好吃的肘子1 天前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
斯普信专业组1 天前
Elasticsearch索引全生命周期管理指南之一
大数据·elasticsearch·搜索引擎
Clown951 天前
go-zero(十九)使用Prometheus监控ES指标
elasticsearch·golang·prometheus
数巨小码人2 天前
Linux常见命令
大数据·linux·运维·服务器·elasticsearch·搜索引擎
真实的菜2 天前
Elasticsearch 分片机制高频面试题(含参考答案)
elasticsearch·搜索引擎·es
ice___Cpu2 天前
Git - 1( 14000 字详解 )
大数据·git·elasticsearch
tebukaopu1482 天前
官方 Elasticsearch SQL NLPChina Elasticsearch SQL
大数据·sql·elasticsearch
jiedaodezhuti2 天前
ElasticSearch重启之后shard未分配问题的解决
笔记·elasticsearch