Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring cloud config client not loading configuration from config server

I am following this link: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

I tested this again and again and not seeing Spring cloud client is loading configuration from cloud server, please help to see where is the error:

POM:

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
</dependencies>

Application:

@Configuration
@EnableAutoConfiguration
@RestController
public class ConfigclientApplication {
    
    @Value("${spring.cloud.config.uri}")
    String url;
    
    @Value("${production.host}")
    String host;
    
    
    @RequestMapping("/")
    public String home() {
        return "Host is  => " + this.host ;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigclientApplication.class, args);
    }
}

bootstrap.properties: spring.cloud.config.uri=http://localhost:8888

  1. The config server is good: http://localhost:8888/spirent/default
{
  "name": "spirent",
  "profiles": [
    "default"
  ],
  "label": "master",
  "propertySources": [
    {
      "name": "classpath:/spirent.yml",
      "source": {
        "production.host": "server1",
        "production.port": 9999,
        "production.value1": 12345,
        "test.host": "server2.com",
        "test.port": 4444,
        "test.value": "hello123"
      }
    }
  ]
}
  1. now http://localhost:8080/ can not be started at all.

    Error creating bean with name 'configclientApplication' It seemed the auto inject of @Value can not find the production.host environment value.

How can I read the configuration in client once loaded from config server?

Thanks for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using 2020.0.0 version of spring cloud than you need to this dependency in your maven dependencies to enable bootstrap, which is desabled by default in 2020.0.0.

Breaking changes in 2020.0.0

It work for me.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...