gpt4 book ai didi

spring - 在 Spring Cloud 配置客户端之间共享配置

转载 作者:行者123 更新时间:2023-12-02 18:21:46 26 4
gpt4 key购买 nike

我正在尝试在 Spring Cloud 客户端与具有基于文件的存储库的 Spring Cloud 配置服务器之间共享配置:

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {

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

// application.yml
server:
port: 8888

spring:
profiles:
active: native

test:
foo: world

我的一个Spring Cloud客户端使用在配置服务器中定义的test.foo配置,其配置如下:

@SpringBootApplication
@RestController
public class HelloWorldServiceApplication {

@Value("${test.foo}")
private String foo;

@RequestMapping(path = "/", method = RequestMethod.GET)
@ResponseBody
public String helloWorld() {
return "Hello " + this.foo;
}

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

// boostrap.yml
spring:
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
fail-fast: true

// application.yml
spring:
application:
name: hello-world-service

尽管有这样的配置,Spring Cloud 客户端中的 Environment 并不包含 test.foo 条目(参见 java .lang.IllegalArgumentException:无法解析占位符“test.foo”)

但是,如果我将属性放入我的配置服务器基于文件的存储库中的 hello-world-service.yml 文件中,它就可以完美工作。

Maven dependencies on Spring Cloud Brixton.M5 and Spring Boot 1.3.3.RELEASE with spring-cloud-starter-config and spring-cloud-config-server

最佳答案

来自Spring Cloud documentation

With the "native" profile (local file system backend) it is recommended that you use an explicit search location that isn’t part of the server’s own configuration. Otherwise the application* resources in the default search locations are removed because they are part of the server.

所以我应该将共享配置放在外部目录中,并将路径添加到config-serverapplication.yml文件中。

// application.yml
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:/Users/herau/config-repo

// /Users/herau/config-repo/application.yml
test:
foo: world

关于spring - 在 Spring Cloud 配置客户端之间共享配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com