在 Spring 云配置中选择特定配置文件时不返回默认配置文件值

Default profile values are not returned when specific profile selected in Spring Cloud Config

我正在从本地 application.yml 配置文件移动到配置服务器管理的配置。

我的 application.yml 文件在同一文件中包含 2 个(或更多)配置文件,格式为:

spring.application.name: config-client
app.myvar1: "Var 1 in default profile"
app.myvar2: "Var 2 in default profile"
---
spring.config.activate.on-profile: docker
app.myvar1: "Var 1 in docker profile"

当我在非配置服务器环境中测试此配置文件时,我从特定配置文件中读取结果,如果未找到,则从默认值读取。样本

我在没有配置服务器的情况下进行测试时的正确结果

Profile: docker
MyVar1=Var 1 in docker profile
MyVar2=Var 2 in default profile

为了测试,我使用了一个简单的程序:

package com.bthinking.configclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    @Autowired
    private Environment environment;

    @Value("${app.myvar1:MyVar1 not found}")
    String myVar1;
    @Value("${app.myvar2:MyVar2 not found}")
    String myVar2;

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @RequestMapping("/")
    public String index() {
        StringBuffer b = new StringBuffer();
        for (String profile : environment.getActiveProfiles()) {
            b.append(String.format("Profile: %s</br>", profile));
        }
        b.append(String.format("MyVar1=%s</br>", myVar1));
        b.append(String.format("MyVar2=%s</br>", myVar2));
        return b.toString();
    }
}

然后我开始程序(我使用 gradle):

gradle :config-client:bootRun --args='--spring.profiles.active=docker'

但是,当我迁移到配置服务器,将文件移动到配置存储库(我正在使用基于文件的报告)时,我得到了无效结果(它无法读取默认部分中的变量)。我也试过--spring.profiles.active=docker,默认没有改变

Profile: docker
MyVar1=Var 1 in docker profile
MyVar2=MyVar2 not found

作为参考,我的配置服务器具有以下配置:

server.port: 8888

spring.application.name: config-server

spring.cloud.config.server.native.searchLocations: file:${PWD}/config-repo
# spring.cloud.config.server.native.searchLocations: file:/Users/jmalbarran/Projects/BTH/BTH/SPB_SpringBoot/bugs/config/config-repo

logging.level:
  root: debug
---
spring.config.activate.on-profile: docker
spring.cloud.config.server.native.searchLocations: file:/config-repo

主要class

package com.bthinking.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

还有 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.b-thinking'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', "2020.0.0")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

我开始:

gradle :config-server:bootRun --args='--spring.profiles.active=native'

注意:由于我认为这是一个错误,我还在 github 中创建了一个问题。签到 Issue

@spencergibb(谢谢!)评论后,我尝试使用 3.0.1 版本,它解决了问题。

现在这是我的 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.1'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.b-thinking'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', "2020.0.0")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server:3.0.1'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

太棒了,因为据报道 3.0.1 版本解决了相反的错误(默认配置覆盖了特定的),但我想评论解决了这两个问题。

作为参考,这是相关的问题

Issue#1777 Profile not correct with SpringBoot 2.4.1 + Ilford 2020.0.0 (working with 2.4.1 + Ilford 2020.0.0-RC1)

Issue@1778 multidocument files from config server have the same property name