Skip to content

Conversation

@namelessssssssssss
Copy link
Contributor

@namelessssssssssss namelessssssssssss commented Jul 17, 2023

What is the purpose of the change

Through the new SPI interface ApplicationLifecycle, the life cycle management method of each module in dubbo-config is dispersed back to each module itself, so as to minimize the dubbo-config dependency.

ModelContext is the internal lifecycle attribute aggregate of ScopeModel. It acts as context of model lifecycle, and provides a way to trigger a model life-changing process.
This allows Deployer's responsibility to be a pure process managing, without fields related to the current state of life.

Brief changelog

  • Add SPI interface ApplicationLifecycleManager and implements
  • Add interface ModelContext and implements

Call sequence of lifecycles

image

Full changelog (Chinese)

Lifecycle 相关变更

变动的中心为:添加 ApplicationLifecycle SPI接口,其提供了几个生命周期管理方法,随着DefaultApplicationDeployer 中对应的方法而调用。

包括 :

  • start-应用启动时调用

  • initialize-应用初始化时调用,早于start

  • preDestroy 应用关闭前调用

  • postDestroy 应用关闭后调用

  • preModuleChanged、postModuleChanged 应用中模块状态发生变化时调用

  • refreshServiceInstance 应用中新模块注册时调用

1、该接口的实现通过@activate + 多个实现的形式来实现加载顺序的编排操作,整体加载顺序如下:
image
https://boardmix.cn/app/share/CAE.CJbbqwwgASoQLZUgu_72RIU63KXPgkKo7DAGQAE/AhUMjA,
点击链接加入boardmix中的文件「未命名文件」

编排后Lifecycle实现的调用顺序保证与原有DefaultApplicationDeployer中的逻辑调用顺序相同。

2、dubbo-config-api解耦的最终目标是使其依赖关系反转,使所有外部模块依赖它,而其本身不依赖外部模块。

但由于dubbo-config-api的依赖尚未完全解耦,目前暂时保留大多数模块的依赖,因此这些实现暂时放在config-api包中,防止循环依赖。这些实现对应的SPI声明文件已放在它应在的包中。

3、拆分的指导思想是:尽量复用原有的逻辑,且保证不改变语义。Lifecycle实现中大多数方法是对原有DefaultApplicationDeployer中逻辑的重新编排,保证最大程度的兼容性。

按照加载流程分析拆分出的实现:

initialize:
image

  • 首先,ApplicationConfigPreHandleLifecycle 、ConfigCenterApplicationLifecycle、ApplicationConfigPostHandleLifecycle 先执行,对应读取本地配置、配置中心拉取配置、使用ConfigManager 加载所有已读取配置 ,共三步操作

image

  • 之后,指标、追踪、元数据模块执行初始化(顺序不分先后)

image

preDestroy:

image

  • 该方法共两个实现,ApplicationOfflineLifecycle 和 MetricsApplicationLifecycle

    image

首先,ApplicationOfflineLifecycle 在应用关闭时注销元数据服务(注销所有模块中服务的url)

最后,MetricsApplicationLifecycle 注销指标服务。

postDestroy:

image

image

该方法共两个实现,RegistryApplicationLifecycle 和 MetadataApplicationLifecycle,分别完成服务在注册中心的注销操作和服务在元数据中心的注销操作。

pre/postModuleChanged:

应用下的模块状态发生变化时,该方法会被调用。

image

image

preModuleChanged:

首先,MetricsExportApplicationLifecycle先被调用。如果其判断状态发生变化的模块的新状态为已开始,(STARTED),且当前应用未完成注册,会尝试导出指标服务。

之后,RegistryApplicaitonLifecycle 若判断状态发生变化的模块的新状态为已开始,且当前服务为未注册,会将当前服务标记为已注册,执行服务注册逻辑。

之后执行postModuleChanged:

MetricsApplicationLifecycle 被调用。如果它判断模块新状态为已开始,且当前应用的状态为启动中(Starting),会执行指标收集器初始化的相关逻辑。

RegistryApplicationLifecycle 也会被调用。如果模块的新状态不是正在启动(Starting),它会刷新应用元数据。

refreshServiceInstance:

image

image

image

该方法只被MetadataApplicationLifecycle实现,在应用下有新模块启动时被调用。每次被调用都会刷新当前应用的元数据和实例信息。

其它变更

namelessssssssssss and others added 30 commits July 17, 2023 18:33
* Code style fix
* Remove unused dependencies from dubbo-config-api
@namelessssssssssss
Copy link
Contributor Author

@qinliujie PTAL

@namelessssssssssss namelessssssssssss changed the base branch from 3.3 to 3.3-config-api-refactor September 6, 2023 13:25
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 26 Code Smells

79.3% 79.3% Coverage
5.4% 5.4% Duplication

@AlbumenJ AlbumenJ merged commit a6a6d57 into apache:3.3-config-api-refactor Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants