700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Cloud——Spring Cloud Alibaba Nacos Config bootstrap 配置文件失效解决方案

Spring Cloud——Spring Cloud Alibaba Nacos Config bootstrap 配置文件失效解决方案

时间:2023-05-10 05:08:17

相关推荐

Spring Cloud——Spring Cloud Alibaba  Nacos Config bootstrap 配置文件失效解决方案

基本概念

微服务是基于Spring Cloud框架搭建的,Spring Cloud Config作为服务配置中心。

业务服务只配置服务名称、启用环境和config的URL地址,其他都配置在配置中心,例如服务端口、服务注册中心地址等。可在开发环境(dev)、测试环境(test)和生产环境(prod)分别配置。

所以预想的启动流程是:先加载配置文件,再启动服务。

添加配置文件:bootstrap.properties或者bootstrap.yml。

环境配置

Spring Boot 2.4.5

Spring Cloud .0.0

Spring Cloud Alibaba .1

问题描述

没有加载到bootstrap.properties文件,bootstrap.yml同样无效。

问题分析

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

另外也有配置的默认值变化,如下:

Spring Boot 2.3.8.RELEASE

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 Boot 2.4.2

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;}

官方文档

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-first-bootstrap

解决方案

方法一:添加依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>

原来的bootstrap.properties或者bootstrap.yml不需要任何修改。

方法二:设置启动参数或者环境变量

IDEA环境: select Run/Debug Configurations > Override parameters 中添加 spring.cloud.bootstrap.enabled=true

命令列: java -jar -Dspring.cloud.bootstrap.enabled=true gateway.jar

参考文章

Spring Cloud bootstrap 配置文件失效

Spring Cloud .0.0 版本 bootstrap.yml 失效的原因與解決

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。