简体   繁体   中英

Spring Boot Config Client is not getting property from Config Server

I was able to start Config Server, pointing my browser to http://localhost:8888/example/default , gives me:

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

I assume the above should be enough to see that Config Server is up and running, and it contains "example.mykey" property, which I'm planning to use in the example client app. (Let me know if config server internals are also needed here).

The client app is as follows:

@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;
        }
    }
}

The client app also has a bootstrap.yaml in its src/main/resources dir:

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

In my understanding, the above client app is configured to use 'example' app's 'default' profile configuration from config server. Yet, the this application fails with:

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]

Why cannot it take this property from the Config Server? What am I missing?

=== UPDATE. Adding also client pom.xml, as requested in comments ===

<?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>

After spending the whole day in vain, I finally tried something that made it work. Server config uri comes under spring.config.uri as opposed to non-working spring.cloud.config.uri (which is given in most examples). Maybe this has changed in spring boot 2.0.5, but this yaml works:

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

Faced a similar issue, you are missing a dependency org.springframework.cloud:spring-cloud-starter-config

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

and then proceed with yml

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM