Search in sources :

Example 31 with SpringClientFactory

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

Example 32 with SpringClientFactory

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

Aggregations

SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)32 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)26 IClientConfig (com.netflix.client.config.IClientConfig)24 Test (org.junit.Test)24 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)22 RibbonCommandContext (org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext)20 FallbackProvider (org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider)20 RibbonLoadBalancingHttpClient (org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient)11 OkHttpLoadBalancingClient (org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient)11 RibbonLoadBalancerContext (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerContext)5 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 ServerIntrospector (org.springframework.cloud.netflix.ribbon.ServerIntrospector)4 Server (com.netflix.loadbalancer.Server)3 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 HashSet (java.util.HashSet)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2