且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

奇奇怪怪的问题-SpringCloud与SpringBoot配置文件问题

更新时间:2022-03-16 01:47:50

今天遇到一个问题,记录一下

SpringBoot版本 2.5.7

SpringCloud版本 2020.0.4

出现了 bootstrap.yml 中的配置完全不生效的情况

经过一番debug源码后,发现根本不读取bootStrap.yml配置文件,怀疑是SpringCloud有功能改动,于是查询官方文档版本更新记录: https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes

果然发现了端倪,官网解释如下:

Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.


To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument.

The other option is to include the new spring-cloud-starter-bootstrap.

在2020.0版本中移除了默认启用的Bootstrap功能,这意味着就不会再读取bootstrap.yml了

当然,官方也给出了两种解决方案

  1. 修改启动命令
    1. 在环境变量/或者启动命令中加上下面两个属性任意一个即可

spring.cloud.bootstrap.enabled=true

spring.config.use-legacy-processing=true

  1. 增加依赖

<dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-starter-bootstrap</artifactId>

</dependency>

经过测试,上面两种方案都可以解决