Spring Cloud 2020 bootstrap 配置文件失效

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}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章