【发布时间】:2019-10-15 07:36:50
【问题描述】:
我一直在关注 Baeldung 的 this 教程来设置 Spring Cloud Config 服务器和客户端。我相信我已经正确设置了服务器,因为转到 http://localhost:8888/service-config/development/master 似乎会正确显示定义了 user.role 的 json:
{"name":"service-config","profiles":["development"],"label":"master","version":"d30a6015a6b8cb1925f6e61cee22721f331e5783","state":null,"propertySources":[{"name":"file:///C:.../git/service-config-data/service-config-development.properties","source":{"user.role":"Developer"}},{"name":"file:///C:.../git/service-config-data/service-config.properties","source":{"user.role":"Developer"}}]}
但是,我似乎无法将 Spring Cloud Config 客户端正确连接到服务器 - 客户端无法运行,无法解析 @Value("${user.role}") 注释上的占位符错误。我尝试向注释添加默认值,然后手动刷新值(使用@refreshscope 注释),但无济于事。
我通过运行带有--debug --trace 标志的工具查看了调试和跟踪日志,但我找不到客户端何时/是否正在调用服务器以及服务器实际上是哪个 config-data .properties 文件寻找。我对如何进行有点迷茫。
这是我的服务器application.properties:
spring.application.name=service-config
server.port=8888
spring.cloud.config.server.git.uri=file:///${user.home}/git/service-config-data
spring.cloud.config.server.git.clone-on-start=false
spring.security.user.name=root
spring.security.user.password=toor
在service-config-data 文件夹内,有这样的文件:
user.role=Developer
我已经尝试过名为 service-config.properties、service-config-client.properties、service-config-development.properties 和 service-config-client-development.properties 的文件,但似乎没有一个能够通过user.role.
这是我的客户:
@SpringBootApplication
@RestController
@RefreshScope
public class ServiceConfigClient {
@Value("${user.role:unknown}")
private String role;
@RequestMapping(
value = "/whoami/{username}",
method = RequestMethod.GET,
produces = MediaType.TEXT_PLAIN_VALUE)
public String whoami(@PathVariable("username") String username) {
return String.format("Hello! You're %s and you'll become a(n) %s...\n", username, role);
}
}
还有我的客户bootstrap.properties:
spring.application.name=service-config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=root
spring.cloud.config.password=toor
management.endpoints.web.exposure.include=*
如何将客户端正确连接到服务器,才能成功拉取配置数据?谢谢。
【问题讨论】:
标签: spring spring-boot spring-cloud-config