【问题标题】:Default profile values are not returned when specific profile selected in Spring Cloud Config在 Spring Cloud Config 中选择特定配置文件时,不返回默认配置文件值
【发布时间】:2021-04-18 20:08:18
【问题描述】:

我正在从本地 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 环境中测试此配置文件时,我会从特定配置文件中读取结果,如果未找到,则从默认配置文件中读取。样本

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

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

【问题讨论】:

标签: spring spring-boot spring-cloud spring-cloud-config spring-cloud-config-server


【解决方案1】:

在@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

【讨论】:

    猜你喜欢
    • 2023-02-15
    • 2016-03-21
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2016-07-12
    • 2022-10-19
    相关资源
    最近更新 更多