首页 > 解决方案 > 在 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"

当我在 NOT config-server 环境中测试此配置文件时,我会从特定配置文件中读取结果,如果未找到,则从默认配置文件中读取。样本

当我在没有 config-server 的情况下进行测试时结果正确

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'

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

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

主要班

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 中创建了一个问题。签入问题

标签: springspring-bootspring-cloudspring-cloud-configspring-cloud-config-server

解决方案


在@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 版本解决了相反的错误(默认配置覆盖了特定的),但我想审查解决了这两个问题。

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

问题#1777 配置文件与 SpringBoot 2.4.1 + Ilford 2020.0.0 不正确(使用 2.4.1 + Ilford 2020.0.0-RC1)

来自配置服务器的 Issue@1778 多文档文件具有相同的属性名称


推荐阅读