gpt4 book ai didi

com.github.dapeng.registry.zookeeper.ZkServiceInfo类的使用及代码示例

转载 作者:知者 更新时间:2024-03-18 14:35:31 24 4
gpt4 key购买 nike

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

ZkServiceInfo介绍

[英]service information of ZK, including runtime and config
[中]ZK的服务信息,包括运行时和配置

代码示例

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 同步cookie注入的规则
 *
 * @param serviceInfo
 */
private void syncZkCookieRuleInfo(ZkServiceInfo serviceInfo) {
  LOGGER.warn("ClientZKAgent::syncZkCookieRuleInfo service:" + serviceInfo.serviceName());
  String servicePath = COOKIE_RULES_PATH + "/" + serviceInfo.serviceName();
  try {
    byte[] data = zk.getData(servicePath, this, null);
    List<CookieRule> cookieRules = processCookieRuleData(data);
    serviceInfo.cookieRules(cookieRules);
    LOGGER.warn("ClientZk::syncZkCookieRuleInfo rules changes:" + cookieRules);
  } catch (KeeperException.NoNodeException e) {
    ZkUtils.createPersistNodeOnly(servicePath, zk);
    syncZkCookieRuleInfo(serviceInfo);
  } catch (KeeperException | InterruptedException e) {
    LOGGER.error(getClass() + "::syncZkCookieRuleInfo serviceName: " + serviceInfo.serviceName()
        + " 出现异常, zkStatus:" + zk.getState(), e);
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 获取 zookeeper 上的 限流规则 freqRule
 *
 * @return
 */
private void syncZkFreqControl(ZkServiceInfo serviceInfo) {
  if (!ZkUtils.isZkReady(zk)) return;
  try {
    Stat stat = new Stat();
    byte[] data = zk.getData(FREQ_PATH + "/" + serviceInfo.serviceName(), this, stat);
    serviceInfo.freqControl(ZkDataProcessor.processFreqRuleData(serviceInfo.serviceName(), data));
  } catch (KeeperException | InterruptedException e) {
    LOGGER.error(getClass() + "::syncZkFreqControl 获取freq 节点: " + serviceInfo.serviceName() + " 出现异常, zkStatus:" + zk.getState(), e);
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * process zk data 解析route 信息
 */
private void processRouteData(ZkServiceInfo serviceInfo, byte[] data) {
  try {
    String routeData = new String(data, StandardCharsets.UTF_8);
    List<Route> zkRoutes = RoutesExecutor.parseAll(routeData);
    serviceInfo.routes(zkRoutes);
  } catch (Exception e) {
    LOGGER.error(getClass() + "::processCookieRuleData, parser routes 信息 失败,请检查路由规则写法是否正确:" + e.getMessage());
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 同步zk信息
 *
 * @param serviceInfo
 */
private void startWatch(ZkServiceInfo serviceInfo) {
  LOGGER.info(getClass().getSimpleName() + "::syncServiceZkInfo[serviceName:" + serviceInfo.serviceName() + "], runtimeInstants:" + serviceInfo.runtimeInstances().size());
  try {
    // sync runtimeList
    syncZkRuntimeInfo(serviceInfo);
    // sync router config
    syncZkRouteInfo(serviceInfo);
    // sync service config, no need to try 5 times any more
    syncZkConfigInfo(serviceInfo, zk, this, true);
    syncZkConfigInfo(serviceInfo, zk, this, false);
    // sync cookie injection rule
    syncZkCookieRuleInfo(serviceInfo);
    LOGGER.info(getClass().getSimpleName() + "::syncServiceZkInfo[serviceName:" + serviceInfo.serviceName() + "]:zkInfo succeed, runtimeInstants:" + serviceInfo.runtimeInstances().size());
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
    LOGGER.error(getClass().getSimpleName() + "::syncServiceZkInfo[serviceName:" + serviceInfo.serviceName() + "]:zkInfo failed, runtimeInstants:" + serviceInfo.runtimeInstances().size());
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * route 根据给定路由规则对可运行实例进行过滤
 */
private void syncZkRouteInfo(ZkServiceInfo serviceInfo) {
  LOGGER.warn("ClientZKAgent::syncZkRouteInfo service:" + serviceInfo.serviceName());
  String servicePath = ROUTES_PATH + "/" + serviceInfo.serviceName();
  int retry = 5;
  do {
    if (zk == null || !zk.getState().isConnected()) {
      LOGGER.warn("ClientZKAgent::syncZkRouteInfo service:"
          + serviceInfo.serviceName() + ", zk status:"
          + (zk == null ? null : zk.getState()) + ", retry:"
          + retry + " times after 300ms");
      sleep(300);
    } else {
      try {
        byte[] data = zk.getData(servicePath, this, null);
        processRouteData(serviceInfo, data);
        LOGGER.warn("ClientZk::getRoutes routes changes:" + serviceInfo.routes());
        return;
      } catch (KeeperException.NoNodeException e) {
        ZkUtils.createPersistNodeOnly(servicePath, zk);
      } catch (KeeperException | InterruptedException e) {
        LOGGER.error(getClass() + "::syncZkRouteInfo serviceName: " + serviceInfo.serviceName() + " 出现异常, zkStatus:" + zk.getState(), e);
        sleep(300);
      }
    }
  } while (--retry > 0);
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 取消zk服务节点同步
 *
 * @param serviceInfo
 */
public void cancel(ZkServiceInfo serviceInfo) {
  LOGGER.info("ClientZkAgent::cancel, serviceName:" + serviceInfo.serviceName());
  synchronized (serviceInfoByName) {
    // 1
    ZkServiceInfo oldServiceInfo = serviceInfoByName.get(serviceInfo.serviceName());
    if (oldServiceInfo != null && serviceInfo == oldServiceInfo) {
      // 2, 步骤1跟2之间, serviceInfosByName可能会发生变化, 所以需要做同步
      serviceInfoByName.remove(serviceInfo.serviceName());
      LOGGER.info("ClientZkAgent::cancel succeed, serviceName:" + serviceInfo.serviceName());
    } else {
      LOGGER.warn("ClientZkAgent::cancel, no serviceInfo found for:" + serviceInfo.serviceName());
    }
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 获取zk 配置信息,封装到 ZkConfigInfo
 * 加入并发考虑
 *
 * @param serviceName 服务名(服务唯一)
 * @return ZkServiceInfo
 */
protected ZkServiceInfo getZkServiceInfo(String serviceName) {
  ZkServiceInfo info = serviceInfoByName.get(serviceName);
  if (info == null) {
    synchronized (serviceInfoByName) {
      info = serviceInfoByName.get(serviceName);
      if (info == null) {
        info = new ZkServiceInfo(serviceName, new CopyOnWriteArrayList<>());
        try {
          // when container is shutdown, zk is down and will throw execptions
          syncZkConfigInfo(info, zk, this, true);
          syncZkConfigInfo(info, zk, this, false);
          syncZkFreqControl(info);
          serviceInfoByName.put(serviceName, info);
        } catch (Throwable e) {
          LOGGER.error("ServerZk::getConfigData failed." + e.getMessage());
          info = null;
        }
      }
    }
  }
  return info;
}

代码示例来源:origin: com.github.dapeng-soa/dapeng-container-impl

final ZkServiceInfo serviceInfo = serverZkAgent.getZkServiceInfo(false, context.getHeader().getServiceName());
if (serviceInfo == null || serviceInfo.freqControl() == null) {
  return true;
final ServiceFreqControl freqControl = serviceInfo.freqControl();
String method = context.getHeader().getMethodName();
if (freqControl.globalRules.isEmpty() && !freqControl.rules4methods.containsKey(method)) {

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 将zk config 中的权重设置,同步到运行实例中
 *
 * @param zkInfo
 */
public static void recalculateRuntimeInstanceWeight(ZkServiceInfo zkInfo) {
  if (zkInfo != null) {
    List<RuntimeInstance> runtimeInstances = zkInfo.runtimeInstances();
    if (runtimeInstances != null && runtimeInstances.size() > 0) {
      for (RuntimeInstance runtimeInstance : runtimeInstances) {
        if (zkInfo.weightGlobalConfig.ip != null) {   //没有全局配置的情况下ip = null,有全局配置ip = ""
          runtimeInstance.weight = zkInfo.weightGlobalConfig.weight;
        }
        if (zkInfo.weightServiceConfigs != null) {
          List<Weight> weights = zkInfo.weightServiceConfigs;
          for (Weight weight : weights) {
            if (weight.ip.equals(runtimeInstance.ip)) {
              if (weight.port == runtimeInstance.port) {
                runtimeInstance.weight = weight.weight;
                break;
              } else if (weight.port == -1) {
                runtimeInstance.weight = weight.weight;
              }
            }
          }
        }
      }
    }
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
   * cookie injection
   */
  private static void injectCookie(String service, InvocationContextImpl context) throws SoaException {
    ZkServiceInfo zkServiceInfo = clientRefManager.serviceInfo(service);

    if (zkServiceInfo == null) {
      logger.error(InvocationContextUtils.class + "::injectCookie serviceInfo not found: " + service);
      throw new SoaException(SoaCode.NotFoundServer, "服务 [ " + service + " ] 无可用实例");
    }

    List<CookieRule> cookieRules = zkServiceInfo.cookieRules();

    if (cookieRules == null || cookieRules.size() == 0) {
      logger.debug("cookie rules 信息为空或size为0, 跳过 cookie injection");
    } else {
      CookieExecutor.injectCookies(context, cookieRules);
    }
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

@Override
public RuntimeInstance getRuntimeInstance(String service, String serviceIp, int servicePort) {
  return clientRefManager.serviceInfo(service).runtimeInstance(serviceIp, servicePort);
}

代码示例来源:origin: dapeng-soa/dapeng-soa

String servicePath = RUNTIME_PATH + "/" + serviceInfo.serviceName();
if (zk == null) {
  LOGGER.warn(getClass().getSimpleName() + "::syncZkRuntimeInfo[" + serviceInfo.serviceName() + "]:zkIsNull, now_init()");
  connect();
do {
  if (!zk.getState().isConnected()) {
    LOGGER.error(getClass().getSimpleName() + "::syncZkRuntimeInfo[" + serviceInfo.serviceName()
        + "]:zk doesn't connected yet, status:" + zk.getState() + ", retry:" + retry + " times after 300ms");
    sleep(300);
        serviceInfo.runtimeInstances().clear();
        LOGGER.info(getClass().getSimpleName() + "::syncZkRuntimeInfo["
            + serviceInfo.serviceName() + "]:no service instances found");
        return;
          + serviceInfo.serviceName() + "], 获取" + servicePath + "的子节点成功");
      List<RuntimeInstance> runtimeInstanceList = serviceInfo.runtimeInstances();
      runtimeInstanceList.clear();
      runtimeInstanceList.addAll(getRuntimeInstances(children, serviceInfo.serviceName()));
          + serviceInfo.serviceName() + " -> " + serviceInfo.runtimeInstances());
      return;
    } catch (KeeperException | InterruptedException e) {
      LOGGER.error(getClass() + "::syncZkRuntimeInfo serviceName: " + serviceInfo.serviceName() + " 出现异常, zkStatus:" + zk.getState(), e);
      sleep(300);

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 同步zk服务节点信息
 *
 * @param serviceInfo
 */
public void sync(ZkServiceInfo serviceInfo) {
  synchronized (serviceInfoByName) {
    serviceInfoByName.put(serviceInfo.serviceName(), serviceInfo);
  }
  startWatch(serviceInfo);
}

代码示例来源:origin: dapeng-soa/dapeng-soa

public SoaConnectionPool.ClientInfo registerClient(String serviceName, String version) {
  SoaConnectionPoolImpl.ClientInfoSoftRef softRef = handlesByName.get(serviceName);
  SoaConnectionPool.ClientInfo clientInfo;
  if (softRef != null) {
    clientInfo = softRef.get();
    if (clientInfo != null) {
      return clientInfo;
    }
  }
  // todo: one lock per service
  synchronized (this) {
    LOGGER.debug("ClientRefManager::registerClient, serviceName:" + serviceName);
    clientInfo = new SoaConnectionPool.ClientInfo(serviceName, version);
    ZkServiceInfo serviceInfo = new ZkServiceInfo(serviceName, new CopyOnWriteArrayList<>());
    clientZkAgent.sync(serviceInfo);
    SoaConnectionPoolImpl.ClientInfoSoftRef clientInfoSoftRef = new SoaConnectionPoolImpl.ClientInfoSoftRef(clientInfo, serviceInfo, referenceQueue);
    handlesByName.put(serviceName, clientInfoSoftRef);
  }
  return clientInfo;
}

代码示例来源:origin: dapeng-soa/dapeng-soa

final ZkServiceInfo serviceInfo = serverZkAgent.getZkServiceInfo(false, context.getHeader().getServiceName());
if (serviceInfo == null || serviceInfo.freqControl() == null) {
  return true;
final ServiceFreqControl freqControl = serviceInfo.freqControl();
String method = context.getHeader().getMethodName();
if (freqControl.globalRules.isEmpty() && !freqControl.rules4methods.containsKey(method)) {

代码示例来源:origin: dapeng-soa/dapeng-soa

RuntimeInstance runtimeInstance = clientRefManager.serviceInfo(service).runtimeInstance(host, port);
if (runtimeInstance == null) {
  LOGGER.error("SoaBaseConnection::runtimeInstance not found.");

代码示例来源:origin: dapeng-soa/dapeng-soa

List<RuntimeInstance> compatibles = serviceInfo.runtimeInstances();
if (compatibles == null || compatibles.isEmpty()) {
  return null;
  logger.error(getClass().getSimpleName() + "::findConnection[service: " + serviceInfo.serviceName() + ":" + version + "], not found available version of instances");
  throw new SoaException(NoMatchedService, "服务 [ " + serviceInfo.serviceName() + ":" + version + "] 无可用实例:没有找到对应的服务版本");
capsuleContext(context, serviceInfo.serviceName(), version, method);
  logger.error(getClass().getSimpleName() + "::findConnection[service: " + serviceInfo.serviceName() + "], not found available instances by routing rules");
  throw new SoaException(NoMatchedRouting, "服务 [ " + serviceInfo.serviceName() + " ] 无可用实例:路由规则没有解析到可运行的实例");
if (inst == null) {
  throw new SoaException(NotFoundServer, "服务 [ " + serviceInfo.serviceName() + " ] 无可用实例:负载均衡没有找到合适的运行实例");

代码示例来源:origin: dapeng-soa/dapeng-soa

public static void syncZkConfigInfo(ZkServiceInfo zkInfo, ZooKeeper zk, Watcher watcher, boolean isGlobal) {
  if (!isZkReady(zk)) return;
  String configPath = CONFIG_PATH;
  if (isGlobal) {
    configPath += "/" + zkInfo.serviceName();
  }
  try {
    byte[] data = zk.getData(configPath, watcher, null);
    ZkDataProcessor.processZkConfig(data, zkInfo, isGlobal);
  } catch (KeeperException | InterruptedException e) {
    LOGGER.error(ZkUtils.class + "::syncZkConfigInfo failed, service:"
        + zkInfo.serviceName() + ", zk status:" + zk.getState(), e);
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

/**
 * 服务路由
 *
 * @param serviceInfo
 * @param compatibles
 * @return
 */
private List<RuntimeInstance> router(ZkServiceInfo serviceInfo, List<RuntimeInstance> compatibles) throws SoaException {
  List<Route> routes = serviceInfo.routes();
  if (routes == null || routes.size() == 0) {
    logger.debug("router 获取 路由信息为空或size为0,跳过router,服务实例数:{}", compatibles.size());
    return compatibles;
  } else {
    InvocationContextImpl context = (InvocationContextImpl) InvocationContextImpl.Factory.currentInstance();
    List<RuntimeInstance> runtimeInstances = RoutesExecutor.executeRoutes(context, routes, compatibles);
    if (runtimeInstances.size() == 0) {
      throw new SoaException(SoaCode.NoMatchedRouting);
    }
    return runtimeInstances;
  }
}

代码示例来源:origin: dapeng-soa/dapeng-soa

RuntimeInstance runtimeInstance = clientRefManager.serviceInfo(service).runtimeInstance(host, port);
if (runtimeInstance == null) {
  LOGGER.error("SoaBaseConnection::runtimeInstance not found.");

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