Search in sources :

Example 1 with ILoadBalancer

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);
}
Also used : ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with ILoadBalancer

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));
}
Also used : EurekaInstanceConfigBean(org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) DiscoveryEnabledServer(com.netflix.niws.loadbalancer.DiscoveryEnabledServer) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) EurekaClientConfigBean(org.springframework.cloud.netflix.eureka.EurekaClientConfigBean) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ILoadBalancer

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: "));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) Server(com.netflix.loadbalancer.Server) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientException(com.netflix.client.ClientException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 4 with ILoadBalancer

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));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URI(java.net.URI) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 5 with ILoadBalancer

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());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)24 Test (org.junit.Test)18 URI (java.net.URI)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 HttpMethod (org.springframework.http.HttpMethod)15 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)13 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)13 RequestConfig (org.apache.http.client.config.RequestConfig)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 StatusLine (org.apache.http.StatusLine)10 IOException (java.io.IOException)8 Server (com.netflix.loadbalancer.Server)5 ClientException (com.netflix.client.ClientException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 Locale (java.util.Locale)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2