Search in sources :

Example 1 with IRule

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

the class LBBuilderTest method testBuildWithDiscoveryEnabledNIWSServerList.

@Test
public void testBuildWithDiscoveryEnabledNIWSServerList() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<DiscoveryEnabledServer>();
    ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder().withDynamicServerList(list).withRule(rule).withServerListFilter(filter).buildDynamicServerListLoadBalancer();
    assertNotNull(lb);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
    assertSame(filter, lb.getFilter());
    assertSame(list, lb.getServerListImpl());
    Server server = lb.chooseServer();
    // make sure load balancer does not recreate the server instance
    assertTrue(server instanceof DiscoveryEnabledServer);
}
Also used : Server(com.netflix.loadbalancer.Server) ZoneAffinityServerListFilter(com.netflix.loadbalancer.ZoneAffinityServerListFilter) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) IRule(com.netflix.loadbalancer.IRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with IRule

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

the class LBBuilderTest method testBuildStaticServerListLoadBalancer.

@Test
public void testBuildStaticServerListLoadBalancer() {
    List<Server> list = Lists.newArrayList(expected, expected);
    IRule rule = new AvailabilityFilteringRule();
    IClientConfig clientConfig = IClientConfig.Builder.newBuilder().withDefaultValues().withMaxAutoRetriesNextServer(3).build();
    assertEquals(3, clientConfig.get(Keys.MaxAutoRetriesNextServer).intValue());
    BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder().withRule(rule).buildFixedServerListLoadBalancer(list);
    assertEquals(list, lb.getAllServers());
    assertSame(rule, lb.getRule());
}
Also used : Server(com.netflix.loadbalancer.Server) IClientConfig(com.netflix.client.config.IClientConfig) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) IRule(com.netflix.loadbalancer.IRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with IRule

use of com.netflix.loadbalancer.IRule in project java-chassis by ServiceComb.

the class LoadbalanceHandler method createLoadBalancer.

private LoadBalancer createLoadBalancer(String appId, String microserviceName, String microserviceVersionRule, String transportName) {
    IRule rule;
    try {
        rule = (IRule) Class.forName(policy, true, Thread.currentThread().getContextClassLoader()).newInstance();
        LOGGER.info("Using loadbalance rule [{}] for service [{},{}].", policy, microserviceName, transportName);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        LOGGER.warn("Loadbalance rule [{}] is incorrect, using default RoundRobinRule.", policy);
        rule = new RoundRobinRule();
    }
    CseServerList serverList = new CseServerList(appId, microserviceName, microserviceVersionRule, transportName);
    LoadBalancer lb = new LoadBalancer(serverList, rule);
    return lb;
}
Also used : RoundRobinRule(com.netflix.loadbalancer.RoundRobinRule) IRule(com.netflix.loadbalancer.IRule)

Aggregations

IRule (com.netflix.loadbalancer.IRule)3 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)2 Server (com.netflix.loadbalancer.Server)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 IClientConfig (com.netflix.client.config.IClientConfig)1 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)1 RoundRobinRule (com.netflix.loadbalancer.RoundRobinRule)1 ZoneAffinityServerListFilter (com.netflix.loadbalancer.ZoneAffinityServerListFilter)1