Spring configurable, high performance, metered http client instances

谁说胖子不能爱 提交于 2019-12-04 10:10:31

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!