belouto
belouto

Reputation: 33

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. 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.

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.

What would be the best way to do that?

Many thanks

Upvotes: 3

Views: 3429

Answers (2)

Olayinka
Olayinka

Reputation: 2845

To expand on @spencergibb answer.

  • 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

    # Bootstrap components
    org.springframework.cloud.bootstrap.BootstrapConfiguration=\
    path.to.config.ConfigClientBootstrapConfiguration
    

Upvotes: 4

spencergibb
spencergibb

Reputation: 25177

There is a ConfigServicePropertySourceLocator.setRestTemplate(). In you configuration class add a @PostConstruct method where you could set your RestTemplate there.

Upvotes: 2

Related Questions