Allow OkHttpClient to be customized.

Moved common functionality to AbstractLoadBalancingClient
parent 9eed2490
......@@ -23,13 +23,11 @@ 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.client.HttpClientBuilder;
import org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.AbstractLoadBalancingClient;
import org.springframework.web.util.UriComponentsBuilder;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
......@@ -42,40 +40,27 @@ import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttps
//TODO: rename (ie new class that extends this in Dalston) to ApacheHttpLoadBalancingClient
public class RibbonLoadBalancingHttpClient
extends
AbstractLoadBalancingClient<RibbonApacheHttpRequest, RibbonApacheHttpResponse> {
private final HttpClient delegate;
private final IClientConfig config;
private final ServerIntrospector serverIntrospector;
AbstractLoadBalancingClient<RibbonApacheHttpRequest, RibbonApacheHttpResponse, HttpClient> {
@Deprecated
public RibbonLoadBalancingHttpClient() {
this(new DefaultClientConfigImpl(), new DefaultServerIntrospector());
super();
}
@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);
super(config, serverIntrospector);
}
public RibbonLoadBalancingHttpClient(HttpClient delegate, IClientConfig config, ServerIntrospector serverIntrospector) {
this.delegate = delegate;
this.config = config;
this.serverIntrospector = serverIntrospector;
initWithNiwsConfig(config);
super(delegate, config, serverIntrospector);
}
protected HttpClient createHttpClient(IClientConfig config) {
protected HttpClient createDelegate(IClientConfig config) {
return HttpClientBuilder.create()
// already defaults to 0 in builder, so resetting to 0 won't hurt
.setMaxConnTotal(config.getPropertyAsInteger(CommonClientConfigKey.MaxTotalConnections, 0))
......@@ -86,10 +71,6 @@ public class RibbonLoadBalancingHttpClient
.build();
}
protected HttpClient getDelegate() {
return this.delegate;
}
@Override
public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request,
final IClientConfig configOverride) throws Exception {
......
......@@ -19,13 +19,11 @@ package org.springframework.cloud.netflix.ribbon.okhttp;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import org.springframework.cloud.netflix.ribbon.support.AbstractLoadBalancingClient;
import org.springframework.web.util.UriComponentsBuilder;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
......@@ -40,30 +38,31 @@ import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToHttps
* @author Spencer Gibb
*/
public class OkHttpLoadBalancingClient
extends AbstractLoadBalancingClient<OkHttpRibbonRequest, OkHttpRibbonResponse> {
private final OkHttpClient delegate = new OkHttpClient();
private final IClientConfig config;
private final ServerIntrospector serverIntrospector;
extends AbstractLoadBalancingClient<OkHttpRibbonRequest, OkHttpRibbonResponse, OkHttpClient> {
@Deprecated
public OkHttpLoadBalancingClient() {
super();
config = new DefaultClientConfigImpl();
serverIntrospector = new DefaultServerIntrospector();
}
@Deprecated
public OkHttpLoadBalancingClient(final ILoadBalancer lb) {
super(lb);
config = new DefaultClientConfigImpl();
serverIntrospector = new DefaultServerIntrospector();
}
public OkHttpLoadBalancingClient(IClientConfig config,
ServerIntrospector serverIntrospector) {
this.config = config;
this.serverIntrospector = serverIntrospector;
initWithNiwsConfig(config);
super(config, serverIntrospector);
}
public OkHttpLoadBalancingClient(OkHttpClient delegate, IClientConfig config,
ServerIntrospector serverIntrospector) {
super(delegate, config, serverIntrospector);
}
@Override
protected OkHttpClient createDelegate(IClientConfig config) {
return new OkHttpClient();
}
@Override
......
......@@ -17,6 +17,9 @@
package org.springframework.cloud.netflix.ribbon.support;
import org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector;
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.IResponse;
import com.netflix.client.RequestSpecificRetryHandler;
......@@ -29,7 +32,7 @@ import com.netflix.loadbalancer.ILoadBalancer;
/**
* @author Spencer Gibb
*/
public abstract class AbstractLoadBalancingClient<S extends ContextAwareRequest, T extends IResponse> extends
public abstract class AbstractLoadBalancingClient<S extends ContextAwareRequest, T extends IResponse, D> extends
AbstractLoadBalancerAwareClient<S, T> {
protected int connectTimeout;
......@@ -42,16 +45,47 @@ public abstract class AbstractLoadBalancingClient<S extends ContextAwareRequest,
protected boolean okToRetryOnAllOperations;
protected final D delegate;
protected final IClientConfig config;
protected final ServerIntrospector serverIntrospector;
@Deprecated
public AbstractLoadBalancingClient() {
super(null);
this.config = new DefaultClientConfigImpl();
this.delegate = createDelegate(this.config);
this.serverIntrospector = new DefaultServerIntrospector();
this.setRetryHandler(RetryHandler.DEFAULT);
initWithNiwsConfig(config);
}
@Deprecated
public AbstractLoadBalancingClient(final ILoadBalancer lb) {
super(lb);
this.config = new DefaultClientConfigImpl();
this.delegate = createDelegate(config);
this.serverIntrospector = new DefaultServerIntrospector();
this.setRetryHandler(RetryHandler.DEFAULT);
initWithNiwsConfig(config);
}
protected AbstractLoadBalancingClient(IClientConfig config, ServerIntrospector serverIntrospector) {
super(null);
this.delegate = createDelegate(config);
this.config = config;
this.serverIntrospector = serverIntrospector;
this.setRetryHandler(RetryHandler.DEFAULT);
initWithNiwsConfig(config);
}
protected AbstractLoadBalancingClient(D delegate, IClientConfig config, ServerIntrospector serverIntrospector) {
super(null);
this.delegate = delegate;
this.config = config;
this.serverIntrospector = serverIntrospector;
this.setRetryHandler(RetryHandler.DEFAULT);
initWithNiwsConfig(config);
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
......@@ -72,6 +106,12 @@ public abstract class AbstractLoadBalancingClient<S extends ContextAwareRequest,
DefaultClientConfigImpl.DEFAULT_OK_TO_RETRY_ON_ALL_OPERATIONS);
}
protected abstract D createDelegate(IClientConfig config);
public D getDelegate() {
return this.delegate;
}
@Override
public RequestSpecificRetryHandler getRequestSpecificRetryHandler(
final S request, final IClientConfig requestConfig) {
......
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