问题描述
开发的Spring Alibaba Cloud项目,依照阿里巴巴官方提供的毕业依赖关系做了依赖版本的升级,升级后启动应用服务失败。发现bootstrap.yml中所配置的信息都没有生效。
问题的原因
网上搜了一下,从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。
另外也有配置的默认值变化,原来默认启用 true 现在变更为 false 如下:
version:2.2.5
package org.springframework.cloud.bootstrap;public class BootstrapApplicationListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {ConfigurableEnvironment environment = event.getEnvironment();if ((Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) {}}}
version:2.4.2 ```java package org.springframework.cloud.util; public abstract class PropertyUtils { public static boolean bootstrapEnabled(Environment environment) {
return (Boolean)environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, false) || MARKER_CLASS_EXISTS;
} }
<a name="Igmcj"></a>## 解决方案:传统的解决方案- 官方文档:[点击访问](https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-data-import)<br /><a name="Ye7mi"></a>### 方案1:引用依赖pom.xml中引用 `spring-cloud-starter-bootstrap` 依赖```shell<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><version>3.0.2</version></dependency>
可以在maven仓库中自行找对应的版本信息
方案2:指定运行参数
指定参数 spring.cloud.bootstrap.enabled 值为 true
IDEA中:spring.cloud.bootstrap.enabled=true---命令行:java -jar -Dspring.cloud.bootstrap.enabled=true test.jar

配置完成后再次启动即可成功运行
