【发布时间】: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
【问题讨论】:
-
你可以试试3.0.1版本的配置服务器吗,有一些已知问题已经修复github.com/spring-cloud/spring-cloud-release/wiki/…
标签: spring spring-boot spring-cloud spring-cloud-config spring-cloud-config-server