【问题标题】:Spring Cloud Config client doesn't get values from serverSpring Cloud Config 客户端没有从服务器获取值
【发布时间】: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


    【解决方案1】:

    配置服务器中的客户端服务名称和相应的属性来源存在混淆。

    您需要执行以下操作之一:

    1. 更改客户端服务名称:
    //client bootstrap.properties
    spring.application.name=service-config
    
    1. 将 git repo 中的文件夹名称从 service-config 更改为 service-config-client

    【讨论】:

    • 感谢您的回答。我尝试了第一个选项,但这没有任何影响 - 我仍然是未知的,而不是“开发者”的拉通值。对于第二个选项,您指的是哪个文件夹? service-config 文件夹是服务器的,service-config-client 是客户端,service-config-data 是配置存储。哪个需要重命名?
    • @SWilliams 抱歉,我有几个小时没在线了。很高兴听到您的问题已解决。
    【解决方案2】:

    我的问题通过将spring.cloud.config.name=service-config 添加到服务配置客户端的bootstrap.properties 以及添加spring-cloud-config-client 并从我的pom.xml 中删除spring-cloud-config-server 得到解决

    【讨论】:

    猜你喜欢
    • 2016-11-26
    • 2015-07-13
    • 1970-01-01
    • 2020-02-04
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多