2015-05-14 130 views
1

我想在启动时使用spring-cloud-config-client从spring-cloud-config-server应用程序读取我的配置属性。 我的应用程序是Spring-Boot应用程序,我需要做的是在将请求发送到配置服务器之前向请求中添加特定的标头。如何使用spring-cloud-config-client配置自定义RestTemplate?

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

什么是最好的方法来做到这一点?

非常感谢

回答

1

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

+0

尝试过使用下面的要点,但它似乎属性已经被加载后@PostConstruct获取运行。是否需要某种秩序? https://gist.github.com/dwelch2344/3424b7752fbb0172ad89 –

+0

我不确定此解答如何标记为完整,因为解决方案无效。如果达成了解决方案,用实际的解决方案更新答案将会很好。 – Ceekay

1

展开@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.factoriesresources/META-INF

    # Bootstrap components 
    org.springframework.cloud.bootstrap.BootstrapConfiguration=\ 
    path.to.config.ConfigClientBootstrapConfiguration 
    
相关问题