【问题标题】:Configure spring.* boot properties from cloud config server从云配置服务器配置 spring.* 启动属性
【发布时间】:2018-04-14 07:46:57
【问题描述】:

这似乎是一个非常基本的问题,但我在任何地方都找不到直接的答案。

如何从 Cloud Config Server 实例配置基本的 Spring Boot 属性?显然,与配置服务器相关的 Spring boot 属性(即 cloud.config.uri、*.username、*.password)必须在 bootstrap.yml 中

这里的其他属性呢:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#common-application-properties

我希望 spring.rabbitmq.addresses 等来自我的配置服务器实例,而不是硬编码到引导程序或应用程序 yml 中。

如果我把那个特定的属性放到我的 Git Repo 中,Spring AMQP 仍然默认为 localhost。在我看来,Git 中的设置完全被忽略了。

【问题讨论】:

  • 您提到了bootstrap.yml 设置cloud.config.uri 属性。 bootstrap.yml中访问Cloud Config服务器所需的属性实际上是spring.application.namespring.cloud.config.urispring.cloud.config.label
  • 是的,我很简短。我与云配置服务器的连接正常。我的整个应用程序目前可以正常工作,@Values 与配置服务器的设置相连接。

标签: spring-boot spring-cloud-config


【解决方案1】:

所以对代码的调查显示我的问题是基于一些错误的假设。 Spring 看起来没有任何潜在的统一性。* Spring Boot 的属性。

例如,我所指的 Spring Rabbit 属性来自 Rabbit 特定对象:org.springframework.boot.autoconfigure.amqp.RabbitProperties

这由 RabbitAutoConfiguration.java 引入。 RabbitProperties 当然没有任何东西可以引用 Spring Cloud Config Server 值。我仍然需要追踪 RabbitProperties 是在哪里制作的 bean。

目前,我破解了自己的 RabbitProperties bean 实现并将其标记为 @Primary。我只是使用标准的@Value 从云配置服务器中提取我想要的值。这会让我通过我正在做的 POC,但这是一个 hack,因为 RabbitProperties 有大量可能的配置。我当然不想复制那里的所有内容。

 @EnableRabbit
 @Configuration
 public class RabbitConfigurer {

     @Value("${spring.rabbitmq.host:'localhost'}")
     private String host;

     @Value("${spring.rabbitmq.port:5672}")
     private int port;

     @Value("${spring.rabbitmq.username}")
     private String username;

     @Value("${spring.rabbitmq.password}")
     private String password;

     @Bean
     @Primary
     public RabbitProperties rabbitProperties() {

        RabbitProperties rabbitProperties = new RabbitProperties();
        rabbitProperties.setHost(host);
        rabbitProperties.setPort(port);
        rabbitProperties.setUsername(username);
        rabbitProperties.setPassword(password);

        return rabbitProperties;
     }
 }

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2015-01-03
    • 1970-01-01
    • 2016-02-07
    • 2016-01-18
    • 2019-07-21
    • 2019-12-31
    • 1970-01-01
    • 2015-06-02
    相关资源
    最近更新 更多