Spring Cloud:Feign和Http连接池



有人能告诉我Spring Cloud Feign客户端是否提供或支持Http连接池,如果是,如何配置池大小等设置吗?我似乎在官方文件中找不到这个。非常感谢。

通过调查,我将尝试回答我自己的问题:

Spring Cloud Feign使用Netflix Feign。Netflix Feign反过来使用java.net.HttpURLConnection创建连接,该连接使用"持久连接",但不使用连接池。

可以覆盖客户端,例如使用Apache HttpClient,而Netflix为此提供了一个库(foreign HttpClient)。使用这种方法时,可以使用SystemProperties设置连接池大小。

在Spring Cloud Brixton中,如果Apache HttpClient或OkHttpClient可用(通过@ConditionalOnClass),那么它们将被自动使用。

这是一个例子。

@Bean
public ServiceXFeignClient serviceXClient(Encoder encoder, Decoder decoder,
  Contract contract, ClientProperties properties, ProxyProperties proxyProperties) {
  OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder()
    .connectionPool(
      new ConnectionPool(properties.getPoolConnectionMaxIdle(),
      properties.getPoolConnectionKeepMinutesAlive(), TimeUnit.MINUTES))
    .build();
  return Feign.builder()
        .client(new feign.okhttp.OkHttpClient(okHttpClient))
    .encoder(encoder)
    .decoder(decoder)
    .contract(contract)
    .target(ServiceXFeignClient.class, properties.getUrl());
}

最新更新