2015-05-14 83 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 
    
相關問題