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

标签 java spring spring-cloud spring-cloud-config

我正在尝试使用 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/

相关文章:

java - Spring和MyBatis中语句的参数group_id没有找到TypeHandler

java - 保证TCP客户端和服务器之间不丢包

Spring Data JPA 插入而不是更新

java - 从字符串启动 Controller 打印列表对象以查看(HTML)

spring-boot - 无法构建 `reactor.core.publisher.Mono` Spring Cloud OpenFeign 和 Spring boot 2 的实例

java - 服务器如何停止已经正在处理的请求?

java - java中的notifyAll()会释放其他类的线程吗?

java - 每个 Liferay JSP 都有一个术语表吗?

java - Spring 的 JdbcTemplate 和事务

spring - 请求 'OPTIONS/logout'与'POST/logout不匹配