com.netflix.loadbalancer.ZoneAwareLoadBalancer Java Examples

The following examples show how to use com.netflix.loadbalancer.ZoneAwareLoadBalancer. 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 testBuildWithDiscoveryEnabledNIWSServerList() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<>();
    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);
}
 
Example #2
Source File: LBBuilderTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildWithDiscoveryEnabledNIWSServerListAndUpdater() {
    IRule rule = new AvailabilityFilteringRule();
    ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
    ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<>();
    ServerListUpdater updater = new PollingServerListUpdater();
    ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
            .withDynamicServerList(list)
            .withRule(rule)
            .withServerListFilter(filter)
            .withServerListUpdater(updater)
            .buildDynamicServerListLoadBalancerWithUpdater();
    assertNotNull(lb);
    assertEquals(Lists.newArrayList(expected), lb.getAllServers());
    assertSame(filter, lb.getFilter());
    assertSame(list, lb.getServerListImpl());
    assertSame(updater, lb.getServerListUpdater());
    Server server = lb.chooseServer();
    // make sure load balancer does not recreate the server instance
    assertTrue(server instanceof DiscoveryEnabledServer);
}
 
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: RibbonZuulCommon.java    From s2g-zuul with MIT License 5 votes vote down vote up
protected RestClient getRestClient() throws ZuulException {
		Application application = DiscoveryManager.getInstance().getDiscoveryClient().getApplication(serviceName);
		if (application == null) {
			throw new ZuulException( "Service-NotFoud",HttpServletResponse.SC_NOT_FOUND, serviceName + "服务未找到");
		}
		
		List<DiscoveryEnabledServer> instances = Lists.newArrayList();
		for (InstanceInfo info : application.getInstances()) {
			if (info.getStatus() == InstanceStatus.UP) {
				instances.add(new DiscoveryEnabledServer(info, false, false));
			}
		}

		RestClient client = (RestClient) ClientFactory.getNamedClient(serviceName);
		ZoneAwareLoadBalancer loadbalancer = (ZoneAwareLoadBalancer) client.getLoadBalancer();
		
//		//loadbalancer.setServersList(instances);		
//		IRule rule = new RandomRule();
//		int ruleLoad = ZuulCommandHelper.getLoadBalanceRule(commandGroup, commandKey);
//		if (ruleLoad == 2) {
//			rule = new ClientConfigEnabledRoundRobinRule();
//		} else if (ruleLoad == 3) {
//			rule=new AvailabilityFilteringRule();
//		} else if (ruleLoad == 3) {
//			rule=new ZoneAvoidanceRule();
//		} else if (ruleLoad == 4) {
//			rule=new RetryRule();
//		} else if (ruleLoad == 5) {
//			rule=new RoundRobinRule();
//		}else if (ruleLoad == 6) {
//			rule=new ResponseTimeWeightedRule();
//		}else if (ruleLoad == 7) {
//			rule=new WeightedResponseTimeRule();
//		}
//		loadbalancer.setRule(rule);
//		client.setLoadBalancer(loadbalancer);
		return client;
	}
 
Example #5
Source File: RibbonDefaultConfigApplication.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Throws exception if the SpringClientFactory doesn't return a balancer with a server
 * list of the expected type.
 *
 */
@PostConstruct
public void test() throws Exception {
	@SuppressWarnings("unchecked")
	ZoneAwareLoadBalancer<Server> lb = (ZoneAwareLoadBalancer<Server>) this.clientFactory.getLoadBalancer("baz");

	ServerList<Server> serverList = lb.getServerListImpl();
	if (!(serverList instanceof MyDefaultRibbonConfig.BazServiceList)) {
		throw new Exception("wrong server list type");
	}
}
 
Example #6
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param scheme Communication scheme (usually http or https)
 * @param loadBalancer Load Balancer
 * @param delegate Jersey Client delegate
 * @deprecated Use non-scheme constructor instead
 */
@Deprecated
public RibbonJerseyClient(
    final String scheme,
    final ZoneAwareLoadBalancer<Server> loadBalancer,
    final Client delegate) {
  this.loadBalancer = Objects.requireNonNull(loadBalancer);
  this.delegate = Objects.requireNonNull(delegate);
}
 
Example #7
Source File: NacosRibbonClientPropertyOverrideTests.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private ZoneAwareLoadBalancer<Server> getLoadBalancer(String name) {
	return (ZoneAwareLoadBalancer<Server>) this.factory.getLoadBalancer(name);
}
 
Example #8
Source File: RibbonJerseyClientBuilder.java    From dropwizard-consul with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link RibbonJerseyClient} with an existing Jersey Client and service discoverer
 *
 * @param name Client name
 * @param jerseyClient Jersey Client
 * @param serviceDiscoverer Service discoverer
 * @return new RibbonJerseyClient
 */
public RibbonJerseyClient build(
    final String name,
    final Client jerseyClient,
    final ConsulServiceDiscoverer serviceDiscoverer) {

  // dynamic server list that is refreshed from Consul
  final ConsulServerList serverList = new ConsulServerList(consul, serviceDiscoverer);

  // build a new load balancer based on the configuration
  final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
  clientConfig.set(CommonClientConfigKey.AppName, name);
  clientConfig.set(
      CommonClientConfigKey.ServerListRefreshInterval,
      Ints.checkedCast(configuration.getRefreshInterval().toMilliseconds()));

  final ZoneAwareLoadBalancer<Server> loadBalancer =
      LoadBalancerBuilder.newBuilder()
          .withClientConfig(clientConfig)
          .withRule(new WeightedResponseTimeRule())
          .withDynamicServerList(serverList)
          .buildDynamicServerListLoadBalancer();

  final RibbonJerseyClient client = new RibbonJerseyClient(loadBalancer, jerseyClient);

  environment
      .lifecycle()
      .manage(
          new Managed() {
            @Override
            public void start() throws Exception {
              // nothing to start
            }

            @Override
            public void stop() throws Exception {
              client.close();
            }
          });
  return client;
}
 
Example #9
Source File: RibbonJerseyClient.java    From dropwizard-consul with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor
 *
 * @param loadBalancer Load Balancer
 * @param delegate Jersey Client delegate
 */
public RibbonJerseyClient(
    final ZoneAwareLoadBalancer<Server> loadBalancer, final Client delegate) {
  this.loadBalancer = Objects.requireNonNull(loadBalancer);
  this.delegate = Objects.requireNonNull(delegate);
}