spring-boot – spring-cloud-consul:无法覆盖ConsulProperties中的默认值

我正在使用spring cloud consul 1.0.0.M5和spring boot 1.3.2. spring cloud consul用于连接到consul代理的默认主机和端口是localhost:8500.我想改变这一点,但到目前为止我尝试的每种方法都不尊重这些设置.

我查看了spring cloud consul的源代码,并使用spring-boot中的@ConfigurationProperties在属性前缀spring.cloud.consul中读取了ConsulProperties.

以下是ConsulProperties的Spring云源中的声明:

/**
 * @author Spencer Gibb
 */
@ConfigurationProperties("spring.cloud.consul")
@Data
public class ConsulProperties {
    @NotNull
    private String host = "localhost";

    @NotNull
    private int port = 8500;

    private boolean enabled = true;

    private String prefix = "config";
}

以及如何在spring cloud consul源中注入它以定义向咨询代理发出http请求的ConsulClient:

/**
 * @author Spencer Gibb
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnConsulEnabled
public class ConsulAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ConsulProperties consulProperties() {
        return new ConsulProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    public ConsulClient consulClient() {
        return new ConsulClient(consulProperties().getHost(), consulProperties()
                .getPort());
    }

...

我在application.properties中放置了以下内容,但忽略了每个组合,而是使用默认的“localhost”和“8500”.在这种情况下,我认为“spring.cloud.consul.host”和“spring.cloud.consul.port”会受到尊重.

spring.cloud.consul.config.host=172.17.42.1
spring.cloud.consul.config.port=8500
config.host=172.17.42.1
config.port=8500
spring.cloud.consul.host=172.17.42.1
spring.cloud.consul.port=8500

我还尝试显式定义ConsulProperties bean,因为它在spring配置中被标记为@ConditionalOnMissingBean,但是也没有读取这个bean.在那种情况下我不得不绕过类型系统,因为所有字段都是私有的(呃).以下是我的代码中配置类的片段:

...
import org.apache.commons.lang3.reflect.FieldUtils;
...
@Configuration
@EnableDiscoveryClient
@Order(Ordered.HIGHEST_PRECEDENCE)
@Priority(Ordered.HIGHEST_PRECEDENCE)
public class ConsulConfig {
...

    @Bean
    public ConsulProperties consulProperties() {
        ConsulProperties props;

        try {
            props = new ConsulProperties();

            String host = resolveConsulHost();
            int port = this.consulPort;

            FieldUtils.writeField(props, "host", host,true);
            FieldUtils.writeField(props, "port", port,true);

        }
        catch(Exception e) {
            logger.warn("Unable to set ConsulProperties - using defaults",e);
            props = new ConsulProperties();
        }

        logger.info("Using consulProperties={}",props);

        return props;

    }

...
}

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan
public class Application {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
...
}

如何定制spring cloud consul用于主机和端口连接到领事代理的内容?

最佳答案
迁移评论回答:

如果您使用的是spring-cloud-starter-consul-all或spring-cloud-starter-consul-config,则需要将值放在bootstrap.properties中.

这是documentation.要点,spring.application.name以及连接到领事所需的一切.

转载注明原文:spring-boot – spring-cloud-consul:无法覆盖ConsulProperties中的默认值 - 代码日志