Spring Cloud配置客户端未从配置服务器加载配置

标签 spring cloud config

我正在点击此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

我测试了一遍又一遍,没有看到Spring cloud客户端正在从云服务器加载配置,请帮忙看看错误在哪里:

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>

应用:

@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. 配置服务器很好: 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"
      }
    }
  ]
}
  • 现在http://localhost:8080/根本无法启动。

    创建名称为“configclientApplication”的 bean 时出错 看来@Value的自动注入(inject)找不到product.host环境值。

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

    感谢您的帮助。

    最佳答案

    如果您使用的是 2020.0.0 版本的 Spring Cloud,则需要将此依赖项添加到您的 Maven 依赖项中以启用配置 Bootstrap ,该功能在 2020.0.0 中默认禁用。

    Breaking changes in 2020.0.0

    这对我有用。

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

    关于Spring Cloud配置客户端未从配置服务器加载配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016868/

    相关文章:

    amazon-web-services - 谷歌云有没有像AWS一样的存储网关概念?

    javascript - 如何从视频元素中删除“下载”按钮?

    Spring @Transactional - javax.persistence.TransactionRequiredException

    spring - 在 Spring-MVC Tomcat webapp 中嵌入 ActiveMQ 代理

    spring - 动态 Spring 特性

    laravel - 为什么在缓存配置文件时不使用 env() 助手是 "strongly recommended"?

    json - Keycloak:是否可以在配置 json 文件中编写脚本组?

    Spring mvc HandlerMapping VS HandlerAdapter

    spring - 如何覆盖Spring Cloud Ribbon中的ribbon.serverListRefreshInterval默认值?

    linux - Nginx 仍然尝试打开默认错误日志文件,即使我在重新加载时设置了 nginx 配置文件