Spring Cloud: Feign and Http Connection Pooling
Asked Answered
V

2

9

Can anyone please tell me if the Spring Cloud Feign Client provides or supports Http Connection Pooling, and if so how to configure settings like pool size? I can't seem to find this in the official documentation. Thank you.

Viviennevivify answered 22/3, 2016 at 11:25 Comment(0)
V
10

From investigation I will try to answer my own question:

Spring Cloud Feign uses Netflix Feign. Netflix Feign in turn creates connections using java.net.HttpURLConnection which makes use of 'persistent connections' but not a connection pool.

It is possible to override the Client, for example using Apache HttpClient instead, and Netflix provide a library for this (feign-httpclient). When using this approach the connection pool size can be set using SystemProperties.

In Spring Cloud Brixton it seems that if Apache HttpClient or OkHttpClient are available (via @ConditionalOnClass) then they are automatically used.

Viviennevivify answered 31/3, 2016 at 14:47 Comment(2)
Are you sure about Spring Cloud will use Apache HttpClient automatically when it's in classpath?Prankster
You need to add io.github.openfeign:feign-httpclient, not a just apache http clientNeom
S
0

This is an example.

@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());
}
Spreader answered 23/10, 2020 at 4:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.