简体   繁体   English

使用spring-cloud-config-client时如何配置自定义RestTemplate?

[英]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.

相关问题 Spring - 无法导入 spring-cloud-config-client 依赖项。 IntellliJ 错误 - Spring - Cannot import spring-cloud-config-client dependency. IntellliJ Bug 如何为 Spring 中的每个客户端配置和构建自定义 RestTemplate? - How to configure and build a custom RestTemplate for each client in Spring? 使用 SimpleClientHttpRequestFactory 时,如何使自定义 RestTemplate 使用标准 Spring Boots 的 Prometheus RestTemplate 指标进行检测? - How to make custom RestTemplate to instrument with standard Spring Boots' Prometheus RestTemplate metrics when using SimpleClientHttpRequestFactory? 如何为客户端和服务器配置Spring Boot RestTemplate代理 - How to configure spring boot resttemplate proxy for client and server 当 Spring Security 在 Spring Cloud Config Server 上处于活动状态时,Spring Cloud Config Client 不获取配置 - Spring Cloud Config Client not fetching config when Spring Security is active on Spring Cloud Config Server Spring Cloud-如何在Zuul反向代理上配置OAuth2RestTemplate来传递授权令牌? - Spring Cloud - How to configure OAuth2RestTemplate on Zuul reverse proxy to pass authorization tokens through? Spring Cloud Config客户端配置刷新不起作用 - spring cloud config client config refresh not working 如何配置spring security 3.2以使用java配置使用dao身份验证和自定义身份验证过滤器 - How to configure spring security 3.2 to use dao authentication and custom authentication filter using java config 使用spring RestTemplate在rest客户端上的最佳实践 - Best practices on rest client using spring RestTemplate Spring 云配置客户端应用程序未使用 bootstrap.properties - Spring Cloud Config Client app not using bootstrap.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM