【发布时间】:2016-06-15 10:40:00
【问题描述】:
我正在尝试在 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: Could not resolve placeholder 'test.foo')
但是,如果我将属性放在基于配置服务器文件的存储库中的 hello-world-service.yml 文件中,它会完美运行。
Maven 依赖于 Spring Cloud Brixton.M5 和 Spring Boot 1.3.3.RELEASE 与
spring-cloud-starter-config和spring-cloud-config-server
【问题讨论】:
-
@AliDehghani 该问题已更新为正确的拼写
-
您说的是“基于文件的存储库”,但您没有显示它的配置,也没有说明
application.yml的位置。 -
@DaveSyer 我说的是
file-base repository,因为我使用的是nativespring profile。application.yml文件位于本地类路径中,如标准 Spring Boot 应用程序 -
如果我没记错的话,默认情况下我们不会为配置服务器自己的 application.yml 提供服务,否则任何服务都会尝试使用
server.port: 8888运行。将test.foo放在application.yml中,放在与hello-world-service.yml相同的目录中。 -
@spencergibb
hello-world-service.yml文件与application.yml位于同一目录中。我终于找到了问题的原因(见我的回答)
标签: spring spring-boot spring-cloud