Search in sources :

Example 1 with ServerIntrospector

use of org.springframework.cloud.netflix.ribbon.ServerIntrospector 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, RetryListener[] retryListeners) 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 RetryListener[] createRetryListeners(String service) {
            return retryListeners;
        }

        @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) RetryListener(org.springframework.retry.RetryListener)

Example 2 with ServerIntrospector

use of org.springframework.cloud.netflix.ribbon.ServerIntrospector in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testDoubleEncoding.

@Test
public void testDoubleEncoding() throws Exception {
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    final URI uri = new URI("https://" + host + ":" + port + "/a%2Bb");
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.setClientName(serviceName);
    ServerIntrospector introspector = mock(ServerIntrospector.class);
    RibbonCommandContext context = new RibbonCommandContext(serviceName, method.toString(), uri.toString(), false, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new ByteArrayInputStream("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));
    RibbonLoadBalancingHttpClient client = new RibbonLoadBalancingHttpClient(delegate, clientConfig, introspector);
    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) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) ArgumentMatcher(org.mockito.ArgumentMatcher) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 3 with ServerIntrospector

use of org.springframework.cloud.netflix.ribbon.ServerIntrospector in project spring-cloud-netflix by spring-cloud.

the class SpringRetryEnabledOkHttpClientTests method setupClientForServerValidation.

private RetryableOkHttpLoadBalancingClient setupClientForServerValidation(String serviceName, String host, int port, OkHttpClient 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);
    RetryableOkHttpLoadBalancingClient client = new RetryableOkHttpLoadBalancingClient(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 4 with ServerIntrospector

use of org.springframework.cloud.netflix.ribbon.ServerIntrospector in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testNeverRetry.

@Test
public void testNeverRetry() throws Exception {
    ServerIntrospector introspector = mock(ServerIntrospector.class);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    doThrow(new IOException("boom")).when(delegate).execute(any(HttpUriRequest.class));
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.setClientName("foo");
    RibbonLoadBalancingHttpClient client = new RibbonLoadBalancingHttpClient(delegate, clientConfig, introspector);
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    when(request.toRequest(any(RequestConfig.class))).thenReturn(mock(HttpUriRequest.class));
    try {
        client.execute(request, null);
        fail("Expected IOException");
    } catch (IOException e) {
    } finally {
        verify(delegate, times(1)).execute(any(HttpUriRequest.class));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) IOException(java.io.IOException) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 5 with ServerIntrospector

use of org.springframework.cloud.netflix.ribbon.ServerIntrospector 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)

Aggregations

DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)6 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)6 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)4 RetryHandler (com.netflix.client.RetryHandler)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 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 Server (com.netflix.loadbalancer.Server)2 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 StatusLine (org.apache.http.StatusLine)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1