Search in sources :

Example 1 with ZoneAwareLoadBalancer

use of com.netflix.loadbalancer.ZoneAwareLoadBalancer in project camel by apache.

the class RibbonLoadBalancer method createLoadBalancer.

// ************************
// Helpers
// ************************
private ZoneAwareLoadBalancer<RibbonServiceDefinition> createLoadBalancer(String serviceName, ServiceDiscovery serviceDiscovery) {
    // setup client config
    IClientConfig config = configuration.getClientName() != null ? IClientConfig.Builder.newBuilder(configuration.getClientName()).build() : IClientConfig.Builder.newBuilder().build();
    if (configuration.getClientConfig() != null) {
        for (Map.Entry<String, String> entry : configuration.getClientConfig().entrySet()) {
            IClientConfigKey key = IClientConfigKey.Keys.valueOf(entry.getKey());
            String value = entry.getValue();
            LOGGER.debug("RibbonClientConfig: {}={}", key.key(), value);
            config.set(key, value);
        }
    }
    return new ZoneAwareLoadBalancer<>(config, configuration.getRuleOrDefault(RoundRobinRule::new), configuration.getPingOrDefault(DummyPing::new), new RibbonServerList(serviceName, serviceDiscovery, serviceFilter), null, new PollingServerListUpdater(config));
}
Also used : PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) IClientConfig(com.netflix.client.config.IClientConfig) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) IClientConfigKey(com.netflix.client.config.IClientConfigKey) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ZoneAwareLoadBalancer

use of com.netflix.loadbalancer.ZoneAwareLoadBalancer 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 ZoneAwareLoadBalancer

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

the class RibbonClientConfigurationIntegrationTests method testLoadBalancerConstruction.

@Test
public void testLoadBalancerConstruction() {
    ILoadBalancer loadBalancer = clientFactory.getInstance("test", ILoadBalancer.class);
    assertThat(loadBalancer, is(instanceOf(ZoneAwareLoadBalancer.class)));
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) loadBalancer;
    ServerListUpdater serverListUpdater = (PollingServerListUpdater) ReflectionTestUtils.getField(loadBalancer, "serverListUpdater");
    Long refreshIntervalMs = (Long) ReflectionTestUtils.getField(serverListUpdater, "refreshIntervalMs");
    // assertThat(refreshIntervalMs, equalTo(999L));
    ServerListUpdater updater = clientFactory.getInstance("test", ServerListUpdater.class);
    assertThat(updater, is(sameInstance(serverListUpdater)));
}
Also used : PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ZoneAwareLoadBalancer

use of com.netflix.loadbalancer.ZoneAwareLoadBalancer in project ribbon by Netflix.

the class SampleApp method main.

public static void main(String[] args) throws Exception {
    // 1
    ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
    System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
    // 2
    RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
    // 3
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 20; i++) {
        // 4
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
    }
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
    System.out.println(lb.getLoadBalancerStats());
    ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", // 5
    "www.linkedin.com:80,www.google.com:80");
    System.out.println("changing servers ...");
    // 6
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
    }
    // 7
    System.out.println(lb.getLoadBalancerStats());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) URI(java.net.URI)

Example 5 with ZoneAwareLoadBalancer

use of com.netflix.loadbalancer.ZoneAwareLoadBalancer in project ribbon by Netflix.

the class SampleApp method main.

public static void main(String[] args) throws Exception {
    // 1
    ConfigurationManager.loadPropertiesFromResources("sample-client.properties");
    System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
    // 2
    RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
    // 3
    HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
    for (int i = 0; i < 20; i++) {
        // 4
        HttpResponse response = client.executeWithLoadBalancer(request);
        System.out.println("Status code for " + response.getRequestedURI() + "  :" + response.getStatus());
    }
    @SuppressWarnings("rawtypes") ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
    System.out.println(lb.getLoadBalancerStats());
    ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", // 5
    "www.linkedin.com:80,www.google.com:80");
    System.out.println("changing servers ...");
    // 6
    Thread.sleep(3000);
    for (int i = 0; i < 20; i++) {
        HttpResponse response = null;
        try {
            response = client.executeWithLoadBalancer(request);
            System.out.println("Status code for " + response.getRequestedURI() + "  : " + response.getStatus());
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }
    // 7
    System.out.println(lb.getLoadBalancerStats());
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) RestClient(com.netflix.niws.client.http.RestClient) HttpResponse(com.netflix.client.http.HttpResponse) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) URI(java.net.URI)

Aggregations

ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)5 HttpRequest (com.netflix.client.http.HttpRequest)2 HttpResponse (com.netflix.client.http.HttpResponse)2 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)2 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 RestClient (com.netflix.niws.client.http.RestClient)2 URI (java.net.URI)2 Test (org.junit.Test)2 IClientConfig (com.netflix.client.config.IClientConfig)1 IClientConfigKey (com.netflix.client.config.IClientConfigKey)1 ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)1 DiscoveryEnabledServer (com.netflix.niws.loadbalancer.DiscoveryEnabledServer)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Ignore (org.junit.Ignore)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 EurekaClientConfigBean (org.springframework.cloud.netflix.eureka.EurekaClientConfigBean)1 EurekaInstanceConfigBean (org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean)1 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)1