Spring Cloud Config:客户端不会尝试连接到配置服务器

vto*_*osh 3 spring-boot spring-cloud spring-cloud-config

我正在尝试创建一个简单的 Spring Cloud Config 服务器/客户端设置,并且松散地遵循文档:

https://cloud.spring.io/spring-cloud-config/reference/html/

到目前为止,我已经实现了一个似乎可以正常工作的服务器,即当我调用相应的端点时返回正确的属性值:

GET http://localhost:8888/config-client/development

{
  "name": "config-client",
  "profiles": [
    "development"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/config/config-client-development.properties",
      "source": {
        "user.role": "Developer"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

但是,我在让客户端连接到服务器方面没有任何运气。我做了以下工作:

  1. 添加了spring-cloud-starter-config依赖项:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 添加了一个bootstrap.properties文件:
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
Run Code Online (Sandbox Code Playgroud)

但我仍然得到一个

java.lang.IllegalArgumentException: Could not resolve placeholder 'user.role' in value "${user.role}"
Run Code Online (Sandbox Code Playgroud)

尝试运行客户端应用程序时。

应用程序日志中没有任何内容看起来像是客户端正在尝试与配置服务器进行通信。

链接到重现该问题的最小GitHub 存储库:https : //github.com/Bragogirith/spring-cloud-minimal

重现步骤:

  1. 构建并运行config-service应用程序
  2. 构建并运行config-client应用程序

知道我做错了什么吗?

vto*_*osh 10

好的,谜团解决了。

似乎一周前发布了一个新的 Spring Cloud 版本(https://spring.io/blog/2020/10/07/spring-cloud-2020-0-0-m4-aka-ilford-is-available)有一种新的方法来激活引导程序 - 现在默认情况下不会发生,但需要添加额外的依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

尽管这个新版本现在是您使用 Spring Initializr 时的默认版本,但文档仍未更新以反映更改 - 它们仅在发行说明中简要提及。

作为使用上述spring-cloud-starter-bootstrap依赖项和bootstrap.properties文件的替代方案,现在似乎也可以(甚至首选):

应用程序属性

spring.application.name=config-client
spring.profiles.active=development
spring.config.import=configserver:http://localhost:8888
Run Code Online (Sandbox Code Playgroud)