请问Spring Cloud Feign客户端是否提供或支持HTTP连接池,如果是的话,如何配置设置(例如池大小)?我似乎在官方文档中找不到这个信息。谢谢。
请问Spring Cloud Feign客户端是否提供或支持HTTP连接池,如果是的话,如何配置设置(例如池大小)?我似乎在官方文档中找不到这个信息。谢谢。
经过调查,我将尝试回答自己的问题:
Spring Cloud Feign使用Netflix Feign。Netflix Feign又使用java.net.HttpURLConnection创建连接,它使用“持久连接”而不是连接池。
可以通过覆盖客户端(例如使用Apache HttpClient)来实现连接池。Netflix为此提供了一个库(feign-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());
}