spring-cloud-netflix icon indicating copy to clipboard operation
spring-cloud-netflix copied to clipboard

RibbonLoadBalancingHttpClient pool size not configurable

Open nissel opened this issue 10 years ago • 6 comments

RibbonLoadBalancingHttpClient uses the default HttpClientBuilder. private final HttpClient delegate = HttpClientBuilder.create().build();

This defaults to a small maxConnTotal (20) and maxConnPerRoute (2)

There are IClientConfig settings for MaxTotalConnections and MaxConnectionsPerHost it would be great if these could be honoured.

nissel avatar Jun 30 '16 09:06 nissel

Actually having a closer look with the debugger in 1.1.2 my zuul proxy with:

@Bean
public RibbonCommandFactory<?> ribbonCommandFactory(SpringClientFactory clientFactory) {
   return new HttpClientRibbonCommandFactory(clientFactory);
}

It actually makes a new RibbonLoadBalancingHttpClient per request, and in turn a new delegate HttpClient. So the connection limits are not a problem but there will be a lot of unnecessary churn of HttpClient instances. I'm not familiar enough with this code to attempt a pull request or even suggest a fix.

nissel avatar Jun 30 '16 15:06 nissel

It looks like the RibbonLoadBalancingHttpClient's aren't being cached in the context inside of the SpringClientFactory.

The Ribbon RestClient's do get cached in SpringClientFactory but I'm not sure why.

nissel avatar Jun 30 '16 15:06 nissel

Good investigation. Both things look like they should be fixed. ~~RestClient's are cached because it is used to get the RestClient, therefore it gets cached.~~ They are in the child contexts, not cached.

spencergibb avatar Jun 30 '16 16:06 spencergibb

See #1150 for caching.

spencergibb avatar Jun 30 '16 16:06 spencergibb

Not sure what I was saying about caching there :-(

spencergibb avatar Sep 22 '16 14:09 spencergibb

@nissel @stephlag

The following is a workaround for the Apache Client being created each time:

// other annotations like @EnableZuulProxy
@SpringBootApplication
@RibbonClients(defaultConfiguration = {RibbonClientConfiguration.class, DefaultConfig.class})
public class DemoapachehttpclientcreateApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoapachehttpclientcreateApplication.class, args);
    }
}
class DefaultConfig {
    @Bean
    public RibbonLoadBalancingHttpClient ribbonLoadBalancingHttpClient(IClientConfig config, RetryHandler retryHandler) {
        RibbonLoadBalancingHttpClient client = new RibbonLoadBalancingHttpClient();
        client.initWithNiwsConfig(config);
        client.setRetryHandler(retryHandler);
        return client;
    }
}

For OkHttp replace RibbonLoadBalancingHttpClient with OkHttpLoadBalancingClient.

spencergibb avatar Sep 22 '16 21:09 spencergibb