问题描述
在使用spring clouod .0.0,发现对接nacos配置中心的时候,一直无法读取到bootstarp的配置信息。
问题细究
网上搜了一下,从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。
spring cloud2.4之前的源码:
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)) {}}}
spring cloud2.4之后的源码:
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;}}
对比之下,能看出问题,在spring cloud2.4之后将bootstrap.enabled设置为false。
解决方式一:
添加bootstrap的POM依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><!-- 自行修改相应版本--><version>3.0.2</version></dependency>
解决方式二:
添加环境变量:
spring.cloud.bootstrap.enabled=true
特殊说明:如果你不是spring cloud项目,只是spring boot项目,在启动无法加载到bootstrap的配置,应该为如下解决方式:
添加spring-cloud-context的POM依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId><version>3.0.1.RELEASE</version></dependency>