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);
}
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);
}
}
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;
}
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;
}
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));
}
Aggregations