spring cloud config client not loading configuration from config server

こ雲淡風輕ζ 提交于 2021-02-04 13:04:26

问题


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.


回答1:


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



回答2:


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.




回答3:


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>



回答4:


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



来源:https://stackoverflow.com/questions/30016868/spring-cloud-config-client-not-loading-configuration-from-config-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!