Spring Config 客户端无法从服务器获取配置

时间:2021-02-27 13:57:24

标签: spring spring-cloud spring-cloud-config-server

我的 Spring Cloud 配置客户端无法解析 server1 属性。它抛出“由:java.lang.IllegalArgumentException:无法解析值“${server1}”中的占位符“server1””

@Controller
@RefreshScope
public class OSLController {
    @Value("${server1}")
    public String oslServer; 
}

这是配置客户端的 bootstrap.properties:

spring.cloud.config.uri=http://localhost:8888
spring.profiles.active=preprod
spring.application.name=osl

这里是配置服务器的配置: 应用程序属性

server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/config/osl,classpath:/osl,classpath:/config

以下是浏览器点击时的路径: http://localhost:8888/osl/preprod

{
  "name": "osl",
  "profiles": [
    "preprod"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "class path resource [config/osl/osl-preprod.properties",
      "source": {
        "server1": "preprod"
      }
    },
    {
      "name": "class path resource [config/osl/osl.properties",
      "source": {
        "server1": "common"
      }
    }
  ]
}

当配置客户端启动时,它会打印以下日志:


2021-02-27 19:19:06.576  INFO 27040 --- [           main] in.rahul.ConfigClient2Application        : Starting ConfigClient2Application using Java 15.0.1 on DESKTOP-CJ4TSGP with PID 27040 (C:\Rahul\Workspace\ConfigClient-2\target\classes started by Rahul in C:\Rahul\Workspace\ConfigClient-2)
2021-02-27 19:19:06.579  INFO 27040 --- [           main] in.rahul.ConfigClient2Application        : No active profile set, falling back to default profiles: default
2021-02-27 19:19:07.282  INFO 27040 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d5ff5a52-737f-3fd5-8528-04cdeb4cd847
2021-02-27 19:19:07.475  INFO 27040 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

我正在遵循的教程在启动配置客户端时也会打印以下内容

Fetching config from server at: http://localhost:8888/

我的配置客户端没有出现上述消息,这让我相信配置客户端无法识别配置服务器。

有人可以指导问题以及如何解决这个问题吗?如果我能够从浏览器读取配置,为什么客户端无法连接和解析 server1 属性。

我使用的是 windows 机器。

谢谢。

0 个答案:

没有答案