npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

zk-curator

v1.0.10

Published

nodejs连接zookeeper(curator风格)

Readme

zk-curator

zookeeper客户端 nodejs版本,和curator的api风格一致。 主要对node-zookeeper-client进行了二次封装。

对比node-zookeeper-client的新功能

  • 基于Promise调用
  • Fluent风格的API接口
  • 递归创建、删除节点
  • 命名空间
  • 实时获取某个节点及该节点下所有子节点的信息

Install

$ npm install zk-curator

连接zookeeper

const { CuratorFrameworkFactory }  = require('zk-curator');

// promise形式连接
const connectAsync = async function () {
    let client;
    client = await CuratorFrameworkFactory.builder()
    .connectString(process.env.ZK_URL)
    .namespace('http')
    .setOptions({
        sessionTimeout: 1000
    })
    .build()
    .start();

    console.log("connectAsync连接成功");
    client.close();
};
// callback形式连接
const connectCallback = function () {
    let client;
    client = CuratorFrameworkFactory.builder()
    .connectString(process.env.ZK_URL)
    .namespace('http')
    .setOptions({
        sessionTimeout: 1000
    })
    .build(function () {
        console.log('connectCallback连接成功');
        client.close();

    });

    // 打开连接
    client.start();
};
connectAsync();
connectCallback();

Example

Documentation

连接

构造CuratorFrameworkFactory
const { CuratorFrameworkFactory } = require('zk-curator');
const curatorFrameworkFactory = CuratorFrameworkFactory.builder()
connectString 设置连接地址

url:zookeeper连接地址 多个以逗号隔开(ip:port;ip:port)

curatorFrameworkFactor.connectString(url);
namespace

设置当前连接的namespace (命名空间) str:如http/develop 不能以/开头

curatorFrameworkFactor.namespace(str);
setLogger 设置日志

logger: 推荐使用log42-node获取指定日志类 需要包括info等标准方法 curatorFrameworkFactor.setLogger(logger)

const log4j2 = require('log42-node');
const log4j2Config = require('../config/log4j2.json');
log4j2.configure(log4j2Config);
let client;
client = CuratorFrameworkFactory.builder()
    .connectString(process.env.ZK_URL)
    .namespace(`http`)
    .setLogger(log4j2.getLogger('zookeeper'))
    .build(main);
client.start();

async function main() {
	console.log('连接成功')
}

client

Client start()

启动连接到所提供的服务器列表或集合。客户端将从列表中选择任意的服务器,并尝试连接到该列表。如果连接的建立失败,将尝试另一个服务器(随机选择),直到建立连接或关闭方法。

void close()

关闭这个客户端。一旦客户端关闭,它的会话就会失效。与会话关联的zookeeper服务器上的所有临时节点将被删除。

create()

checkExists()

getChildren()

delete()

cache

cache为永久监听器,包括NodeCache、PathCache、TreeCache三种。node对象数据格式:

  • path : 节点的完整路径。
  • id : 节点的id。
  • state : 节点的状态。
  • data : 节点的数据。
  • childrenData : 子节点的对象,包括迭代子节点的数据。对象的key为子节点的id,value为子节点的node对象。
  • children : 子节点id的数组。
  • tag : 标签 (通过setTag设置)。

addListener

增加监听的方法,格式为{eventName:function(cache,deep, changeNode),...}。 eventName包括:

  • childAdd:子节点添加时调用, 这里的changeNode为添加后的子节点的node对象。
  • childRemove:子节点删除时调用, 这里的changeNode为删除前的子节点的node对象。
  • nodeCreate:节点创建时候调用, 这里的changeNode为创建后的节点的node对象。
  • nodeRemove:节点删除时候调用, 这里的changeNode为删除前的节点的node对象。
  • nodeDataChange:节点数据改变时候调用, 这里的changeNode为修改前的节点的node对象。

setTag

设置标签:setTag(path, tag); path: string 要设置节点的完整路径,可通过node.path获取。 tag: number 要设置节点tag。 @return: bool 如果path找不到或者tag不为number,则返回false,设置成功返回true

getTag

设置标签:getTag(path); path: string 要获取节点的完整路径,可通过node.path获取。 @return: node 如果path找不到则返回null,能找到则返回对应tag

NodeCache

监视一个结点的创建、更新、删除,并将结点的数据缓存在本地。NodeCache(Client,path):path节点的路径

const {NodeCache} = require('zk-curator');
const nodeCache = new NodeCache(client,'/test/create');
nodeCache.addListener({
   nodeCreate: function (cache, deep, changeNode) {
       console.log('nodeCreate', deep, changeNode);
       console.log(cache.getData());
   },
   nodeRemove: function (cache, deep, changeNode) {
       console.log('nodeRemove', deep, changeNode);
       console.log(cache.getData());

   },
   nodeDataChange: function (cache, deep, changeNode) {
       console.log('nodeDataChange', deep, changeNode);
       console.log(cache.getData())
   }
});
nodeCache.start();

PathCache

监视一个结点的创建、更新、删除,子节点的创建更新,并将结点的数据缓存在本地。PathCache(Client,path):path节点的路径

const {PathCache} = require('zk-curator');
const pathCache = new PathCache(client,'/test');
pathCache.addListener({
    childAdd: function (cache, deep, changeNode) {
        console.log('childAdd', deep, changeNode);
        console.log(cache.getData())
    },
    childRemove: function (cache, deep, changeNode) {
        console.log('childRemove', deep, changeNode);
        console.log(cache.getData())

    },
    nodeCreate: function (cache, deep, changeNode) {
        console.log('nodeCreate', deep, changeNode)
        console.log(cache.getData())
    },
    nodeRemove: function (cache, deep, changeNode) {
        console.log('nodeRemove', deep, changeNode);
        console.log(cache.getData())

    },
    nodeDataChange: function (cache, deep, changeNode) {
        console.log('nodeDataChange', deep, changeNode);
        console.log(cache.getData());
    }
});
pathCache.start();

TreeCache

监视一个结点的创建、更新、删除,及子节点的创建、更新、删除, 从数并将结点的数据缓存在本地。TreeCache(Client, path, maxDeep):path节点的路径 maxDeep最大深度。、 如:path=/test,那么/test的deep=0; /test/a/b/c为3。当maxDeep=2时候,最大可监听/test/a/b的子节点的变化,也就是当创建/test/a/b/c时候, 会触发/test/a/b的childAdd事件,并且不能监听/test/a/b/c节点的变化。

const {TreeCache} = require('zk-curator');
const treeCache = new TreeCache(client,'/test', 2);
treeCache.addListener({
    childAdd: function (cache, deep, changeNode) {
        console.log('childAdd', deep, changeNode);
        console.log(cache.getData())
    },
    childRemove: function (cache, deep, changeNode) {
        console.log('childRemove', deep, changeNode);
        console.log(cache.getData())

    },
    nodeCreate: function (cache, deep, changeNode) {
        console.log('nodeCreate', deep, changeNode)
        console.log(cache.getData())
    },
    nodeRemove: function (cache, deep, changeNode) {
        console.log('nodeRemove', deep, changeNode);
        console.log(cache.getData())
    },
    nodeDataChange: function (cache, deep, changeNode) {
        console.log('nodeDataChange', deep, changeNode);
        console.log(cache.getData())
    }
});
treeCache.start();