Spring configurable, high performance, metered http client instances

Andrea Ratto picture Andrea Ratto · Apr 30, 2015 · Viewed 10.3k times · Source

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 clients instances to be used, by RestTemplates for example.

To work in production the underlying client implementation should be high performance (e.g. non blocking io, with connection reuse and pooling).

Then I need to set timeouts or authentication, possibly metrics gathering, cookie settings, SSL certificates settings.

All of the above should be easy to set up in different variants for different instances to be used in different contexts (e.g. for service X use these settings and this pool, for Y use another pool and settings) and most parameters should be set via environment-specific properties to have different values in production/qa/development.

Is there something that can be used towards this end?

Answer

Donovan Muller picture Donovan Muller · Apr 30, 2015

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