Spring Boot Config Client无法从Config Server获取属性

时间:2018-09-19 15:20:48

标签: spring-boot spring-cloud-config

我能够启动Config Server,将浏览器指向http://localhost:8888/example/default,这给了我

{
  "name": "example",
  "profiles": [
    "default"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/example.yaml",
      "source": {
        "example.mykey": "myvalue"
      }
    }
  ]
}

我认为以上内容足以确保Config Server已启动并正在运行,并且其中包含“ example.mykey”属性,我计划在示例客户端应用程序中使用该属性。 (让我知道这里是否还需要配置服务器内部)。

客户端应用程序如下:

@SpringBootApplication
public class ExampleClientConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExampleClientConfigApplication.class, args);
    }

    @RestController
    class ExampleController {

        @Value("${example.mykey}")
        private String value;

        @RequestMapping
        public String sayValue() {
            return value;
        }
    }
}

客户端应用程序的src / main / resources目录中还包含bootstrap.yaml:

spring:
  application:
    name: example
  cloud:
    config:
      enabled: true
      uri: http://localhost:8888

据我了解,上述客户端应用程序已配置为使用配置服务器中“示例”应用程序的“默认”配置文件配置。但是,此应用程序失败并显示:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'example.mykey' in value "${example.mykey}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]

为什么不能从配置服务器获取此属性?我想念什么?

===更新。根据评论===

的要求,还添加客户端pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.jnetx</groupId>
<artifactId>example-config-client</artifactId>
<version>1.0.0</version>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <!-- Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
</project>

2 个答案:

答案 0 :(得分:0)

一整天徒劳无功之后,我终于尝试了一些使之奏效的方法。服务器配置uri属于spring.config.uri,而不是不起作用的spring.cloud.config.uri(在大多数示例中给出)。也许这在Spring Boot 2.0.5中已更改,但是此yaml可以工作:

spring:
  application:
    name: example
  config:
    enabled: true
    uri: http://localhost:8888

答案 1 :(得分:0)

遇到类似问题,您缺少依赖项 org.springframework.cloud:spring-cloud-starter-config

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

然后继续yml

spring:
  application:
    name: example
  cloud:
    config:
      enabled: true
      uri: http://localhost:8888