spring cloud config客户端未从配置服务器加载配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30016868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:32:48  来源:igfitidea点击:

spring cloud config client not loading configuration from config server

springcloudconfig

提问by user3006967

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

我正在关注此链接: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:

我一次又一次地测试了这个,没有看到Spring cloud client正在从云服务器加载配置,请帮忙看看错误在哪里:

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

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"}}]}

  2. 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.

  1. 配置服务器是好的: 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.值":"hello123"}}]}

  2. 现在http://localhost:8080/根本无法启动。

    创建名为“configclientApplication”的 bean 时出错似乎@Value 的自动注入找不到 production.host 环境值。

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

从配置服务器加载后,如何读取客户端中的配置?

Thanks for your help.

谢谢你的帮助。

采纳答案by RubesMN

As Deinum implies, I'd ensure you have the client configured with the parentas 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:

作为Deinum意味着,我会确保你与客户端配置弹簧云起动父母,并给它一个版本。当您包含在依赖项中时,spring cloud 提供的 Maven 插件将无法工作,并记住 cloud 是与 boot 不同的项目。将其更改为:

<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

其次,作为一门新学科(在这里可能不是你的问题),我会在你的应用程序上使用新的注释而不是 @Configuration 和 @EnableAutoConfiguration

@SpringBootApplication

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

第三,仔细检查您的配置服务器上是否有 @EnableConfigServer

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

第四,确保您的客户端上的 bootstrap.properties 指定了您的 spring 应用程序名称:

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:

最后,如果您使用 spring-cloud-config 示例项目,则必须在 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:

否则,请尝试从位于此处的 spring-cloud-config 项目开始,以确保您的配置服务器设置正确:

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