关于spring-cloud:Spring-Cloud-2020-bootstrap-配置文件失效

6次阅读

共计 2818 个字符,预计需要花费 8 分钟才能阅读完成。

Spring Cloud 2020 版本 bootstrap 配置文件(properties 或者 yml)有效

如何解决?

背景介绍

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

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

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

之前的做法是,将配置文件名称改为:bootstrap.properties。

问题

之前间接就能够用,而当初,启动的端口是 8080,显著没有加载到 bootstrap.properties 文件,我认为我的文件名字写错了,核查了几次,确认无误,我猜测预计是 bootstramp.properties 配置文件没有失效。

之前的版本:

spring boot 2.3.1.RELEASE

spring cloud Hoxton.SR4

以后版本:

spring boot 2.4.2

spring cloud 2020.0.1

查找起因

依据下面呈现的问题,我应用百度搜寻了下,大略的起因晓得了:从 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;
    }

传统解决方案

其实官网说得很明确。看上面这段:

Config First Bootstrap

To use the legacy bootstrap way of connecting to Config Server, bootstrap must be enabled via a property or the spring-cloud-starter-bootstrap starter. The property is spring.cloud.bootstrap.enabled=true. It must be set as a System Property or environment variable. Once bootstrap has been enabled any application with Spring Cloud Config Client on the classpath will connect to Config Server as follows: When a config client starts, it binds to the Config Server (through the spring.cloud.config.uri bootstrap configuration property) and initializes Spring Environment with remote property sources.

The net result of this behavior is that all client applications that want to consume the Config Server need a bootstrap.yml (or an environment variable) with the server address set in spring.cloud.config.uri (it defaults to “http://localhost:8888”).

两个关键点:

1、加一个依赖:spring-cloud-starter-bootstrap

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

2、加一个配置:spring.cloud.config.uri

bootstrap.properties

# 利用名称
spring.application.name=erwin-cloud-user
# 启用环境
spring.profiles.active=dev

# 配置文件
spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.uri=http://localhost:9000

解决方案

当初,你只须要这样:

application.properties

# 利用名称
spring.application.name=erwin-cloud-user
# 启用环境
spring.profiles.active=dev

spring.config.import=optional:configserver:http://localhost:9000

spring.cloud.config.label=${spring.application.name}
spring.cloud.config.name=${spring.application.name}
spring.cloud.config.profile=${spring.profiles.active}
正文完
 0