SpringCloud 提供自定義的RestTemplate

2023-08-04 09:32 更新

在某些情況下,您可能需要自定義來(lái)自客戶端對(duì)配置服務(wù)器的請(qǐng)求。通常,這樣做涉及傳遞特殊的?Authorization?標(biāo)頭以驗(yàn)證對(duì)服務(wù)器的請(qǐng)求。提供自定義?RestTemplate?:

1. 創(chuàng)建一個(gè)新的配置bean,實(shí)現(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)建一個(gè)名為?spring.factories?的文件并指定您的自定義配置,如以下示例所示:

spring.factories.  

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


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)