Search in sources :

Example 6 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project ribbon by Netflix.

the class LoadBalancerContext method initWithNiwsConfig.

/**
     * Set necessary parameters from client configuration and register with Servo monitors.
     */
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
    if (clientConfig == null) {
        return;
    }
    clientName = clientConfig.getClientName();
    if (clientName == null) {
        clientName = "default";
    }
    vipAddresses = clientConfig.resolveDeploymentContextbasedVipAddresses();
    maxAutoRetries = clientConfig.getPropertyAsInteger(CommonClientConfigKey.MaxAutoRetries, DefaultClientConfigImpl.DEFAULT_MAX_AUTO_RETRIES);
    maxAutoRetriesNextServer = clientConfig.getPropertyAsInteger(CommonClientConfigKey.MaxAutoRetriesNextServer, maxAutoRetriesNextServer);
    okToRetryOnAllOperations = clientConfig.getPropertyAsBoolean(CommonClientConfigKey.OkToRetryOnAllOperations, okToRetryOnAllOperations);
    defaultRetryHandler = new DefaultLoadBalancerRetryHandler(clientConfig);
    tracer = getExecuteTracer();
    Monitors.registerObject("Client_" + clientName, this);
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler)

Example 7 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project feign-reactive by kptfh.

the class LoadBalancingReactiveHttpClientTest method loadBalancingWithRetry.

private void loadBalancingWithRetry(int failedAttemptsNo, int retryOnSame, int retryOnNext) throws IOException, InterruptedException {
    String body = "success!";
    Stream.of(server1, server2).forEach(server -> {
        mockSuccessAfterSeveralAttempts(server, "/", failedAttemptsNo, 503, aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(body));
    });
    String serverListKey = serviceName + ".ribbon.listOfServers";
    getConfigInstance().setProperty(serverListKey, "localhost:" + server1.port() + "," + "localhost:" + server2.port());
    RetryHandler retryHandler = new RequestSpecificRetryHandler(true, true, new DefaultLoadBalancerRetryHandler(retryOnSame, retryOnNext, true), null);
    TestInterface client = CloudReactiveFeign.<TestInterface>builder().webClient(WebClient.create()).setLoadBalancerCommand(LoadBalancerCommand.builder().withLoadBalancer(AbstractLoadBalancer.class.cast(getNamedLoadBalancer(serviceName))).withRetryHandler(retryHandler).build()).target(TestInterface.class, "http://" + serviceName);
    try {
        String result = client.get().block();
        assertThat(result).isEqualTo(body);
    } finally {
        getConfigInstance().clearProperty(serverListKey);
    }
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) RetryHandler(com.netflix.client.RetryHandler) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) AbstractLoadBalancer(com.netflix.loadbalancer.AbstractLoadBalancer)

Example 8 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method setupClientForServerValidation.

private RetryableRibbonLoadBalancingHttpClient setupClientForServerValidation(String serviceName, String host, int port, CloseableHttpClient delegate, ILoadBalancer lb) throws Exception {
    ServerIntrospector introspector = mock(ServerIntrospector.class);
    RetryHandler retryHandler = new DefaultLoadBalancerRetryHandler(1, 1, true);
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.set(CommonClientConfigKey.OkToRetryOnAllOperations, true);
    clientConfig.set(CommonClientConfigKey.MaxAutoRetriesNextServer, 0);
    clientConfig.set(CommonClientConfigKey.MaxAutoRetries, 1);
    clientConfig.set(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES, "");
    clientConfig.set(CommonClientConfigKey.IsSecure, false);
    clientConfig.setClientName(serviceName);
    RibbonLoadBalancerContext context = new RibbonLoadBalancerContext(lb, clientConfig, retryHandler);
    SpringClientFactory clientFactory = mock(SpringClientFactory.class);
    doReturn(context).when(clientFactory).getLoadBalancerContext(eq(serviceName));
    doReturn(clientConfig).when(clientFactory).getClientConfig(eq(serviceName));
    LoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
    RetryableRibbonLoadBalancingHttpClient client = new RetryableRibbonLoadBalancingHttpClient(delegate, clientConfig, introspector, factory);
    client.setLoadBalancer(lb);
    ReflectionTestUtils.setField(client, "delegate", delegate);
    return client;
}
Also used : DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RetryHandler(com.netflix.client.RetryHandler) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RibbonLoadBalancerContext(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext) RibbonLoadBalancedRetryFactory(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) RibbonLoadBalancedRetryFactory(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory) LoadBalancedRetryFactory(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)

Example 9 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method setupClientForRetry.

private RetryableRibbonLoadBalancingHttpClient setupClientForRetry(int retriesNextServer, int retriesSameServer, boolean retryable, boolean retryOnAllOps, String serviceName, String host, int port, CloseableHttpClient delegate, ILoadBalancer lb, String statusCodes, BackOffPolicy backOffPolicy, boolean isSecure) throws Exception {
    ServerIntrospector introspector = mock(ServerIntrospector.class);
    RetryHandler retryHandler = new DefaultLoadBalancerRetryHandler(retriesSameServer, retriesNextServer, retryable);
    doReturn(new Server(host, port)).when(lb).chooseServer(eq(serviceName));
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.set(CommonClientConfigKey.OkToRetryOnAllOperations, retryOnAllOps);
    clientConfig.set(CommonClientConfigKey.MaxAutoRetriesNextServer, retriesNextServer);
    clientConfig.set(CommonClientConfigKey.MaxAutoRetries, retriesSameServer);
    clientConfig.set(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES, statusCodes);
    clientConfig.set(CommonClientConfigKey.IsSecure, isSecure);
    clientConfig.setClientName(serviceName);
    RibbonLoadBalancerContext context = new RibbonLoadBalancerContext(lb, clientConfig, retryHandler);
    SpringClientFactory clientFactory = mock(SpringClientFactory.class);
    doReturn(context).when(clientFactory).getLoadBalancerContext(eq(serviceName));
    doReturn(clientConfig).when(clientFactory).getClientConfig(eq(serviceName));
    LoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory) {

        @Override
        public BackOffPolicy createBackOffPolicy(String service) {
            return backOffPolicy;
        }
    };
    RetryableRibbonLoadBalancingHttpClient client = new RetryableRibbonLoadBalancingHttpClient(delegate, clientConfig, introspector, factory);
    client.setLoadBalancer(lb);
    ReflectionTestUtils.setField(client, "delegate", delegate);
    return client;
}
Also used : Server(com.netflix.loadbalancer.Server) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RetryHandler(com.netflix.client.RetryHandler) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) RibbonLoadBalancerContext(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext) RibbonLoadBalancedRetryFactory(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) RibbonLoadBalancedRetryFactory(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory) LoadBalancedRetryFactory(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)

Example 10 with DefaultLoadBalancerRetryHandler

use of com.netflix.client.DefaultLoadBalancerRetryHandler in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyRetryCount.

@Test
public void testGetRetryPolicyRetryCount() 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("").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();
    LoadBalancedRetryContext context = spy(new LoadBalancedRetryContext(null, request));
    // inner loop is for same server retries
    for (int i = 0; i < nextServer + 1; i++) {
        // the same sever counter and increment the next server counter
        for (int j = 0; j < sameServer + 1; j++) {
            if (j < 3) {
                assertThat(policy.canRetrySameServer(context), is(true));
            } else {
                assertThat(policy.canRetrySameServer(context), is(false));
            }
            policy.registerThrowable(context, new IOException());
        }
        if (i < 3) {
            assertThat(policy.canRetryNextServer(context), is(true));
        } else {
            assertThat(policy.canRetryNextServer(context), is(false));
        }
    }
    assertThat(context.isExhaustedOnly(), is(true));
    assertThat(policy.retryableStatusCode(400), is(false));
    verify(context, times(4)).setServiceInstance(any(ServiceInstance.class));
}
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) ServiceInstance(org.springframework.cloud.client.ServiceInstance) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) IOException(java.io.IOException) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Aggregations

DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)13 Test (org.junit.Test)6 RetryHandler (com.netflix.client.RetryHandler)5 IClientConfig (com.netflix.client.config.IClientConfig)5 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)5 RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)5 HttpRequest (org.springframework.http.HttpRequest)5 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)4 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)4 LoadBalancedRetryFactory (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory)4 RibbonLoadBalancedRetryFactory (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryFactory)4 RibbonLoadBalancerContext (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext)4 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)4 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)4 Server (com.netflix.loadbalancer.Server)3 IOException (java.io.IOException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1 AbstractLoadBalancer (com.netflix.loadbalancer.AbstractLoadBalancer)1 IRule (com.netflix.loadbalancer.IRule)1