一文带你了解,springcloude框架中bootstrap相关配置文件如何加载

首先这也是自己第一次发文章,自己口才文采也那样,主要自己喜欢死磕东西。

最近新项目中,有个问题一直困扰我,就是明明已经在bootstrasp.properties配置文件中,配了关闭服务启动时es心跳检测,但是启动项目还是报了发送es心跳错误,自己狠是恼火,明明已经关了es心跳配置,但是没有起到效果,自己有些头疼,

由于项目中引入,org.springframework.boot.actuate.autoconfigure,这个组件依赖,这个挺好用,是spring官方提供的,可以监控各种中间件心跳,其中es的心跳监控源码是通过这个注解判断@ConditionalOnEnabledHealthIndicator("elasticsearch"),可以继续点进去,看到@Conditional(OnEnabledHealthIndicatorCondition.class),继续进入

OnEnabledHealthIndicatorCondition这个类,你就可以看到这个类全貌,

java 复制代码
class OnEnabledHealthIndicatorCondition extends OnEndpointElementCondition {</p>

   OnEnabledHealthIndicatorCondition() {
      super("management.health.", ConditionalOnEnabledHealthIndicator.class);
   }</p>

}

看到这是不是自己可以实现一个类似@ConditionalOnEnabledHealthIndicator的条件注解,模仿OnEnabledHealthIndicatorCondition就可以,这是题外话,也就是只要配了management.health.elasticsearch.enabled=false,就不会往es发心跳,但是我在bootstrap.properties明明已经配了啊,就是没生效,嗯,感觉世界很神奇,

其实自己犯了一个很大错,就是这个项目不是springcloud项目,哈哈,真的是到最后才发现,spring-boot项目不会读取bootstrap的配置文件。开始进入正题,那么在spring-cloud框架这个配置文件怎么读取呢,自己比较笨,debug半天才看出来的,直接到启动类,SpringApplication.run,跟着spring启动走一圈就知道了。这个run里边刚开始创建springapplication的容器,

java 复制代码
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
   return new SpringApplication(primarySources).run(args);
}

springapplication的构造方法有重要起步就是,注册监听器,

可以跟进getSpringFactoriesInstances(ApplicationListener.class)这个方法中,

获取所有实现ApplicationListener类,怎么获取呢,点进去据可以知道,

这个FACTORIES_RESOURCE_LOCATION就是META-INF/spring.factories,也就是说他要获取所有spring.factories,并找到所有实现ApplicationListener的类,我们直接看spring-cloude-context这个包的spring.factories,

有个让人眼前一亮监听器,BootstrapApplicationListener,bootstrap的文件和这是有关系的,好了这个是创建容器,接着跟着run继续往下走。

这个代码spring框架人,再熟悉不过了吧,什么源码阅读,refreshContext(context)再熟悉不过了吧,对spring的整个生命周期都在这里,今天咋不研究着,就看怎么解析bootstrasp怎么解析的。

java 复制代码
SpringApplicationRunListeners listeners = getRunListeners(args);

这个很重要,spring的事件管理就是通过这个类。

获取的是EventPublishingRunListener这个,用于事件发布,可以看看这个类的方法,你会很吃惊。像是开启了大门。

各种spring内部事件,可以看看这个类,其中在spring的环境准备阶段会发布一个事件,

而事件监听者有很多个,第一个监听器就是,BootstrapApplicationListener,

可以看这两处代码,第一个获取配置文件名字,第二个代码有个重要一步就是

将配置文件名字放到环境对象的配置中,

这个块代码又把spring的run又走了一遍不是很理解,

这次走的时候,也是发布环境准备事件,直接看ConfigFileApplicationListener,这个监听器,这个就是解析bootstrasp配置文件的类,大家可以直接这个类就行,不用一步一步跟着,

老规矩,监听器,直接看onApplicationEvent方法,由于是环境准备事件,直接进第一判断就行。

获取所有后置处理器,调用其

java 复制代码
postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());

但是你惊奇发现这个ConfigFileApplicationListener监听器,自己也实现EnvironmentPostProcessor

世界就是很奇妙,里边有个load方法,

题外话,这个类也加载其他配置文件,比如通过maven插件动态配置开发环境,加载不同环境配置也在这,

直接进入这个load方法,里边有个getSearchNames(),里边就是获取配置文件名字,就是在环境对象里边放的bootstrap,

题外话,如果,没有配置,获取到的配置文件名字是什么,看看下面最后一行,DEFAULT_NAMES就是 application,后面applicationk就是这么解析的

下面这个就是加载bootstrap.application,bootstrap.yml的配置文件,好了,怎么读取就是这么来的。

相关推荐
鸽鸽程序猿7 小时前
【JavaEE】【SpringCloud】注册中心_nacos
java·spring cloud·java-ee
递归尽头是星辰10 小时前
Spring Cloud Alibaba 核心理论体系:Nacos、Sentinel、Seata深度解析
spring cloud·nacos·sentinel·seata·微服务治理
lpfasd12310 小时前
springcloud docker 部署问题排查与解决方案
spring·spring cloud·docker
蓝眸少年CY12 小时前
(第七篇)spring cloud之Hystrix断路器
spring·spring cloud·hystrix
蓝眸少年CY13 小时前
(第八篇)spring cloud之zuul路由网关
后端·spring·spring cloud
码出财富1 天前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
daladongba1 天前
Spring Cloud Gateway
java·spring cloud·gateway
梁bk1 天前
[spring cloud] Seata分布式事务管理
分布式·spring·spring cloud
张二狗和苗翠花2 天前
Spring Cloud Nacos + @RefreshScope + @Value实现配置项动态刷新
spring·spring cloud
资生算法程序员_畅想家_剑魔2 天前
Java常见技术分享-分布式篇-SpringCloud-01-基础组件
java·分布式·spring cloud