gpt4 book ai didi

com.netflix.loadbalancer.ZoneAwareLoadBalancer类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 21:07:51 25 4
gpt4 key购买 nike

本文整理了Java中com.netflix.loadbalancer.ZoneAwareLoadBalancer类的一些代码示例,展示了ZoneAwareLoadBalancer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZoneAwareLoadBalancer类的具体详情如下:
包路径:com.netflix.loadbalancer.ZoneAwareLoadBalancer
类名称:ZoneAwareLoadBalancer

ZoneAwareLoadBalancer介绍

[英]Load balancer that can avoid a zone as a whole when choosing server.

The key metric used to measure the zone condition is Average Active Requests, which is aggregated per rest client per zone. It is the total outstanding requests in a zone divided by number of available targeted instances (excluding circuit breaker tripped instances). This metric is very effective when timeout occurs slowly on a bad zone.

The LoadBalancer will calculate and examine zone stats of all available zones. If the Average Active Requests for any zone has reached a configured threshold, this zone will be dropped from the active server list. In case more than one zone has reached the threshold, the zone with the most active requests per server will be dropped. Once the the worst zone is dropped, a zone will be chosen among the rest with the probability proportional to its number of instances. A server will be returned from the chosen zone with a given Rule (A Rule is a load balancing strategy, for example AvailabilityFilteringRule) For each request, the steps above will be repeated. That is to say, each zone related load balancing decisions are made at real time with the up-to-date statistics aiding the choice.
[中]在选择服务器时可以避免整个区域的负载平衡器。
用于衡量区域状况的关键指标是平均活动请求数,即每个rest客户端和每个区域的平均活动请求数。它是一个区域中未完成的请求总数除以可用的目标实例数(不包括断路器跳闸实例)。当超时在坏区缓慢发生时,此指标非常有效。
LoadBalancer将计算并检查所有可用区域的区域统计信息。如果任何区域的平均活动请求已达到配置的阈值,则该区域将从活动服务器列表中删除。如果有多个区域达到阈值,则每个服务器上具有最活跃请求的区域将被丢弃。一旦最差的区域被删除,将在其余区域中选择一个区域,其概率与其实例数成正比。对于每个请求,将使用给定的规则(规则是一种负载平衡策略,例如AvailabilityFilteringRule)从所选区域返回服务器,并重复上述步骤。也就是说,每个区域相关的负载平衡决策都是实时做出的,最新的统计数据有助于做出选择。

代码示例

代码示例来源:origin: yu199195/hmily

@Override
public Server chooseServer(final Object key) {
  List<Server> serverList;
  serverList = super.getServerListImpl().getUpdatedListOfServers();
  serverList = super.getFilter().getFilteredListOfServers(serverList);
  if (null == serverList || serverList.isEmpty() || serverList.size() == 1) {
    return super.chooseServer(key);
  }
  final Server server = super.chooseServer(key);
  final HmilyTransactionContext hmilyTransactionContext = HmilyTransactionContextLocal.getInstance().get();
  if (Objects.isNull(hmilyTransactionContext)) {
    return server;
  }
  final String transId = hmilyTransactionContext.getTransId();
  //if try
  if (hmilyTransactionContext.getAction() == HmilyActionEnum.TRYING.getCode()) {
    SERVER_MAP.put(transId, server);
    return server;
  }
  final Server oldServer = SERVER_MAP.get(transId);
  SERVER_MAP.remove(transId);
  if (Objects.nonNull(oldServer)) {
    for (Server s : serverList) {
      if (Objects.equals(s, oldServer)) {
        return oldServer;
      }
    }
  }
  return server;
}

代码示例来源:origin: Nepxion/Discovery

@Bean
  public ILoadBalancer ribbonLoadBalancer(IClientConfig config, ServerList<Server> serverList, ServerListFilter<Server> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
    if (this.propertiesFactory.isSet(ILoadBalancer.class, serviceId)) {
      return this.propertiesFactory.get(ILoadBalancer.class, config, serviceId);
    }

    ZoneAwareLoadBalancer<?> loadBalancer = new ZoneAwareLoadBalancer<>(config, rule, ping, serverList, serverListFilter, serverListUpdater);
    loadBalanceListenerExecutor.setLoadBalancer(loadBalancer);

    return loadBalancer;
  }
}

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

@VisibleForTesting
BaseLoadBalancer getLoadBalancer(String zone) {
  zone = zone.toLowerCase();
  BaseLoadBalancer loadBalancer = balancers.get(zone);
  if (loadBalancer == null) {
    // We need to create rule object for load balancer for each zone
    IRule rule = cloneRule(this.getRule());
    loadBalancer = new BaseLoadBalancer(this.getName() + "_" + zone, rule, this.getLoadBalancerStats());
    BaseLoadBalancer prev = balancers.putIfAbsent(zone, loadBalancer);
    if (prev != null) {
      loadBalancer = prev;
    }
  } 
  return loadBalancer;        
}

代码示例来源:origin: spring-cloud/spring-cloud-core-tests

/**
 * 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");
  }
}

代码示例来源:origin: smoketurner/dropwizard-consul

/**
 * Fetch a server from the load balancer or throw an exception if none are available.
 *
 * @return a server
 * @throws IllegalStateException if no servers are available
 */
private Server fetchServerOrThrow() {
 final Server server = loadBalancer.chooseServer();
 if (server == null) {
  throw new IllegalStateException("No available servers for " + loadBalancer.getName());
 }
 return server;
}

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

@Override
public Server chooseServer(Object key) {
  if (!ENABLED.get() || getLoadBalancerStats().getAvailableZones().size() <= 1) {
    logger.debug("Zone aware logic disabled or there is only one zone");
    return super.chooseServer(key);
    LoadBalancerStats lbStats = getLoadBalancerStats();
    Map<String, ZoneSnapshot> zoneSnapshot = ZoneAvoidanceRule.createSnapshot(lbStats);
    logger.debug("Zone snapshots: {}", zoneSnapshot);
    if (triggeringLoad == null) {
      triggeringLoad = DynamicPropertyFactory.getInstance().getDoubleProperty(
          "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".triggeringLoadPerServerThreshold", 0.2d);
          "ZoneAwareNIWSDiscoveryLoadBalancer." + this.getName() + ".avoidZoneWithBlackoutPercetage", 0.99999d);
      logger.debug("Zone chosen: {}", zone);
      if (zone != null) {
        BaseLoadBalancer zoneLoadBalancer = getLoadBalancer(zone);
        server = zoneLoadBalancer.chooseServer(key);

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

@Override
  public void setRule(IRule rule) {
    super.setRule(rule);
    if (balancers != null) {
      for (String zone: balancers.keySet()) {
        balancers.get(zone).setRule(cloneRule(rule));
      }
    }
  }
}

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

private IRule cloneRule(IRule toClone) {
  IRule rule;
  if (toClone == null) {
    rule = new AvailabilityFilteringRule();
  } else {
    String ruleClass = toClone.getClass().getName();                
    try {
      rule = (IRule) ClientFactory.instantiateInstanceWithClientConfig(ruleClass, this.getClientConfig());
    } catch (Exception e) {
      throw new RuntimeException("Unexpected exception creating rule for ZoneAwareLoadBalancer", e);
    }
  }
  return rule;
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-netflix-ribbon

@Bean
@ConditionalOnMissingBean
public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
    ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
    IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
  if (this.propertiesFactory.isSet(ILoadBalancer.class, name)) {
    return this.propertiesFactory.get(ILoadBalancer.class, config, name);
  }
  return new ZoneAwareLoadBalancer<>(config, rule, ping, serverList,
      serverListFilter, serverListUpdater);
}

代码示例来源:origin: liaokailin/springcloud

@RequestMapping("/")
public void getServerList() throws Exception {
  ZoneAwareLoadBalancer<Server> lb = (ZoneAwareLoadBalancer<Server>) clientFactory.getLoadBalancer("myclient");
  ServerList<Server> serverList = lb.getServerListImpl();
  List<Server> serverDetailList = serverList.getInitialListOfServers();
  if (!CollectionUtils.isEmpty(serverDetailList)) {
    for (Server s : serverDetailList) {
      System.out.println(s.getHost() + "," + s.getPort());
    }
  } else {
    System.out.println("no service");
  }
}

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

/**
 * Build a {@link ZoneAwareLoadBalancer} with a dynamic {@link ServerList} and an {@link IRule}. The {@link ServerList} can be
 * either set in the {@link #withDynamicServerList(ServerList)} or in the {@link IClientConfig} using {@link CommonClientConfigKey#NIWSServerListClassName}.
 * The {@link IRule} can be either set by {@link #withRule(IRule)} or in the {@link IClientConfig} using
 * {@link CommonClientConfigKey#NFLoadBalancerRuleClassName}. 
 */
public ZoneAwareLoadBalancer<T> buildDynamicServerListLoadBalancer() {
  if (serverListImpl == null) {
    serverListImpl = createServerListFromConfig(config);
  }
  if (rule == null) {
    rule = createRuleFromConfig(config);
  }
  return new ZoneAwareLoadBalancer<T>(config, rule, ping, serverListImpl, serverListFilter);
}

代码示例来源:origin: com.netflix.ribbon/ribbon-loadbalancer

/**
 * Build a {@link ZoneAwareLoadBalancer} with a dynamic {@link ServerList} and an {@link IRule} and a {@link ServerListUpdater}.
 *
 * The {@link ServerList} can be either set in the {@link #withDynamicServerList(ServerList)} or in the {@link IClientConfig}
 * using {@link CommonClientConfigKey#NIWSServerListClassName}.
 * The {@link IRule} can be either set by {@link #withRule(IRule)} or in the {@link IClientConfig} using
 * {@link CommonClientConfigKey#NFLoadBalancerRuleClassName}.
 * The {@link ServerListUpdater} can be either set by {@link #withServerListUpdater(ServerListUpdater)} or
 * in the {@link IClientConfig} using {@link CommonClientConfigKey#ServerListUpdaterClassName}.
 */
public ZoneAwareLoadBalancer<T> buildDynamicServerListLoadBalancerWithUpdater() {
  if (serverListImpl == null) {
    serverListImpl = createServerListFromConfig(config);
  }
  if (rule == null) {
    rule = createRuleFromConfig(config);
  }
  if (serverListUpdater == null) {
    serverListUpdater = createServerListUpdaterFromConfig(config);
  }
  return new ZoneAwareLoadBalancer<T>(config, rule, ping, serverListImpl, serverListFilter, serverListUpdater);
}

代码示例来源:origin: org.apache.camel/camel-ribbon

private ZoneAwareLoadBalancer<RibbonServiceDefinition> createLoadBalancer(String serviceName) {
  // setup client config
  IClientConfig config = configuration.getClientName() != null
    ? IClientConfig.Builder.newBuilder(configuration.getClientName()).build()
    : IClientConfig.Builder.newBuilder().build();
  if (configuration.getProperties() != null) {
    for (Map.Entry<String, String> entry : configuration.getProperties().entrySet()) {
      IClientConfigKey key = CommonClientConfigKey.valueOf(entry.getKey());
      String value = entry.getValue();
      LOGGER.debug("RibbonClientConfig: {}={}", key.key(), value);
      config.set(key, value);
    }
  }
  ZoneAwareLoadBalancer<RibbonServiceDefinition> loadBalancer;
  if (serviceDiscovery != null) {
    loadBalancer = new ZoneAwareLoadBalancer<>(
      config,
      configuration.getRuleOrDefault(RoundRobinRule::new),
      configuration.getPingOrDefault(DummyPing::new),
      new RibbonServerList(serviceName, serviceDiscovery, serviceFilter),
      null,
      new PollingServerListUpdater(config));
  } else {
    loadBalancer = new ZoneAwareLoadBalancer<>(config);
  }
  return loadBalancer;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com