SpringCloud 提供自定義的RestTemplate

2023-08-04 09:32 更新

在某些情況下,您可能需要自定義來自客戶端對配置服務(wù)器的請求。通常,這樣做涉及傳遞特殊的?Authorization?標頭以驗證對服務(wù)器的請求。提供自定義?RestTemplate?:

1. 創(chuàng)建一個新的配置bean,實現(xiàn)為?PropertySourceLocator?,如以下示例所示:

CustomConfigServiceBootstrapConfiguration.java

@Configuration
public class CustomConfigServiceBootstrapConfiguration {
    @Bean
    public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
        ConfigClientProperties clientProperties = configClientProperties();
       ConfigServicePropertySourceLocator configServicePropertySourceLocator =  new ConfigServicePropertySourceLocator(clientProperties);
        configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
        return configServicePropertySourceLocator;
    }
}

2. 在?resources/META-INF?中,創(chuàng)建一個名為?spring.factories?的文件并指定您的自定義配置,如以下示例所示:

spring.factories.  

org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號