Java Code Examples for io.grpc.NameResolver#getProvidersForLoadBalance()

The following examples show how to use io.grpc.NameResolver#getProvidersForLoadBalance() . 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: ErrorNumberUtil.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
/**
 * 将当前出错的服务器从备选列表中去除
 *
 * @author sxp
 * @since 2018-6-21
 * @since 2019/12/11 modify by wlh 10分钟(时间可配)后,将服务重新放回至服务提供列表
 */
private static void removeCurrentProvider(NameResolver nameResolver, String providerId, String method) {
  Map<String, ServiceProvider> providersForLoadBalance = nameResolver.getProvidersForLoadBalance();
  if (providersForLoadBalance == null || providersForLoadBalance.size() == 0) {
    logger.info("客户端的备选列表为空", providerId);
    return;
  }

  if (providersForLoadBalance.containsKey(providerId)) {
    logger.error("FATAL ERROR : 服务器节点{}连续调用出错{}次,从客户端备选服务器列表中删除", providerId, switchoverThreshold);
    providersForLoadBalance.remove(providerId);
    nameResolver.reCalculateProvidersCountAfterLoadBalance(method);

    if (timerService == null) {
      timerService = SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE);
    }
    timerService.schedule(new RecoveryServerRunnable(nameResolver, providerId, method), recoveryMilliseconds, TimeUnit.MILLISECONDS);
  }
}
 
Example 2
Source File: ErrorNumberUtil.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * 获取客户端服务列表中服务提供者的数量
 *
 * @author sxp
 * @since 2018-7-7
 */
private static int getConsumerProvidersAmount(NameResolver nameResolver) {
  Map<String, ServiceProvider> providersForLoadBalance = nameResolver.getProvidersForLoadBalance();
  if (providersForLoadBalance == null) {
    return 0;
  }
  return providersForLoadBalance.size();
}
 
Example 3
Source File: ErrorNumberUtil.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * 将服务重新添加到服务提供者列表中
 *
 * @author wlh
 * @since 2019/12/11
 */
public static void addCurrentProvider(NameResolver nameResolver, String providerId, String method) {
  Map<String, ServiceProvider> providersForLoadBalance = nameResolver.getProvidersForLoadBalance();

  if (providersForLoadBalance != null && !providersForLoadBalance.containsKey(providerId)) {
    Map<String, ServiceProvider> allProviders = nameResolver.getAllProviders();
    ServiceProvider serviceProvider = allProviders.get(providerId);

    if (serviceProvider != null) {
      logger.info("服务器节点{}被重新添加到客户端备选服务器列表中", providerId);
      providersForLoadBalance.put(providerId, serviceProvider);
      nameResolver.reCalculateProvidersCountAfterLoadBalance(method);
    }
  }
}