com.mpush.zk.ZKClient类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(119)

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

ZKClient介绍

暂无

代码示例

代码示例来源:origin: mpusher/mpush

@Test
  public void testZK() throws Exception {
    ZKClient.I.syncStart();
    ZKClient.I.registerEphemeral(ServerNodes.gs().serviceName(), "3");
    ZKClient.I.registerEphemeral(ServerNodes.gs().serviceName(), "4");
    System.err.println("==================" + ZKClient.I.getChildrenKeys(ServiceNames.GATEWAY_SERVER));
    List<String> rawData = ZKClient.I.getChildrenKeys(ServiceNames.GATEWAY_SERVER);
    if (rawData == null || rawData.isEmpty()) {
      return;
    }
    for (String raw : rawData) {
      String fullPath = ServiceNames.GATEWAY_SERVER + PATH_SEPARATOR + raw;
      System.err.println("==================" + ZKClient.I.get(fullPath));
    }

  }
}

代码示例来源:origin: mpusher/mpush

private synchronized static ZKClient I() {
  return I == null ? new ZKClient() : I;
}

代码示例来源:origin: mpusher/mpush

@Override
protected void doStart(Listener listener) throws Throwable {
  client.start();
  Logs.RSD.info("init zk client waiting for connected...");
  if (!client.blockUntilConnected(1, TimeUnit.MINUTES)) {
    throw new ZKException("init zk error, config=" + zkConfig);
  }
  initLocalCache(zkConfig.getWatchPath());
  addConnectionStateListener();
  Logs.RSD.info("zk client start success, server lists is:{}", zkConfig.getHosts());
  listener.onSuccess(zkConfig.getHosts());
}

代码示例来源:origin: mpusher/mpush

@Override
public void deregister(ServiceNode node) {
  if (client.isRunning()) {
    client.remove(node.nodePath());
  }
}

代码示例来源:origin: mpusher/mpush

/**
 * 持久化数据
 *
 * @param key
 * @param value
 */
public void registerPersist(final String key, final String value) {
  try {
    if (isExisted(key)) {
      update(key, value);
    } else {
      client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes());
    }
  } catch (Exception ex) {
    Logs.RSD.error("persist:{},{}", key, value, ex);
    throw new ZKException(ex);
  }
}

代码示例来源:origin: mpusher/mpush

@Override
public void register(ServiceNode node) {
  if (node.isPersistent()) {
    client.registerPersist(node.nodePath(), Jsons.toJson(node));
  } else {
    client.registerEphemeral(node.nodePath(), Jsons.toJson(node));
  }
}

代码示例来源:origin: mpusher/mpush

/**
 * 注册临时数据
 *
 * @param key
 * @param value
 */
public void reRegisterEphemeral(final String key, final String value) {
  registerEphemeral(key, value, false);
}

代码示例来源:origin: mpusher/mpush

@Override
public List<ServiceNode> lookup(String serviceName) {
  List<String> childrenKeys = client.getChildrenKeys(serviceName);
  if (childrenKeys == null || childrenKeys.isEmpty()) {
    return Collections.emptyList();
  }
  return childrenKeys.stream()
      .map(key -> serviceName + PATH_SEPARATOR + key)
      .map(client::get)
      .filter(Objects::nonNull)
      .map(childData -> Jsons.fromJson(childData, CommonServiceNode.class))
      .filter(Objects::nonNull)
      .collect(Collectors.toList());
}

代码示例来源:origin: mpusher/mpush

/**
 * 从远程获取数据
 *
 * @param key
 * @return
 */
public String getFromRemote(final String key) {
  if (isExisted(key)) {
    try {
      return new String(client.getData().forPath(key), Constants.UTF_8);
    } catch (Exception ex) {
      Logs.RSD.error("getFromRemote:{}", key, ex);
    }
  }
  return null;
}

代码示例来源:origin: mpusher/mpush

@Override
public void stop(Listener listener) {
  if (isRunning()) {
    super.stop(listener);
  } else {
    listener.onSuccess();
  }
}

代码示例来源:origin: mpusher/mpush

public void registerEphemeralSequential(final String key, final String value) {
  registerEphemeralSequential(key, value, true);
}

代码示例来源:origin: mpusher/mpush

/**
 * 获取数据,先从本地获取,本地找不到,从远程获取
 *
 * @param key
 * @return
 */
public String get(final String key) {
  if (null == cache) {
    return null;
  }
  ChildData data = cache.getCurrentData(key);
  if (null != data) {
    return null == data.getData() ? null : new String(data.getData(), Constants.UTF_8);
  }
  return getFromRemote(key);
}

代码示例来源:origin: com.github.mpusher/mpush-zk

@Override
public void register(ServiceNode node) {
  if (node.isPersistent()) {
    client.registerPersist(node.nodePath(), Jsons.toJson(node));
  } else {
    client.registerEphemeral(node.nodePath(), Jsons.toJson(node));
  }
}

代码示例来源:origin: mpusher/mpush

/**
 * 注册临时数据
 *
 * @param key
 * @param value
 */
public void registerEphemeral(final String key, final String value) {
  registerEphemeral(key, value, true);
}

代码示例来源:origin: com.github.mpusher/mpush-zk

@Override
public void deregister(ServiceNode node) {
  if (client.isRunning()) {
    client.remove(node.nodePath());
  }
}

代码示例来源:origin: com.github.mpusher/mpush-zk

@Override
public List<ServiceNode> lookup(String serviceName) {
  List<String> childrenKeys = client.getChildrenKeys(serviceName);
  if (childrenKeys == null || childrenKeys.isEmpty()) {
    return Collections.emptyList();
  }
  return childrenKeys.stream()
      .map(key -> serviceName + PATH_SEPARATOR + key)
      .map(client::get)
      .filter(Objects::nonNull)
      .map(childData -> Jsons.fromJson(childData, CommonServiceNode.class))
      .filter(Objects::nonNull)
      .collect(Collectors.toList());
}

代码示例来源:origin: com.github.mpusher/mpush-zk

/**
 * 持久化数据
 *
 * @param key
 * @param value
 */
public void registerPersist(final String key, final String value) {
  try {
    if (isExisted(key)) {
      update(key, value);
    } else {
      client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(key, value.getBytes());
    }
  } catch (Exception ex) {
    Logs.RSD.error("persist:{},{}", key, value, ex);
    throw new ZKException(ex);
  }
}

代码示例来源:origin: mpusher/mpush

/**
 * 获取子节点
 *
 * @param key
 * @return
 */
public List<String> getChildrenKeys(final String key) {
  try {
    if (!isExisted(key)) return Collections.emptyList();
    List<String> result = client.getChildren().forPath(key);
    result.sort(Comparator.reverseOrder());
    return result;
  } catch (Exception ex) {
    Logs.RSD.error("getChildrenKeys:{}", key, ex);
    return Collections.emptyList();
  }
}

代码示例来源:origin: mpusher/mpush

@Override
public void start(Listener listener) {
  if (isRunning()) {
    listener.onSuccess();
  } else {
    super.start(listener);
  }
}

代码示例来源:origin: mpusher/mpush

private void reRegisterEphemeralSequential(final String key, final String value) {
  registerEphemeralSequential(key, value, false);
}

相关文章