Search in sources :

Example 1 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyRetryOnNonGet.

@Test
public void testGetRetryPolicyRetryOnNonGet() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    boolean retryOnAllOps = true;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    doReturn(server.getServiceId()).when(config).getClientName();
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    clientFactory.getLoadBalancerContext(server.getServiceId()).initWithNiwsConfig(config);
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.POST).when(request).getMethod();
    LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
    assertThat(policy.canRetryNextServer(context), is(true));
    assertThat(policy.canRetrySameServer(context), is(true));
    assertThat(policy.retryableStatusCode(400), is(false));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 2 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyNotGet.

@Test
public void testGetRetryPolicyNotGet() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    boolean retryOnAllOps = false;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
    doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    doReturn(server.getServiceId()).when(config).getClientName();
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.POST).when(request).getMethod();
    LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
    assertThat(policy.canRetryNextServer(context), is(false));
    assertThat(policy.canRetrySameServer(context), is(false));
    assertThat(policy.retryableStatusCode(400), is(false));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 3 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testRetryableStatusCodes.

@Test
public void testRetryableStatusCodes() throws Exception {
    int sameServer = 3;
    int nextServer = 3;
    RibbonServer server = getRibbonServer();
    IClientConfig config = mock(IClientConfig.class);
    doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
    doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
    doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
    doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
    doReturn("404, 418,502,foo, ,").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
    clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
    RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
    RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
    HttpRequest request = mock(HttpRequest.class);
    doReturn(HttpMethod.GET).when(request).getMethod();
    assertThat(policy.retryableStatusCode(400), is(false));
    assertThat(policy.retryableStatusCode(404), is(true));
    assertThat(policy.retryableStatusCode(418), is(true));
    assertThat(policy.retryableStatusCode(502), is(true));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) Test(org.junit.Test)

Example 4 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy 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 5 with LoadBalancedRetryPolicy

use of org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy in project spring-cloud-netflix by spring-cloud.

the class RetryableOkHttpLoadBalancingClient method execute.

@Override
public OkHttpRibbonResponse execute(final OkHttpRibbonRequest ribbonRequest, final IClientConfig configOverride) throws Exception {
    final LoadBalancedRetryPolicy retryPolicy = loadBalancedRetryFactory.createRetryPolicy(this.getClientName(), this);
    RetryCallback<OkHttpRibbonResponse, Exception> retryCallback = new RetryCallback<OkHttpRibbonResponse, Exception>() {

        @Override
        public OkHttpRibbonResponse doWithRetry(RetryContext context) throws Exception {
            // on retries the policy will choose the server and set it in the context
            // extract the server and update the request being made
            OkHttpRibbonRequest newRequest = ribbonRequest;
            if (context instanceof LoadBalancedRetryContext) {
                ServiceInstance service = ((LoadBalancedRetryContext) context).getServiceInstance();
                validateServiceInstance(service);
                // Reconstruct the request URI using the host and port set in the retry context
                newRequest = newRequest.withNewUri(new URI(service.getUri().getScheme(), newRequest.getURI().getUserInfo(), service.getHost(), service.getPort(), newRequest.getURI().getPath(), newRequest.getURI().getQuery(), newRequest.getURI().getFragment()));
            }
            if (isSecure(configOverride)) {
                final URI secureUri = UriComponentsBuilder.fromUri(newRequest.getUri()).scheme("https").build().toUri();
                newRequest = newRequest.withNewUri(secureUri);
            }
            OkHttpClient httpClient = getOkHttpClient(configOverride, secure);
            final Request request = newRequest.toRequest();
            Response response = httpClient.newCall(request).execute();
            if (retryPolicy.retryableStatusCode(response.code())) {
                ResponseBody responseBody = response.peekBody(Integer.MAX_VALUE);
                response.close();
                throw new OkHttpStatusCodeException(RetryableOkHttpLoadBalancingClient.this.clientName, response, responseBody, newRequest.getURI());
            }
            return new OkHttpRibbonResponse(response, newRequest.getUri());
        }
    };
    return this.executeWithRetry(ribbonRequest, retryPolicy, retryCallback, new LoadBalancedRecoveryCallback<OkHttpRibbonResponse, Response>() {

        @Override
        protected OkHttpRibbonResponse createResponse(Response response, URI uri) {
            return new OkHttpRibbonResponse(response, uri);
        }
    });
}
Also used : LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) OkHttpClient(okhttp3.OkHttpClient) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) RetryContext(org.springframework.retry.RetryContext) HttpRequest(org.springframework.http.HttpRequest) Request(okhttp3.Request) ContextAwareRequest(org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest) ServiceInstance(org.springframework.cloud.client.ServiceInstance) URI(java.net.URI) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) RetryCallback(org.springframework.retry.RetryCallback)

Aggregations

LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)10 HttpRequest (org.springframework.http.HttpRequest)8 IClientConfig (com.netflix.client.config.IClientConfig)7 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)7 Test (org.junit.Test)6 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)6 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)5 ServiceInstance (org.springframework.cloud.client.ServiceInstance)3 InterceptorRetryPolicy (org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy)3 RetryListener (org.springframework.retry.RetryListener)3 BackOffPolicy (org.springframework.retry.backoff.BackOffPolicy)3 NoBackOffPolicy (org.springframework.retry.backoff.NoBackOffPolicy)3 NeverRetryPolicy (org.springframework.retry.policy.NeverRetryPolicy)3 RetryTemplate (org.springframework.retry.support.RetryTemplate)3 IOException (java.io.IOException)2 URI (java.net.URI)2 ContextAwareRequest (org.springframework.cloud.netflix.ribbon.support.ContextAwareRequest)2 RetryCallback (org.springframework.retry.RetryCallback)2 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1 RetryHandler (com.netflix.client.RetryHandler)1