微服务架构|*2.3SpringCloud启动及加载配置文件源码分析(以Nacos为例)

博客 动态
0 293
羽尘
羽尘 2022-01-24 14:00:46
悬赏:0 积分 收藏

微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

目录
  • 前言
  • 1. Spring Cloud 什么时候加载配置文件
  • 2. 准备 Environment 配置环境
    • 2.1 配置 Environment 环境 SpringApplication.prepareEnvironment()
    • 2.2 使用事件主控器创建并发布事件 SimpleApplicationEventMulticaster.multicastEvent()
    • 2.3 BootstrapApplicationListener 处理事件,自动导入一些配置类
  • 3. 刷新应用上下文
    • 3.1 刷新上下文 SpringApplication.prepareContext()
    • 3.2 初始化上下文的额外操作 SpringApplication.applyInitializers()
    • 3.3 读取 Nacos 服务器里的配置 NacosPropertySourceLocator.locate()
    • 3.4 初始化完成,发布 ApplicationContextInitializedEvent 事件
    • 3.5 配置加载完成,将监听器添加进上下文环境
  • 4. 程序运行事件
  • 5. Spring Cloud 启动及加载配置文件源码结构图小结
  • 最后


前言

参考资料
《Spring Microservices in Action》
《Spring Cloud Alibaba 微服务原理与实战》
《B站 尚硅谷 SpringCloud 框架开发教程 周阳》

Spring Cloud 要实现统一配置管理,需要解决两个问题:如何获取远程服务器配置和如何动态更新配置;在这之前,我们先要知道 Spring Cloud 什么时候给我们加载配置文件;


1. Spring Cloud 什么时候加载配置文件

  • 首先,Spring 抽象了一个 Environment 来表示 Spring 应用程序环境配置,整合了各种各样的外部环境,并且提供统一访问的方法 getProperty()
  • 在 Spring 应用程序启动时,会把配置加载到 Environment 中。当创建一个 Bean 时可以从 Environment 中把一些属性值通过 @Value 的形式注入业务代码;
  • Spring Cloud 是基于 Spring 的发展而来,因此也是在启动时加载配置文件,而 Spring Cloud 的启动程序是主程序类 XxxApplication,我们断点进入主程序类;
@EnableDiscoveryClient@SpringBootApplicationpublic class ProviderApplication {	public static void main(String[] args) {	    //【断点步入】主启动方法		SpringApplication.run(ProviderApplication.class, args);	}}
  • SpringApplication.run() 运行方法里会准备 Environment 环境;
public ConfigurableApplicationContext run(String... args) {        //初始化StopWatch,调用 start 方法开始计时        StopWatch stopWatch = new StopWatch();        stopWatch.start();        ConfigurableApplicationContext context = null;        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();        //设置系统属性java.awt.headless,这里为true,表示运行在服务器端,在没有显示器和鼠标键盘的模式下工作,模拟输入输出设备功能        this.configureHeadlessProperty();        SpringApplicationRunListeners listeners = this.getRunListeners(args);        //SpringApplicationRunListeners 监听器工作--->发布 ApplicationStartingEvent 事件        listeners.starting();        Collection exceptionReporters;        try {        //持有着 args 参数        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);                //【断点步入 2.】准备 Environment 环境--->发布 ApplicationEnvironmentPreparedEvent 事件        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);        this.configureIgnoreBeanInfo(environment);        //打印 banner        Banner printedBanner = this.printBanner(environment);        //创建 SpringBoot 上下文        context = this.createApplicationContext();        exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);                //【断点步入 3.】准备应用上下文--->发布 ApplicationEnvironmentPreparedEvent 事件        this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);        //刷新上下文--->发布 ContextRefreshedEvent 事件        this.refreshContext(context);        //在容器完成刷新后,依次调用注册的Runners        this.afterRefresh(context, applicationArguments);        //停止计时        stopWatch.stop();        if (this.logStartupInfo) {            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);        }        //SpringApplicationRunListeners 监听器工作--->发布 ApplicationStartedEvent 事件        listeners.started(context);        this.callRunners(context, applicationArguments);    } catch (Throwable var10) {        this.handleRunFailure(context, var10, exceptionReporters, listeners);        throw new IllegalStateException(var10);    }        try {        //【断点步入 4.】SpringApplicationRunListeners 监听程序运行事件--->发布ApplicationReadyEvent事件        listeners.running(context);        return context;    } catch (Throwable var9) {        this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);        throw new IllegalStateException(var9);    }}
  • 注意,有些同学可能 debug 进不了这里的 try 语句,可能是因为引入了以下这个依赖,去掉这个依赖即可;
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <scope>runtime</scope>    <optional>true</optional></dependency>

2. 准备 Environment 配置环境

2.1 配置 Environment 环境 SpringApplication.prepareEnvironment()

  • 我们进入 SpringApplication.prepareEnvironment() 方法里,该方法主要是根据一些信息配置 Environment 环境,然后调用 SpringApplicationRunListeners(Spring 应用运行监听器) 监听器工作;
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {    //获取可配置的环境    ConfigurableEnvironment environment = this.getOrCreateEnvironment();    //根据可配置的环境,配置 Environment 环境    this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());    //【断点步入】告诉监听者 Environment 环境已经准备完毕    listeners.environmentPrepared((ConfigurableEnvironment)environment);    this.bindToSpringApplication((ConfigurableEnvironment)environment);    if (!this.isCustomEnvironment) {        environment = (new EnvironmentConverter(this.getClassLoader())).convertEnvironmentIfNecessary((ConfigurableEnvironment)environment, this.deduceEnvironmentClass());    }
  • 进入 SpringApplicationRunListeners.environmentPrepared() 方法,该方法的作用是遍历每一个监听者,并对这些监听者进行操作;
public void environmentPrepared(ConfigurableEnvironment environment) {    //使用迭代器遍历监听者    Iterator var2 = this.listeners.iterator();    while(var2.hasNext()) {        SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();        //【断点步入】对每一个监听者进行操作        listener.environmentPrepared(environment);    }}

2.2 使用事件主控器创建并发布事件 SimpleApplicationEventMulticaster.multicastEvent()

  • 进入 EventPublishingRunListener.environmentPrepared() 方法,发现对每一个监听者实现的操作是:使用 SimpleApplicationEventMulticaster(事件主控器) 发布了一个 ApplicationEnvironmentPreparedEvent(应用程序环境准备完成事件)
public void environmentPrepared(ConfigurableEnvironment environment) {    //【断点步入】使用事件主控器发布事件    this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));}
  • 进入 SimpleApplicationEventMulticaster.multicastEvent() 方法
public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) {    //解析出事件类型    ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);    //获得事件执行者    Executor executor = this.getTaskExecutor();    //获得监听者迭代器    Iterator var5 = this.getApplicationListeners(event, type).iterator();    while(var5.hasNext()) {        ApplicationListener<?> listener = (ApplicationListener)var5.next();        //如果有执行者,就通过执行者发布事件        if (executor != null) {            executor.execute(() -> {                this.invokeListener(listener, event);            });        } else {        //【断点步入 1.2】没有执行者就直接发事件            this.invokeListener(listener, event);        }    }}
  • 总结来说就是 Spring Cloud 在配置完 Environment 环境后会发布一个 ApplicationEnvironmentPreparedEvent(应用程序环境准备完成事件) 告诉所有监听者,Environment 环境已经配置完毕了;
  • 所有对 ApplicationEnvironmentPreparedEvent 事件感兴趣的 Listener 都会监听这个事件。其中包括 BootstrapApplicationListener

BootstrapApplicationListener 监听者监听 ApplicationEnvironmentPreparedEvent 事件

  • 注意,这里需要遍历到后面才是 ApplicationEnvironmentPreparedEvent 事件,前面可能是其他事件;

2.3 BootstrapApplicationListener 处理事件,自动导入一些配置类

  • 发布事件后被 BootstrapApplicationListener(Bootstrap 监听器) 监听到,调用 BootstrapApplicationListener.onApplicationEvent() 方法处理事件:
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {    //获取当前 Environment 环境    ConfigurableEnvironment environment = event.getEnvironment();    //如果环境可用    if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {        //如果环境来源不包含 bootstrap        if (!environment.getPropertySources().contains("bootstrap")) {            ConfigurableApplicationContext context = null;            String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");            Iterator var5 = event.getSpringApplication().getInitializers().iterator();            while(var5.hasNext()) {                ApplicationContextInitializer<?> initializer = (ApplicationContextInitializer)var5.next();                if (initializer instanceof ParentContextApplicationContextInitializer) {                    context = this.findBootstrapContext((ParentContextApplicationContextInitializer)initializer, configName);                }            }            if (context == null) {                //【断点步入】将不是 bootstrap 来源的配置添加进 bootstrap 上下文                context = this.bootstrapServiceContext(environment, event.getSpringApplication(), configName);                event.getSpringApplication().addListeners(new ApplicationListener[]{new BootstrapApplicationListener.CloseContextOnFailureApplicationListener(context)});            }            this.apply(context, event.getSpringApplication(), environment);        }    }}
  • 进入 BootstrapApplicationListener.bootstrapServiceContext() 方法,发现其主要做的是配置自动导入的实现;
private ConfigurableApplicationContext bootstrapServiceContext(ConfigurableEnvironment environment, final SpringApplication application, String configName) {    //省略其他代码    //配置自动装配的实现    builder.sources(new Class[]{BootstrapImportSelectorConfiguration.class});    return context;}
  • 我们搜索 BootstrapImportSelectorConfiguration(Bootstrap选择导入配置类) 类,发现其使用 BootstrapImportSelector(Bootstrap导入选择器) 进行自动配置;
@Configuration@Import({BootstrapImportSelector.class}) //使用BootstrapImportSelector类进行自动配置public class BootstrapImportSelectorConfiguration {    public BootstrapImportSelectorConfiguration() {    }}
  • BootstrapImportSelector(Bootstrap 导入选择器) 类的 selectImports() 方法使用 Spring 中的 SPI 机制;
public String[] selectImports(AnnotationMetadata annotationMetadata) {        //省略其他代码        List<String> names = new ArrayList(SpringFactoriesLoader.loadFactoryNames(BootstrapConfiguration.class, classLoader));}
  • 可到 classpath 路径下查找 META-INF/spring.factories 预定义的一些扩展点,其中 key 为 BootstrapConfiguration;
  • 可以得知给我们导入了一些,其中有两个配置类 PropertySourceBootstrapConfigurationNacosConfigBootstrapConfiguration(这两个类在本篇 3.2 和 3.3 里会提到,用来加载额外配置的);

3. 刷新应用上下文

3.1 刷新上下文 SpringApplication.prepareContext()

  • 我们回到 SpringApplication.run() 方法里,在准备完 Environment 环境后,会调用 SpringApplication.prepareContext() 方法刷新应用上下文。我们进入该方法;
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {    //设置上下文的 Environment 环境    context.setEnvironment(environment);    this.postProcessApplicationContext(context);    //【断点步入 3.2】初始化上下文    this.applyInitializers(context);    //【断点步入 3.4】发布上下文初始化完毕事件 ApplicationContextInitializedEvent     listeners.contextPrepared(context);        //后面代码省略    //【断点步入 3.5】这是该方法的最后一条语句,发布     listeners.contextLoaded(context);}

3.2 初始化上下文的额外操作 SpringApplication.applyInitializers()

  • 进入 SpringApplication.applyInitializers() 方法,在应用程序上下文初始化时做一些额外操作;
  • 即执行非 Spring Cloud 官方提供的额外的操作,这里指 Nacos 自己的操作;
protected void applyInitializers(ConfigurableApplicationContext context) {    Iterator var2 = this.getInitializers().iterator();    while(var2.hasNext()) {        ApplicationContextInitializer initializer = (ApplicationContextInitializer)var2.next();        Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(), ApplicationContextInitializer.class);        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");        //【断点步入】遍历迭代器,初始化上下文        initializer.initialize(context);    }}
  • 这里的 .initialize() 方法最终调用的是 PropertySourceBootstrapConfiguration(Bootstrap 属性源配置类) ,而这个类就是 1.2 里准备 Environment 环境时给我们自动导入的。我们进入 PropertySourceBootstrapConfiguration.initialize() 方法,得出最后结论:
public void initialize(ConfigurableApplicationContext applicationContext) {    //省略其他代码     Iterator var5 = this.propertySourceLocators.iterator();    while(var5.hasNext()) {        //PropertySourceLocator 接口的主要作用是实现应用外部化配置可动态加载        PropertySourceLocator locator = (PropertySourceLocator)var5.next();        PropertySource<?> source = null;        //【断点步入】读取 Nacos 服务器里的配置        source = locator.locate(environment);    }}

3.3 读取 Nacos 服务器里的配置 NacosPropertySourceLocator.locate()

  • 我们 1.2 里准备的 Environment 环境中引入的配置类 NacosPropertySourceLocator 实现了该接口,因此最后调用的是 NacosPropertySourceLocator.locate() 方法;
  • 该方法将读取 Nacos 服务器里的配置,把结构存到 PropertySource 的实例里返回;
  • NacosPropertySourceLocator.locate() 方法源码如下:
@Overridepublic PropertySource<?> locate(Environment env) {    //获取配置服务器实例,这是Nacos客户端提供的用于访问实现配置中心基本操作的类	ConfigService configService = nacosConfigProperties.configServiceInstance();	if (null == configService) {		log.warn("no instance of config service found, can't load config from nacos");		return null;	}	long timeout = nacosConfigProperties.getTimeout();	//Nacos 属性源生成器	nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout);	String name = nacosConfigProperties.getName();    //DataId 前缀	String dataIdPrefix = nacosConfigProperties.getPrefix();	if (StringUtils.isEmpty(dataIdPrefix)) {		dataIdPrefix = name;	}    //没有配置 DataId 前缀则用 spring.application.name 属性的值	if (StringUtils.isEmpty(dataIdPrefix)) {		dataIdPrefix = env.getProperty("spring.application.name");	}    //创建复合属性源	CompositePropertySource composite = new CompositePropertySource(NACOS_PROPERTY_SOURCE_NAME);    //加载共享配置	loadSharedConfiguration(composite);	//加载外部配置	loadExtConfiguration(composite);	//加载 Nacos 服务器上应用程序名对应的的配置	loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);	return composite;}

3.4 初始化完成,发布 ApplicationContextInitializedEvent 事件

  • 进入 SpringApplicationRunListeners.contextPrepared() 发布事件;
public void contextPrepared(ConfigurableApplicationContext context) {    //构造迭代器    Iterator var2 = this.listeners.iterator();    while(var2.hasNext()) {        SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();        //【断点步入】发布事件        listener.contextPrepared(context);    }}
  • 进入 EventPublishingRunListener.contextPrepared(),通过 multicastEvent 发布事件;
public void contextPrepared(ConfigurableApplicationContext context) {    //发布 ApplicationContextInitializedEvent 事件    this.initialMulticaster.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));}

3.5 配置加载完成,将监听器添加进上下文环境

  • 注意与 3.4 里的方法不同;
  • 进入 SpringApplicationRunListeners.contextLoaded() 配置加载完成;
public void contextLoaded(ConfigurableApplicationContext context) {    Iterator var2 = this.listeners.iterator();    while(var2.hasNext()) {        SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();        //【断点步入】        listener.contextLoaded(context);    }}
  • 进入 EventPublishingRunListener.contextLoaded() 将监听器添加进上下文环境;
public void contextLoaded(ConfigurableApplicationContext context) {    ApplicationListener listener;    //遍历每一个监听器(一共有13个,如下图),将除最后一个监听器外的监听器添加进 context 上下文    for(Iterator var2 = this.application.getListeners().iterator(); var2.hasNext(); context.addApplicationListener(listener)) {        listener = (ApplicationListener)var2.next();        if (listener instanceof ApplicationContextAware) {            //第10个 ParentContextCloseApplicationListener 会进来            ((ApplicationContextAware)listener).setApplicationContext(context);        }    }    //发布 ApplicationPreparedEvent 事件    this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));}

有 13 个监听器


4. 程序运行事件

  • 进入 SpringApplicationRunListeners.running() 方法,它对每一个监听器操作;
public void running(ConfigurableApplicationContext context) {    Iterator var2 = this.listeners.iterator();    while(var2.hasNext()) {        SpringApplicationRunListener listener = (SpringApplicationRunListener)var2.next();        //【断点步入】操作监听器,其中就有 EventPublishingRunListener        listener.running(context);    }}
  • 进入 EventPublishingRunListener.running() 方法,发布 ApplicationReadyEvent 事件;
public void running(ConfigurableApplicationContext context) {    context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context));}

5. Spring Cloud 启动及加载配置文件源码结构图小结

  • SpringApplication.run():加载配置上下文;
    • stopWatch.start():初始化StopWatch,调用 start 方法开始计时;
    • SpringApplicationRunListeners.starting():发布 ApplicationStartingEvent 事件;
    • SpringApplication.prepareEnvironment():准备 Environment 环境;
      • SpringApplicationRunListeners.environmentPrepared():监听环境准备事件;
        • EventPublishingRunListener.environmentPrepared():对每一个进行操作;
          • SimpleApplicationEventMulticaster.multicastEvent():使用事件主控器发布 ApplicationEnvironmentPreparedEvent 事件;
          • BootstrapApplicationListener.onApplicationEvent():bootstrap 监听器处理事件;
            • BootstrapApplicationListener.bootstrapServiceContext():给我们自动导入一些配置类;
    • SpringApplication.printBanner():打印 Banner;
    • printBanner.createApplicationContext():创建 Spring Boot 上下文;
    • SpringApplication.prepareContext():刷新应用上下文;
      • SpringApplication.applyInitializers():初始化上下文的额外操作;
        • PropertySourceBootstrapConfiguration.initialize():外部化配置;
          • NacosPropertySourceLocator.locate():读取 Nacos 服务器里的配置;
      • SpringApplicationRunListeners.contextPrepared():配置初始化完成;
        • EventPublishingRunListener.contextPrepared():发布 ApplicationContextInitializedEvent 事件;
      • SpringApplicationRunListeners.contextPrepared():配置加载完成
        • EventPublishingRunListener.contextLoaded():将监听器添加进上下文环境;
    • StopWatch.stop():停止计时;
    • SpringApplicationRunListeners.started():发布 ApplicationStartedEvent 事件;
    • SpringApplicationRunListeners.running():监听程序运行事件;
      • EventPublishingRunListener.running():发布 ApplicationReadyEvent 事件;


最后

新人制作,如有错误,欢迎指出,感激不尽!
欢迎关注公众号,会分享一些更日常的东西!
如需转载,请标注出处!
posted @ 2022-01-23 08:42 多氯环己烷 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2333 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员