Coming from DropWizard I am used to its HttpClientConfiguration and I am baffled that in Spring Boot I cannot find some support for controlling in a similar manner http clie
Below is an example of configuring a HttpClient
with a configuration class. It configures basic authentication for all requests through this RestTemplate
as well as some tweaks to the pool.
HttpClientConfiguration.java
@Configuration
public class HttpClientConfiguration {
private static final Logger log = LoggerFactory.getLogger(HttpClientConfiguration.class);
@Autowired
private Environment environment;
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory(httpClient());
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
restTemplate.setInterceptors(ImmutableList.of((request, body, execution) -> {
byte[] token = Base64.encodeBase64((format("%s:%s", environment.getProperty("fake.username"), environment.getProperty("fake.password"))).getBytes());
request.getHeaders().add("Authorization", format("Basic %s", new String(token)));
return execution.execute(request, body);
}));
return restTemplate;
}
@Bean
public HttpClient httpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// Get the poolMaxTotal value from our application[-?].yml or default to 10 if not explicitly set
connectionManager.setMaxTotal(environment.getProperty("poolMaxTotal", Integer.class, 10));
return HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.build();
}
/**
* Just for demonstration
*/
@PostConstruct
public void debug() {
log.info("Pool max total: {}", environment.getProperty("poolMaxTotal", Integer.class));
}
}
and an example application.yml
fake.username: test
fake.password: test
poolMaxTotal: 10
You can externalise configuration values to your application.yml
as with poolMaxTotal
etc. above.
To support different values per environment you can use Spring profiles. Using the example above, you could just create application-prod.yml
with a "prod" specific value for poolMaxTotal
. Then start your app with --spring.profiles.active=prod
and the "prod" value will be used instead of your default value in application.yml
. You can do this for however many environments you need.
application-prod.yml
poolMaxTotal: 20
For an async HttpClient
, see here: http://vincentdevillers.blogspot.fr/2013/10/a-best-spring-asyncresttemplate.html