Search in sources :

Example 16 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-gateway by spring-cloud.

the class LoadBalancerClientFilterTests method testFilter.

private ServerWebExchange testFilter(ServerWebExchange exchange, URI uri) {
    exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(chain.filter(captor.capture())).thenReturn(Mono.empty());
    SpringClientFactory clientFactory = mock(SpringClientFactory.class);
    ILoadBalancer loadBalancer = mock(ILoadBalancer.class);
    when(clientFactory.getLoadBalancerContext("service1")).thenReturn(new RibbonLoadBalancerContext(loadBalancer));
    when(clientFactory.getLoadBalancer("service1")).thenReturn(loadBalancer);
    when(loadBalancer.chooseServer(any())).thenReturn(new Server("service1-host1", 8081));
    RibbonLoadBalancerClient client = new RibbonLoadBalancerClient(clientFactory);
    LoadBalancerClientFilter filter = new LoadBalancerClientFilter(client);
    filter.filter(exchange, chain);
    return captor.getValue();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Server(com.netflix.loadbalancer.Server) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) RibbonLoadBalancerContext(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext) RibbonLoadBalancerClient(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient)

Example 17 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method retryListenerTest.

@Test
public void retryListenerTest() 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();
    MyRetryListener myRetryListener = new MyRetryListener();
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", myBackOffPolicy, false, new RetryListener[] { myRetryListener });
    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());
    assertEquals(2, myRetryListener.getOnError());
}
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)

Example 18 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testRetryOnPost.

@Test
public void testRetryOnPost() throws Exception {
    int retriesNextServer = 1;
    int retriesSameServer = 1;
    boolean retryable = true;
    boolean retryOnAllOps = true;
    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);
    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);
    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)

Example 19 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method retryListenerTestNoRetry.

@Test(expected = TerminatedRetryException.class)
public void retryListenerTestNoRetry() 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();
    MyRetryListenerNotRetry myRetryListenerNotRetry = new MyRetryListenerNotRetry();
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", myBackOffPolicy, false, new RetryListener[] { myRetryListenerNotRetry });
    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);
}
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)

Example 20 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testDoubleEncodingWithRetry.

@Test
public void testDoubleEncodingWithRetry() throws Exception {
    int retriesNextServer = 0;
    int retriesSameServer = 0;
    boolean retryable = true;
    boolean retryOnAllOps = true;
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    final URI uri = new URI("https://" + host + ":" + port + "/a%20b");
    RibbonCommandContext context = new RibbonCommandContext(serviceName, method.toString(), uri.toString(), true, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new ByteArrayInputStream(new String("bar").getBytes()), new ArrayList<RibbonRequestCustomizer>());
    RibbonApacheHttpRequest request = new RibbonApacheHttpRequest(context);
    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();
    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, true);
    client.execute(request, null);
    verify(response, times(0)).close();
    verify(delegate, times(1)).execute(argThat(new ArgumentMatcher<HttpUriRequest>() {

        @Override
        public boolean matches(HttpUriRequest argument) {
            if (argument instanceof HttpUriRequest) {
                HttpUriRequest arg = (HttpUriRequest) argument;
                return arg.getURI().equals(uri);
            }
            return false;
        }
    }));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ArgumentMatcher(org.mockito.ArgumentMatcher) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Aggregations

ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)25 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)6 ClientException (com.netflix.client.ClientException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 Locale (java.util.Locale)2 OkHttpClient (okhttp3.OkHttpClient)2