Java Code Examples for com.netflix.client.ClientFactory#getNamedLoadBalancer()

The following examples show how to use com.netflix.client.ClientFactory#getNamedLoadBalancer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultNIWSServerListFilterTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })    
@Test
public void testZoneExclusivity() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest2.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest2.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest2.ribbon.EnableZoneExclusivity", "true");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest2.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());
    DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest2");
    ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
    LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
    List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();        
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(1, "c"));
    servers.add(createServer(2, "c"));
    servers.add(createServer(3, "c"));
    servers.add(createServer(4, "c"));
    servers.add(createServer(5, "c"));
    List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
    List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    expected.add(createServer(4, "c"));
    expected.add(createServer(5, "c"));
    assertEquals(expected, filtered);
    lb.setServersList(filtered);        
    for (int i = 1; i <= 4; i++) {            
        loadBalancerStats.incrementActiveRequestsCount(createServer(i, "c"));
    }
    filtered = filter.getFilteredListOfServers(servers);
    assertEquals(expected, filtered);
}
 
Example 2
Source File: PrimeConnectionsTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimeConnectionsSmallPool() throws Exception {
	Configuration config = ConfigurationManager.getConfigInstance();
	config.setProperty("PrimeConnectionsTest1.ribbon.NFLoadBalancerClassName", 
			com.netflix.loadbalancer.DynamicServerListLoadBalancer.class.getName());
	config.setProperty("PrimeConnectionsTest1.ribbon.NIWSServerListClassName", SmallFixedServerList.class.getName());
	config.setProperty("PrimeConnectionsTest1.ribbon.EnablePrimeConnections", "true");
	DynamicServerListLoadBalancer<Server> lb = (DynamicServerListLoadBalancer<Server>) ClientFactory.getNamedLoadBalancer("PrimeConnectionsTest1");
	PrimeConnectionEndStats stats = lb.getPrimeConnections().getEndStats();
	assertEquals(stats.success, SMALL_FIXED_SERVER_LIST_SIZE);
}
 
Example 3
Source File: PrimeConnectionsTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimeConnectionsLargePool() throws Exception {
	Configuration config = ConfigurationManager.getConfigInstance();
	config.setProperty("PrimeConnectionsTest2.ribbon.NFLoadBalancerClassName", 
			com.netflix.loadbalancer.DynamicServerListLoadBalancer.class.getName());
	config.setProperty("PrimeConnectionsTest2.ribbon.NIWSServerListClassName", LargeFixedServerList.class.getName());
	config.setProperty("PrimeConnectionsTest2.ribbon.EnablePrimeConnections", "true");
	DynamicServerListLoadBalancer<Server> lb = (DynamicServerListLoadBalancer<Server>) ClientFactory.getNamedLoadBalancer("PrimeConnectionsTest2");
	PrimeConnectionEndStats stats = lb.getPrimeConnections().getEndStats();
	assertEquals(stats.success, LARGE_FIXED_SERVER_LIST_SIZE);
}
 
Example 4
Source File: ServerListLoabBalancerTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
	Configuration config = ConfigurationManager.getConfigInstance();
	config.setProperty("ServerListLoabBalancerTest.ribbon.NFLoadBalancerClassName", 
			com.netflix.loadbalancer.DynamicServerListLoadBalancer.class.getName());
	config.setProperty("ServerListLoabBalancerTest.ribbon.NIWSServerListClassName", FixedServerList.class.getName());
	lb = (DynamicServerListLoadBalancer<Server>) ClientFactory.getNamedLoadBalancer("ServerListLoabBalancerTest");
}
 
Example 5
Source File: LBClientFactory.java    From feign with Apache License 2.0 5 votes vote down vote up
@Override
public LBClient create(String clientName) {
  IClientConfig config =
      ClientFactory.getNamedConfig(clientName, DisableAutoRetriesByDefaultClientConfig.class);
  ILoadBalancer lb = ClientFactory.getNamedLoadBalancer(clientName);
  return LBClient.create(lb, config);
}
 
Example 6
Source File: DefaultNIWSServerListFilterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZoneAffinityEnabled() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());

    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.EnableZoneAffinity", "true");
    DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest1");
    assertTrue(lb.getRule() instanceof AvailabilityFilteringRule);
    ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
    LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
    List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();        
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(1, "c"));
    servers.add(createServer(2, "c"));
    servers.add(createServer(3, "c"));
    servers.add(createServer(4, "c"));
    servers.add(createServer(5, "c"));
    List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
    List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    expected.add(createServer(4, "c"));
    expected.add(createServer(5, "c"));
    assertEquals(expected, filtered);
    lb.setServersList(filtered);        
    for (int i = 1; i <= 4; i++) {            
        loadBalancerStats.incrementActiveRequestsCount(createServer(i, "c"));
    }
    filtered = filter.getFilteredListOfServers(servers);
    assertEquals(servers, filtered);

}
 
Example 7
Source File: DefaultNIWSServerListFilterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZoneAffinityOverride() throws Exception {
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.EnableZoneAffinity", "true");
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());
    ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest3.ribbon.zoneAffinity.minAvailableServers", "3");
    DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest3");
    ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
    LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
    List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();        
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(1, "c"));
    servers.add(createServer(2, "c"));
    List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
    List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
    /*
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    expected.add(createServer(4, "c"));
    expected.add(createServer(5, "c")); */
    // less than 3 servers in zone c, will not honor zone affinity
    assertEquals(servers, filtered);
    lb.setServersList(filtered);        
    servers.add(createServer(3, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    expected.add(createServer(1, "c"));
    expected.add(createServer(2, "c"));
    expected.add(createServer(3, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    // now we have enough servers in C
    assertEquals(expected, filtered);

    // make one server black out
    for (int i = 1; i <= 3; i++) {            
        loadBalancerStats.incrementSuccessiveConnectionFailureCount(createServer(1, "c"));
    }
    filtered = filter.getFilteredListOfServers(servers);
    assertEquals(servers, filtered);
    // new server added in zone c, zone c should now have enough servers
    servers.add(createServer(4, "c"));
    filtered = filter.getFilteredListOfServers(servers);
    expected.add(createServer(4, "c"));
    assertEquals(expected, filtered);
}
 
Example 8
Source File: SubsetFilterTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithLoadBalancer() {
    DynamicServerListLoadBalancer<Server> lb = (DynamicServerListLoadBalancer<Server>) 
            ClientFactory.getNamedLoadBalancer("SubsetFilerTest");        
    MockServerList serverList = (MockServerList) lb.getServerListImpl();
    Object[][] serverStats = { 
            {"server0", 0, 0},
            {"server1", 0, 0},
            {"server2", 0, 0},
            {"server3", 0, 0},
            {"server4", 0, 0},
            {"server5", 0, 0},
            {"server6", 0, 0},
            {"server7", 0, 0},
            {"server8", 0, 0},
            {"server9", 0, 0}
    };
    LoadBalancerStats stats = lb.getLoadBalancerStats();
    List<Server> list = getServersAndStats(stats, serverStats);
    serverList.setServerList(list);

    Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);

    lb.updateListOfServers();
    List<Server> filtered = lb.getAllServers();
    // first filtering, should get 5 servers 
    assertEquals(filtered.size(), 5);
    
    Server s1 = filtered.get(0);        
    Server s2 = filtered.get(1);
    Server s3 = filtered.get(2);
    Server s4 = filtered.get(3);
    Server s5 = filtered.get(4);

    // failure count > threshold
    DummyServerStats stats1 = (DummyServerStats) stats.getSingleServerStat(s1);
    stats1.setConnectionFailureCount(3);
    
    // active requests count > threshold
    DummyServerStats stats2 = (DummyServerStats) stats.getSingleServerStat(s2);
    stats2.setActiveRequestsCount(3);

    // will be forced eliminated after sorting 
    DummyServerStats stats3 = (DummyServerStats) stats.getSingleServerStat(s3);
    stats3.setActiveRequestsCount(2);

    // will be forced eliminated after sorting
    DummyServerStats stats4 = (DummyServerStats) stats.getSingleServerStat(s4);
    stats4.setConnectionFailureCount(1);

    // filter again, this time some servers will be eliminated
    serverList.setServerList(list);
    lb.updateListOfServers();
    filtered = lb.getAllServers();
    
    assertEquals(5, filtered.size());
    assertTrue(!filtered.contains(s1));
    assertTrue(!filtered.contains(s2));
    assertTrue(filtered.contains(s3));
    assertTrue(!filtered.contains(s4));
    assertTrue(filtered.contains(s5));
    
    // Not enough healthy servers, just get whatever is available
    serverList.setServerList(Lists.newArrayList(filtered));
    lb.updateListOfServers();
    List<Server> lastFiltered = lb.getAllServers();
    assertEquals(5, lastFiltered.size());

}
 
Example 9
Source File: ZoneAwareLoadBalancerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testConstruction() {
    ConfigurationManager.getConfigInstance().setProperty("mylb.ribbon.NFLoadBalancerRuleClassName", RandomRule.class.getName());
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) ClientFactory.getNamedLoadBalancer("mylb");
    assertTrue(lb.getLoadBalancer("myzone").getRule() instanceof RandomRule);
}
 
Example 10
Source File: ZoneAwareLoadBalancerTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
@Test
public void testActiveConnectionsLimit() {
    ConfigurationManager.getConfigInstance().clearProperty("niws.loadbalancer.serverStats.activeRequestsCount.effectiveWindowSeconds");
    ConfigurationManager.getConfigInstance().setProperty("testlb.ribbon.ActiveConnectionsLimit", 1);

	ZoneAwareLoadBalancer balancer = (ZoneAwareLoadBalancer) ClientFactory.getNamedLoadBalancer("testlb");
    LoadBalancerStats loadBalancerStats = balancer.getLoadBalancerStats();
    assertNotNull(loadBalancerStats);
    List<Server> servers = new ArrayList<Server>();        
    servers.add(createServer(1, "a"));
    servers.add(createServer(2, "a"));
    servers.add(createServer(3, "a"));
    servers.add(createServer(4, "a"));
    servers.add(createServer(5, "a"));
    servers.add(createServer(6, "a"));
    servers.add(createServer(1, "b"));
    servers.add(createServer(2, "b"));
    servers.add(createServer(3, "b"));
    servers.add(createServer(4, "b"));
    servers.add(createServer(5, "b"));
    servers.add(createServer(6, "b"));
    servers.add(createServer(7, "b"));
    servers.add(createServer(8, "b"));
    balancer.setServersList(servers);
    balancer.setUpServerList(servers);
    // cause both zone a and b outage 
    Server server = servers.get(0);
    loadBalancerStats.getSingleServerStat(server).incrementActiveRequestsCount();
    server = servers.get(8);
    loadBalancerStats.getSingleServerStat(server).incrementActiveRequestsCount();
    
    server = servers.get(servers.size() - 1);
    for (int j = 0; j < 3; j++) {
        loadBalancerStats.getSingleServerStat(server).incrementSuccessiveConnectionFailureCount();
    }

    Set<Server> expected = new HashSet<Server>();
    expected.add(createServer(2, "a"));
    expected.add(createServer(3, "a"));
    expected.add(createServer(4, "a"));
    expected.add(createServer(5, "a"));
    expected.add(createServer(6, "a"));
    expected.add(createServer(1, "b"));
    expected.add(createServer(2, "b"));
    expected.add(createServer(4, "b"));
    expected.add(createServer(5, "b"));
    expected.add(createServer(6, "b"));
    expected.add(createServer(7, "b"));
    AvailabilityFilteringRule rule = (AvailabilityFilteringRule) balancer.getRule();
    assertEquals(expected.size(), rule.getAvailableServersCount());
    AvailabilityFilteringRule rule1 = (AvailabilityFilteringRule) balancer.getLoadBalancer("us-east-1a").getRule();
    assertEquals(5, rule1.getAvailableServersCount());
    AvailabilityFilteringRule rule2 = (AvailabilityFilteringRule) balancer.getLoadBalancer("us-east-1b").getRule();
    assertEquals(6, rule2.getAvailableServersCount());
    
    Set<Server> result = new HashSet<Server>();
    for (int i = 0; i < 100; i++) {            
        result.add(balancer.chooseServer(null));
    }
    assertEquals(expected, result);
}