我正在尝试使用 spring-cloud-config-client 在启动时从 spring-cloud-config-server 应用程序读取我的配置属性。 我的应用程序是一个 Spring-Boot 应用程序,我需要做的是在将请求发送到配置服务器之前向请求添加一个特定的 header 。
我已阅读文档 ( http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html ),但我找不到任何方法来使用提供的 RestTemplate 自定义 ConfigServicePropertySourceLocator。
最好的方法是什么?
非常感谢
最佳答案
扩展@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); } }
在子目录
resources/META-INF
中创建一个bootstrap.factories
# Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ path.to.config.ConfigClientBootstrapConfiguration
关于java - 使用 spring-cloud-config-client 时如何配置自定义 RestTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30239163/