一文带你了解,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的配置文件,好了,怎么读取就是这么来的。

相关推荐
seven97_top2 小时前
SpringCloud 常见面试题(一)
后端·spring·spring cloud
楠枬9 小时前
Spring Cloud 概述
java·spring cloud·微服务
sg_knight1 天前
RabbitMQ 中的预取值(prefetch)详解:如何真正提升消费端性能?
java·spring boot·spring·spring cloud·消息队列·rabbitmq·预取值
pengzhuofan1 天前
微服务初识:核心概念与SpringCloud生态
spring cloud·微服务·架构
hongweihao1 天前
Kafka 消息积压了,同事跑路了
后端·spring cloud·kafka
zl9798991 天前
SpringCloud-LoadBalancer负载均衡服务调用
spring·spring cloud·负载均衡
小七mod1 天前
【微服务】微服务架构演进
分布式·spring·spring cloud·微服务·云原生·架构·单体架构
麦兜*1 天前
Redis在Web3中的应用探索:作为链下状态缓存与索引层
java·spring boot·redis·spring cloud·缓存·docker·web3
ruleslol2 天前
SpringCloud03-Eureka02-搭建Eureka服务
spring cloud·eureka
脸大是真的好~2 天前
尚硅谷 SpringCloud 01 分布式概念-工程创建-nacos安装-nacos服务注册与发现-远程调用-负载均衡注解版-配置中心-动态刷新-环境隔离
分布式·spring·spring cloud