Allow customization of HttpClient.

Uses ribbon settings to set MaxConnTotal and MaxConnPerRoute. Also allows user to create a custom HttpClient as a constructor argument. fixes gh-1149
parent bbff2b24
......@@ -43,30 +43,53 @@ import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttps
public class RibbonLoadBalancingHttpClient
extends
AbstractLoadBalancingClient<RibbonApacheHttpRequest, RibbonApacheHttpResponse> {
private final HttpClient delegate = HttpClientBuilder.create().disableCookieManagement().build();
private final HttpClient delegate;
private final IClientConfig config;
private final ServerIntrospector serverIntrospector;
@Deprecated
public RibbonLoadBalancingHttpClient() {
super();
this.config = new DefaultClientConfigImpl();
this.serverIntrospector = new DefaultServerIntrospector();
this(new DefaultClientConfigImpl(), new DefaultServerIntrospector());
}
@Deprecated
public RibbonLoadBalancingHttpClient(final ILoadBalancer lb) {
super(lb);
this.config = new DefaultClientConfigImpl();
this.delegate = createHttpClient(this.config);
this.serverIntrospector = new DefaultServerIntrospector();
initWithNiwsConfig(config);
}
public RibbonLoadBalancingHttpClient(IClientConfig config, ServerIntrospector serverIntrospector) {
this.delegate = createHttpClient(config);
this.config = config;
this.serverIntrospector = serverIntrospector;
initWithNiwsConfig(config);
}
public RibbonLoadBalancingHttpClient(HttpClient delegate, IClientConfig config, ServerIntrospector serverIntrospector) {
this.delegate = delegate;
this.config = config;
this.serverIntrospector = serverIntrospector;
initWithNiwsConfig(config);
}
protected HttpClient createHttpClient(IClientConfig config) {
return HttpClientBuilder.create()
// already defaults to 0 in builder, so resetting to 0 won't hurt
.setMaxConnTotal(config.getPropertyAsInteger(CommonClientConfigKey.MaxTotalConnections, 0))
// already defaults to 0 in builder, so resetting to 0 won't hurt
.setMaxConnPerRoute(config.getPropertyAsInteger(CommonClientConfigKey.MaxConnectionsPerHost, 0))
.disableCookieManagement()
.useSystemProperties() // for proxy
.build();
}
protected HttpClient getDelegate() {
return this.delegate;
}
@Override
public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request,
final IClientConfig configOverride) throws Exception {
......
......@@ -20,6 +20,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
......@@ -74,6 +75,20 @@ public class RibbonLoadBalancingHttpClientTests {
}
@Test
public void testConnections() throws Exception {
SpringClientFactory factory = new SpringClientFactory();
factory.setApplicationContext(new AnnotationConfigApplicationContext(
RibbonAutoConfiguration.class, Connections.class));
RibbonLoadBalancingHttpClient client = factory.getClient("service",
RibbonLoadBalancingHttpClient.class);
HttpClient delegate = client.getDelegate();
PoolingHttpClientConnectionManager connManager = (PoolingHttpClientConnectionManager) ReflectionTestUtils.getField(delegate, "connManager");
assertThat(connManager.getMaxTotal(), is(101));
assertThat(connManager.getDefaultMaxPerRoute(), is(201));
}
@Test
public void testRequestConfigDoNotFollowRedirectsOverrideWithFollowRedirects()
throws Exception {
......@@ -135,6 +150,18 @@ public class RibbonLoadBalancingHttpClientTests {
}
}
@Configuration
protected static class Connections {
@Bean
public IClientConfig clientConfig() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.set(CommonClientConfigKey.MaxTotalConnections, 101);
config.set(CommonClientConfigKey.MaxConnectionsPerHost, 201);
return config;
}
}
private RequestConfig getBuiltRequestConfig(Class<?> defaultConfigurationClass,
IClientConfig configOverride) throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment