Search in sources :

Example 1 with RibbonProperties

use of org.springframework.cloud.netflix.ribbon.RibbonProperties in project spring-cloud-netflix by spring-cloud.

the class RetryableRibbonLoadBalancingHttpClient method execute.

@Override
public RibbonApacheHttpResponse execute(final RibbonApacheHttpRequest request, final IClientConfig configOverride) throws Exception {
    final RequestConfig.Builder builder = RequestConfig.custom();
    IClientConfig config = configOverride != null ? configOverride : this.config;
    RibbonProperties ribbon = RibbonProperties.from(config);
    builder.setConnectTimeout(ribbon.connectTimeout(this.connectTimeout));
    builder.setSocketTimeout(ribbon.readTimeout(this.readTimeout));
    builder.setRedirectsEnabled(ribbon.isFollowRedirects(this.followRedirects));
    final RequestConfig requestConfig = builder.build();
    final LoadBalancedRetryPolicy retryPolicy = loadBalancedRetryFactory.createRetryPolicy(this.getClientName(), this);
    RetryCallback<RibbonApacheHttpResponse, Exception> retryCallback = context -> {
        // on retries the policy will choose the server and set it in the context
        // extract the server and update the request being made
        RibbonApacheHttpRequest newRequest = request;
        if (context instanceof LoadBalancedRetryContext) {
            ServiceInstance service = ((LoadBalancedRetryContext) context).getServiceInstance();
            validateServiceInstance(service);
            if (service != null) {
                // Reconstruct the request URI using the host and port set in the retry context
                newRequest = newRequest.withNewUri(UriComponentsBuilder.newInstance().host(service.getHost()).scheme(service.getUri().getScheme()).userInfo(newRequest.getURI().getUserInfo()).port(service.getPort()).path(newRequest.getURI().getPath()).query(newRequest.getURI().getQuery()).fragment(newRequest.getURI().getFragment()).build().encode().toUri());
            }
        }
        newRequest = getSecureRequest(newRequest, configOverride);
        HttpUriRequest httpUriRequest = newRequest.toRequest(requestConfig);
        final HttpResponse httpResponse = RetryableRibbonLoadBalancingHttpClient.this.delegate.execute(httpUriRequest);
        if (retryPolicy.retryableStatusCode(httpResponse.getStatusLine().getStatusCode())) {
            throw new HttpClientStatusCodeException(RetryableRibbonLoadBalancingHttpClient.this.clientName, httpResponse, HttpClientUtils.createEntity(httpResponse), httpUriRequest.getURI());
        }
        return new RibbonApacheHttpResponse(httpResponse, httpUriRequest.getURI());
    };
    LoadBalancedRecoveryCallback<RibbonApacheHttpResponse, HttpResponse> recoveryCallback = new LoadBalancedRecoveryCallback<RibbonApacheHttpResponse, HttpResponse>() {

        @Override
        protected RibbonApacheHttpResponse createResponse(HttpResponse response, URI uri) {
            return new RibbonApacheHttpResponse(response, uri);
        }
    };
    return this.executeWithRetry(request, retryPolicy, retryCallback, recoveryCallback);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) InterceptorRetryPolicy(org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy) HttpRequest(org.springframework.http.HttpRequest) RequestConfig(org.apache.http.client.config.RequestConfig) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BooleanUtils(org.apache.commons.lang.BooleanUtils) BackOffPolicy(org.springframework.retry.backoff.BackOffPolicy) LoadBalancedRetryFactory(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory) RecoveryCallback(org.springframework.retry.RecoveryCallback) RetryListener(org.springframework.retry.RetryListener) URI(java.net.URI) ServiceInstanceChooser(org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) NoBackOffPolicy(org.springframework.retry.backoff.NoBackOffPolicy) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) IClientConfig(com.netflix.client.config.IClientConfig) ContextAwareRequest(org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) NeverRetryPolicy(org.springframework.retry.policy.NeverRetryPolicy) LoadBalancedRecoveryCallback(org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback) RetryHandler(com.netflix.client.RetryHandler) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties) RetryCallback(org.springframework.retry.RetryCallback) HttpResponse(org.apache.http.HttpResponse) ServiceInstance(org.springframework.cloud.client.ServiceInstance) RetryTemplate(org.springframework.retry.support.RetryTemplate) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) IClientConfig(com.netflix.client.config.IClientConfig) LoadBalancedRecoveryCallback(org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)

Example 2 with RibbonProperties

use of org.springframework.cloud.netflix.ribbon.RibbonProperties in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClient method execute.

@Override
public RibbonApacheHttpResponse execute(RibbonApacheHttpRequest request, final IClientConfig configOverride) throws Exception {
    IClientConfig config = configOverride != null ? configOverride : this.config;
    RibbonProperties ribbon = RibbonProperties.from(config);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(ribbon.connectTimeout(this.connectTimeout)).setSocketTimeout(ribbon.readTimeout(this.readTimeout)).setRedirectsEnabled(ribbon.isFollowRedirects(this.followRedirects)).build();
    request = getSecureRequest(request, configOverride);
    final HttpUriRequest httpUriRequest = request.toRequest(requestConfig);
    final HttpResponse httpResponse = this.delegate.execute(httpUriRequest);
    return new RibbonApacheHttpResponse(httpResponse, httpUriRequest.getURI());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestConfig(org.apache.http.client.config.RequestConfig) IClientConfig(com.netflix.client.config.IClientConfig) HttpResponse(org.apache.http.HttpResponse) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties)

Example 3 with RibbonProperties

use of org.springframework.cloud.netflix.ribbon.RibbonProperties in project spring-cloud-netflix by spring-cloud.

the class OkHttpLoadBalancingClient method getOkHttpClient.

OkHttpClient getOkHttpClient(IClientConfig configOverride, boolean secure) {
    IClientConfig config = configOverride != null ? configOverride : this.config;
    RibbonProperties ribbon = RibbonProperties.from(config);
    OkHttpClient.Builder builder = this.delegate.newBuilder().connectTimeout(ribbon.connectTimeout(this.connectTimeout), TimeUnit.MILLISECONDS).readTimeout(ribbon.readTimeout(this.readTimeout), TimeUnit.MILLISECONDS).followRedirects(ribbon.isFollowRedirects(this.followRedirects));
    if (secure) {
        builder.followSslRedirects(ribbon.isFollowRedirects(this.followRedirects));
    }
    return builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IClientConfig(com.netflix.client.config.IClientConfig) RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties)

Example 4 with RibbonProperties

use of org.springframework.cloud.netflix.ribbon.RibbonProperties in project spring-cloud-netflix by spring-cloud.

the class AbstractLoadBalancingClient method initWithNiwsConfig.

@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    super.initWithNiwsConfig(clientConfig);
    RibbonProperties ribbon = RibbonProperties.from(clientConfig);
    this.connectTimeout = ribbon.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
    this.readTimeout = ribbon.readTimeout(DEFAULT_READ_TIMEOUT);
    this.secure = ribbon.isSecure();
    this.followRedirects = ribbon.isFollowRedirects();
    this.okToRetryOnAllOperations = ribbon.isOkToRetryOnAllOperations();
}
Also used : RibbonProperties(org.springframework.cloud.netflix.ribbon.RibbonProperties)

Aggregations

RibbonProperties (org.springframework.cloud.netflix.ribbon.RibbonProperties)4 IClientConfig (com.netflix.client.config.IClientConfig)3 HttpResponse (org.apache.http.HttpResponse)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1 RetryHandler (com.netflix.client.RetryHandler)1 URI (java.net.URI)1 OkHttpClient (okhttp3.OkHttpClient)1 BooleanUtils (org.apache.commons.lang.BooleanUtils)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 InterceptorRetryPolicy (org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy)1 LoadBalancedRecoveryCallback (org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback)1 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)1 LoadBalancedRetryFactory (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)1 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)1 ServiceInstanceChooser (org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser)1 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)1 ContextAwareRequest (org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest)1