类容器隔离组件源码浅析

来源:互联网 发布:海淘优惠信息网站 知乎 编辑:程序博客网 时间:2024/05/22 08:26

一、 PandoraBootstrap.run

获取当前AppClassLoader的urls(JDK系统jar包、本地maven仓库jar包、业务系统classes文件夹)

生成一个ReLaunchURLClassLoader,也称为bizClassLoader(URLClassLoader类型。构造函数传入urls、parent为AppClassLoader)

创建sar包Archive(本质为一个JarFile)

二、 PandoraContainer 类初始化

  1. PipelineContext :保存sar的url、plugin url
  2. ClassLoaderHolder :保存bizClassLoader
  3. ServiceContainer :IOC容器(picocontainer),创建并设置类依赖(DeployService、PluginModuleFactory、ClassLoaderService、SharedClassService、HostInfoService、Pipeline、Context、PandoraServiceManager、InitializerChecker、ClassExporter、LifecycleService、RegistryService)

三、 PandoraContainer 调用start方法

  1. ServiceContainer 初始化并注入类依赖关系
  2. Pipeline 初始化 :根据 pipeline.properties 组装任务链表
  3. 调用 Pipeline 的 execute 方法。按顺序执行pipeline下的类的stepIn和stepOut方法。
## From top to bottom out## ======================== DEPLOYING ======================== ##Log4jDefaultInitOverride=com.taobao.pandora.stage.Log4jDefaultInitOverrideMergeFolderPlugin=com.taobao.pandora.stage.MergeFolderPlugin DeployPluginModule=com.taobao.pandora.stage.DeployPluginModule InitializerCheck=com.taobao.pandora.stage.InitializerCheckExportClass2Cache=com.taobao.pandora.stage.ExportClass2CacheRegisterPandoraService=com.taobao.pandora.stage.RegisterPandoraServiceRegisterLifecycleListener=com.taobao.pandora.stage.RegisterLifecycleListener## ======================== DEPLOYED ======================== ##ChangeStateDeployed=com.taobao.pandora.stage.ChangeStateDeployedStartPandoraService=com.taobao.pandora.stage.StartPandoraService

MergeFolderPlugin

合并Plugin,并存储到PipelineContext中。(com.taobao.pandora.plugins.ext_path > pluginUrls > sar包里plugins)

DeployPluginModule

通过DeployService发布所有Plugin模块 。

给定一个模块目录,根据目录中配置文件生成 ModuleClassLoader ,构建PluginModule。
- priority.properties
- version.properties
- import.properties
- export.properties
- initializer.properties
- service.properties
- listener.properties

InitializerCheck

调用每个module的initializer的permitStartup方法

ExportClass2Cache

用module各自的ModuleClassLoader导出ExportInfo的jarList、packageList、classesList的class到sharedClassService中。

RegisterPandoraService

每个module新建一个PandoraServiceWrapper并调用init方法(service.properties)

RegisterLifecycleListener

每个module注册pluginListeners(listener.properties)。目前没中间件会配置。

ChangeStateDeployed

每个module的状态更改为DEPLOYED

StartPandoraService

每个module新建一个PandoraServiceWrapper并调用start方法。

ModuleClassLoader的加载过程

  1. 已经加载的类
  2. 加载JDK相关的类
  3. com.taobao.pandora开头的选择从PandoraClassLoader中加载
  4. 从共享缓存加载
  5. 根据import语义从bizClassLoader中加载
  6. 从当前classpath下加载
  7. 从bizClassLoader中加载
  8. 从SystemClassLoader下加载

类结构图

这里写图片描述

参考资料

http://blog.csdn.net/b2222505/article/details/78806252
https://bingoex.github.io/2015/09/17/jvm-book-3-classloader/

原创粉丝点击