[英]How to configure a custom RestTemplate when using spring-cloud-config-client?
I am trying to use spring-cloud-config-client to read my configuration properties from a spring-cloud-config-server application on startup. 我正在尝试使用spring-cloud-config-client在启动时从spring-cloud-config-server应用程序读取我的配置属性。 My application is a Spring-Boot application and what I need to do is adding a specific header to the request before it is sent to the config server. 我的应用程序是Spring-Boot应用程序,我需要做的是在请求发送到配置服务器之前向请求添加特定的标头。
I have read the documentation ( http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html ) and I can't find any way to customize the ConfigServicePropertySourceLocator with a provided RestTemplate. 我已经阅读了文档( http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html ),但找不到任何使用提供的RestTemplate自定义ConfigServicePropertySourceLocator的方法。
What would be the best way to do that? 最好的方法是什么?
Many thanks 非常感谢
To expand on @spencergibb answer. 扩展@spencergibb答案。
Create a configuration class. 创建一个配置类。
@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); } }
Create a bootstrap.factories
in subdirectory resources/META-INF
在子目录resources/META-INF
创建bootstrap.factories
# Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\\ path.to.config.ConfigClientBootstrapConfiguration
There is a ConfigServicePropertySourceLocator.setRestTemplate()
. 有一个ConfigServicePropertySourceLocator.setRestTemplate()
。 In you configuration class add a @PostConstruct
method where you could set your RestTemplate
there. 在配置类中,添加一个@PostConstruct
方法,您可以在其中设置RestTemplate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.