知方号

知方号

SpringCloud项目无法读取bootstrap.yml配置文件的解决办法<无法加载本地配置文件怎么办>

SpringCloud项目无法读取bootstrap.yml配置文件的解决办法

问题描述

在使用spring clouod 2023.0.0,发现对接nacos配置中心的时候,一直无法读取到bootstarp的配置信息。

问题细究

网上搜了一下,从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。

spring cloud2.4之前的源码:

package org.springframework.cloud.bootstrap;public class BootstrapApplicationListener implements ApplicationListener, 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依赖:

org.springframework.cloud spring-cloud-starter-bootstrap 3.0.2 解决方式二:

添加环境变量:

spring.cloud.bootstrap.enabled=true 

特殊说明:如果你不是spring cloud项目,只是spring boot项目,在启动无法加载到bootstrap的配置,应该为如下解决方式:

添加spring-cloud-context的POM依赖

org.springframework.cloud spring-cloud-context 3.0.1.RELEASE

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。