本文介绍了Spring可配置的高性能计量HTTP客户端实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自DropWizard,我已经习惯了它的 HttpClientConfiguration ,我对此感到困惑在Spring Boot中,我找不到以类似方式控制要使用的http客户端实例(例如RestTemplates)的支持.

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.

要在生产环境中工作,底层客户端实现应具有高性能(例如,非阻塞io,具有连接重用和池化功能).

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

然后,我需要设置超时或身份验证,可能是指标收集,cookie设置,SSL证书设置.

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

对于所有要在不同上下文中使用的实例,上述所有内容应易于设置为不同的变体(例如,对于服务X使用这些设置和该池,对于Y使用其他池和设置),并且大多数参数应为通过特定于环境的属性进行设置,以在生产/质量保证/开发中具有不同的值.

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?

推荐答案

下面是使用配置类配置HttpClient的示例.它为通过此RestTemplate的所有请求以及对池的一些调整配置基本身份验证.

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));
  }
}

和示例application.yml

fake.username: test
fake.password: test
poolMaxTotal: 10

您可以像上面的poolMaxTotal等那样将配置值外部化到您的application.yml.

You can externalise configuration values to your application.yml as with poolMaxTotal etc. above.

要在每个环境中支持不同的值,可以使用Spring概要文件.使用上面的示例,您可以使用poolMaxTotal的特定"prod"值创建application-prod.yml.然后使用--spring.profiles.active=prod启动您的应用,并且将使用"prod"值代替application.yml中的默认值.您可以针对所需的许多环境执行此操作.

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

有关异步HttpClient的信息,请参见此处: http://vincentdevillers.blogspot .fr/2013/10/a-best-spring-asyncresttemplate.html

For an async HttpClient, see here: http://vincentdevillers.blogspot.fr/2013/10/a-best-spring-asyncresttemplate.html

这篇关于Spring可配置的高性能计量HTTP客户端实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:29