Search in sources :

Example 1 with SpringClientFactory

use of org.springframework.cloud.netflix.ribbon.SpringClientFactory in project tutorials by eugenp.

the class GatewayApplication method springClientFactory.

@Bean
public SpringClientFactory springClientFactory() {
    SpringClientFactory factory = new SpringClientFactory();
    factory.setConfigurations(this.configurations);
    return factory;
}
Also used : SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) Bean(org.springframework.context.annotation.Bean)

Example 2 with SpringClientFactory

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

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

the class RibbonLoadBalancingHttpClientTests method testConnections.

@Test
public void testConnections() throws Exception {
    SpringClientFactory factory = new SpringClientFactory();
    factory.setApplicationContext(new AnnotationConfigApplicationContext(RibbonAutoConfiguration.class, Connections.class));
    RetryableRibbonLoadBalancingHttpClient client = factory.getClient("service", RetryableRibbonLoadBalancingHttpClient.class);
    HttpClient delegate = client.getDelegate();
    PoolingHttpClientConnectionManager connManager = (PoolingHttpClientConnectionManager) ReflectionTestUtils.getField(delegate, "connManager");
    assertThat(connManager.getMaxTotal(), is(101));
    assertThat(connManager.getDefaultMaxPerRoute(), is(201));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RibbonAutoConfiguration(org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.Test)

Example 5 with SpringClientFactory

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

the class OkHttpLoadBalancingClientTests method testUpdatedTimeouts.

@Test
public void testUpdatedTimeouts() throws Exception {
    SpringClientFactory factory = new SpringClientFactory();
    OkHttpClient result = getHttpClient(Timeouts.class, null, factory);
    assertThat(result.readTimeoutMillis(), is(50000));
    assertThat(result.connectTimeoutMillis(), is(60000));
    IClientConfig config = factory.getClientConfig("service");
    config.set(CommonClientConfigKey.ConnectTimeout, 60);
    config.set(CommonClientConfigKey.ReadTimeout, 50);
    result = getHttpClient(Timeouts.class, null, factory);
    assertThat(result.readTimeoutMillis(), is(50));
    assertThat(result.connectTimeoutMillis(), is(60));
}
Also used : OkHttpClient(okhttp3.OkHttpClient) SpringClientFactory(org.springframework.cloud.netflix.ribbon.SpringClientFactory) IClientConfig(com.netflix.client.config.IClientConfig) Test(org.junit.Test)

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