com.netflix.loadbalancer.DynamicServerListLoadBalancer Java Examples

The following examples show how to use com.netflix.loadbalancer.DynamicServerListLoadBalancer. 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: LBBuilderTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildWithArchaiusProperties() {
    Configuration config = ConfigurationManager.getConfigInstance();
    config.setProperty("client1.niws.client." + Keys.DeploymentContextBasedVipAddresses, "dummy:7001");
    config.setProperty("client1.niws.client." + Keys.InitializeNFLoadBalancer, "true");
    config.setProperty("client1.niws.client." + Keys.NFLoadBalancerClassName, DynamicServerListLoadBalancer.class.getName());
    config.setProperty("client1.niws.client." + Keys.NFLoadBalancerRuleClassName, RoundRobinRule.class.getName());
    config.setProperty("client1.niws.client." + Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    config.setProperty("client1.niws.client." + Keys.NIWSServerListFilterClassName, ZoneAffinityServerListFilter.class.getName());
    config.setProperty("client1.niws.client." + Keys.ServerListUpdaterClassName, PollingServerListUpdater.class.getName());
    IClientConfig clientConfig = IClientConfig.Builder.newBuilder(NiwsClientConfig.class, "client1").build();
    ILoadBalancer lb = LoadBalancerBuilder.newBuilder().withClientConfig(clientConfig).buildLoadBalancerFromConfigWithReflection();
    assertNotNull(lb);
    assertEquals(DynamicServerListLoadBalancer.class.getName(), lb.getClass().getName());
    DynamicServerListLoadBalancer<Server> dynamicLB = (DynamicServerListLoadBalancer<Server>) lb;
    assertTrue(dynamicLB.getServerListUpdater() instanceof PollingServerListUpdater);
    assertTrue(dynamicLB.getFilter() instanceof ZoneAffinityServerListFilter);
    assertTrue(dynamicLB.getRule() instanceof RoundRobinRule);
    assertTrue(dynamicLB.getPing() instanceof DummyPing);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
}
 
Example #2
Source File: ClientFactoryTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testChooseServers() {
	assertNotNull(client);
	DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) client.getLoadBalancer();
	assertTrue(lb.getServerListImpl() instanceof ConfigurationBasedServerList);
	Set<Server> expected = new HashSet<Server>();
	expected.add(new Server("www.example1.come:80"));
	expected.add(new Server("www.example2.come:80"));
	expected.add(new Server("www.example3.come:80"));
	Set<Server> result = new HashSet<Server>();
	for (int i = 0; i <= 10; i++) {
		Server s = lb.chooseServer();
		result.add(s);			
	}
	assertEquals(expected, result);
}
 
Example #3
Source File: DefaultClientChannelManager.java    From zuul with Apache License 2.0 6 votes vote down vote up
protected DynamicServerListLoadBalancer<?> createLoadBalancer(IClientConfig clientConfig) {
    // Create and configure a loadbalancer for this vip.
    String loadBalancerClassName = clientConfig.get(NFLoadBalancerClassName, ZoneAwareLoadBalancer.class.getName());

    DynamicServerListLoadBalancer<?> lb;
    try {
        Class<?> clazz = Class.forName(loadBalancerClassName);
        lb = clazz.asSubclass(DynamicServerListLoadBalancer.class).getConstructor().newInstance();
        lb.initWithNiwsConfig(clientConfig);
    } catch (Exception e) {
        Throwables.throwIfUnchecked(e);
        throw new IllegalStateException("Could not instantiate LoadBalancer " + loadBalancerClassName, e);
    }

    return lb;
}
 
Example #4
Source File: ServiceCacheEvictor.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public synchronized void onEvent(EurekaEvent event) {
    if (event instanceof CacheRefreshedEvent) {
        if (!evictAll && toEvict.isEmpty()) return;
        if (evictAll) {
            serviceCacheEvicts.forEach(ServiceCacheEvict::evictCacheAllService);
            loadBalancerRegistry.values().forEach(DynamicServerListLoadBalancer::updateListOfServers);
            evictAll = false;
        } else {
            toEvict.forEach(ServiceRef::evict);
            toEvict.clear();
        }
        loadBalancerRegistry.values().forEach(DynamicServerListLoadBalancer::updateListOfServers);
    }
}
 
Example #5
Source File: RuleBaseConfig.java    From spring-cloud-ribbon-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * The load balancer definition.
 *
 * @param config            the client config.
 * @param serverList        the server list.
 * @param serverListFilter  the server list filter.
 * @param rule              the load balancing rule.
 * @param ping              the ping strategy.
 * @param serverListUpdater the server list updater.
 * @return The Dynamic Server List Load Balancer.
 */
@Bean
@ConditionalOnMissingBean
public ILoadBalancer loadBalancer(IClientConfig config,
                                  ServerList<Server> serverList,
                                  ServerListFilter<Server> serverListFilter,
                                  IRule rule, IPing ping,
                                  ServerListUpdater serverListUpdater) {
    log.debug("dynamic server list load balancer enabled.");
    return new DynamicServerListLoadBalancer<>(config, rule, ping, serverList,
            serverListFilter, serverListUpdater);
}
 
Example #6
Source File: RefreshEurekaServersListener.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
	loadBalancers.stream().forEach(lb -> {
		if (lb instanceof DynamicServerListLoadBalancer) {
			((DynamicServerListLoadBalancer<?>)lb).updateListOfServers();
		}
	});
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: ServiceCacheEvictor.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
public void registerLoadBalancer(DynamicServerListLoadBalancer loadBalancer) {
    String loadBalancerName = loadBalancer.getName();
    loadBalancerRegistry.put(loadBalancerName, loadBalancer);
}
 
Example #12
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 #13
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 #14
Source File: DefaultClientChannelManager.java    From zuul with Apache License 2.0 4 votes vote down vote up
public DynamicServerListLoadBalancer<?> getLoadBalancer() {
    return this.loadBalancer;
}