21
votes

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.

4
If you are following a tutorial then follow a tutorial. The tutorial mentions that you should use the spring-cloud-starter dependency not the spring-cloud-config-client. For the application it doesn't matter where the configuration comes from, they are just properties which you can access like any other property through the Environment or with @Value. - M. Deinum
I changed to both client or starter, but can not find out configuration value in the config server, any idea? - user3006967
were you able to resolve the issue, the problem I am facing is the properties from config server are begin read from the method if it annotated with @RequestMapping but not otherwise. not sure what can be done in that case. - viveksinghggits

4 Answers

7
votes

As Deinum implies, I'd ensure you have the client configured with the parent as spring-cloud-starter-parent and give it a version. Maven plugins provided by spring cloud wont work when you include in your dependencies and remember cloud is a different project than boot. Change it to:

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>1.0.0.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

Second, as a new discipline (and likely not your problem here), I'd use the new annotation on your Application instead of @Configuration and @EnableAutoConfiguration

@SpringBootApplication

Third, double check you have @EnableConfigServer on your config server

Fourth, be sure your bootstrap.properties on your client has your spring application name specified:

spring.application.name=spirent

Finally, if you used the spring-cloud-config example project, there is a default user and security password that you have to set in your URI:

http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888

Otherwise, try starting from the spring-cloud-config project located here to ensure your config server is setup correctly:

https://github.com/spring-cloud/spring-cloud-config
31
votes

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>
24
votes

For those getting here after using the default Maven Spring Cloud dependency version related to Spring Boot to 2.4.0 or 2.4.1 (e.g., 2020.0.0-M5 for Spring Boot 2.4.0), besides following RubesMN's good advice, be aware that bootstrapping is not enabled by default in such Spring Cloud dependency. According to the Spring Cloud 2020.0 Release Notes:

Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.

  • To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument.
  • The other option is to include the new spring-cloud-starter-bootstrap (in your POM file).

I used the second option and worked just fine.

8
votes

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

Yes this is correct and this worked for me ... thanks