3

我正在尝试使用 spring-cloud-config-client 在启动时从 spring-cloud-config-server 应用程序读取我的配置属性。我的应用程序是一个 Spring-Boot 应用程序,我需要做的是在请求被发送到配置服务器之前添加一个特定的标头。

我已阅读文档(http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html),但找不到任何方法来使用提供的 RestTemplate 自定义 ConfigServicePropertySourceLocator。

最好的方法是什么?

非常感谢

4

2 回答 2

3

扩展@spencergibb 的答案。

  • 创建一个配置类。

    @Configuration
    @ConditionalOnClass({ConfigServicePropertySourceLocator.class, RestTemplate.class})
    public class ConfigClientBootstrapConfiguration {
    
        private final ConfigServicePropertySourceLocator locator;
    
        @Autowired
        public ConfigClientBootstrapConfiguration(ConfigServicePropertySourceLocator locator) {
            this.locator = locator;
        }
    
        @PostConstruct
        public void init() {
            RestTemplate restTemplate = new RestTemplate();
            locator.setRestTemplate(restTemplate);
        }
    
    }
    
  • bootstrap.factories在子目录中创建resources/META-INF

    # Bootstrap components
    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    path.to.config.ConfigClientBootstrapConfiguration
    
于 2016-12-21T13:36:36.127 回答
2

有一个ConfigServicePropertySourceLocator.setRestTemplate()。在您的配置类中添加一个@PostConstruct您可以在RestTemplate那里设置的方法。

于 2015-05-14T18:06:00.327 回答