use of com.netflix.loadbalancer.ILoadBalancer in project camel by apache.
the class RibbonLoadBalancer method process.
// ************************
// Processor
// ************************
@Override
public <T> T process(String serviceName, LoadBalancerFunction<T> request) throws Exception {
ILoadBalancer loadBalancer = loadBalancers.computeIfAbsent(serviceName, key -> createLoadBalancer(key, serviceDiscovery));
RibbonServiceDefinition service = (RibbonServiceDefinition) loadBalancer.chooseServer(serviceName);
if (service == null) {
throw new RejectedExecutionException("No active services with name " + serviceName);
}
return request.apply(service);
}
use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.
the class EurekaRibbonClientConfigurationTests method basicConfigurationCreatedForLoadBalancer.
@Test
@Ignore
public void basicConfigurationCreatedForLoadBalancer() {
EurekaClientConfigBean client = new EurekaClientConfigBean();
EurekaInstanceConfigBean configBean = getEurekaInstanceConfigBean();
client.getAvailabilityZones().put(client.getRegion(), "foo");
SpringClientFactory clientFactory = new SpringClientFactory();
EurekaRibbonClientConfiguration clientPreprocessor = new EurekaRibbonClientConfiguration(client, "service", configBean, false);
clientPreprocessor.preprocess();
ILoadBalancer balancer = clientFactory.getLoadBalancer("service");
assertNotNull(balancer);
@SuppressWarnings("unchecked") ZoneAwareLoadBalancer<DiscoveryEnabledServer> aware = (ZoneAwareLoadBalancer<DiscoveryEnabledServer>) balancer;
assertTrue(aware.getServerListImpl() instanceof DomainExtractingServerList);
assertEquals("foo", ConfigurationManager.getDeploymentContext().getValue(ContextKey.zone));
}
use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancingHttpClientTests method invalidServerTest.
@Test
public void invalidServerTest() throws Exception {
String serviceName = "noservers";
String host = serviceName;
int port = 80;
HttpMethod method = HttpMethod.POST;
URI uri = new URI("http://" + host + ":" + port);
CloseableHttpClient delegate = mock(CloseableHttpClient.class);
ILoadBalancer lb = mock(ILoadBalancer.class);
RetryableRibbonLoadBalancingHttpClient client = setupClientForServerValidation(serviceName, host, port, delegate, lb);
RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
doReturn(new Server(null, 8000)).when(lb).chooseServer(eq(serviceName));
doReturn(method).when(request).getMethod();
doReturn(uri).when(request).getURI();
doReturn(request).when(request).withNewUri(any(URI.class));
HttpUriRequest uriRequest = mock(HttpUriRequest.class);
doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
try {
client.execute(request, null);
fail("Expected IOException for no servers available");
} catch (ClientException ex) {
assertThat(ex.getMessage(), containsString("Invalid Server for: "));
}
}
use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancingHttpClientTests method testNoRetryOnPost.
@Test
public void testNoRetryOnPost() throws Exception {
int retriesNextServer = 1;
int retriesSameServer = 1;
boolean retryable = true;
boolean retryOnAllOps = false;
String serviceName = "foo";
String host = serviceName;
int port = 80;
HttpMethod method = HttpMethod.POST;
URI uri = new URI("http://" + host + ":" + port);
CloseableHttpClient delegate = mock(CloseableHttpClient.class);
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
doThrow(new IOException("boom")).doThrow(new IOException("boom again")).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
ILoadBalancer lb = mock(ILoadBalancer.class);
RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", null);
RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
doReturn(method).when(request).getMethod();
doReturn(uri).when(request).getURI();
doReturn(request).when(request).withNewUri(any(URI.class));
HttpUriRequest uriRequest = mock(HttpUriRequest.class);
doReturn(uri).when(uriRequest).getURI();
doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
try {
client.execute(request, null);
fail("Expected IOException");
} catch (IOException e) {
} finally {
verify(response, times(0)).close();
verify(delegate, times(1)).execute(any(HttpUriRequest.class));
verify(lb, times(1)).chooseServer(eq(serviceName));
}
}
use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancingHttpClientTests method retryWithOriginalConstructorTest.
@Test
public void retryWithOriginalConstructorTest() throws Exception {
int retriesNextServer = 1;
int retriesSameServer = 1;
boolean retryable = true;
boolean retryOnAllOps = true;
String serviceName = "listener";
String host = serviceName;
int port = 80;
HttpMethod method = HttpMethod.POST;
URI uri = new URI("http://" + host + ":" + port);
CloseableHttpClient delegate = mock(CloseableHttpClient.class);
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
doReturn(200).when(statusLine).getStatusCode();
doReturn(statusLine).when(response).getStatusLine();
doThrow(new IOException("boom")).doThrow(new IOException("boom again")).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
ILoadBalancer lb = mock(ILoadBalancer.class);
MyBackOffPolicy myBackOffPolicy = new MyBackOffPolicy();
RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", myBackOffPolicy, false);
RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
doReturn(method).when(request).getMethod();
doReturn(uri).when(request).getURI();
doReturn(request).when(request).withNewUri(any(URI.class));
HttpUriRequest uriRequest = mock(HttpUriRequest.class);
doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
RibbonApacheHttpResponse returnedResponse = client.execute(request, null);
verify(response, times(0)).close();
verify(delegate, times(3)).execute(any(HttpUriRequest.class));
verify(lb, times(2)).chooseServer(eq(serviceName));
assertEquals(2, myBackOffPolicy.getCount());
}
Aggregations