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 🙏

© 2024 – Pkg Stats / Ryan Hefner

msdr-zk

v1.1.4

Published

"discover service or register service for micro-services environment using zookeeper"

Downloads

10

Readme

msdr-zk-nodejs

Build Status NPM

A Simple ZooKeeper based Microservices Service Discovery and Registery library for node.js services, that automatically re-register the watch again on the path when data is requested for the first time.

Important Point

This library doesn't create the basePath at the ZooKeeper tree for services, assuming each service needs to be provisioned as the requirement comes and to avoid the services getting registered at the incorrect basePath at the Zookeeper end in the system.

Installation

npm install --save msdr-zk

Usage.

const msdzkP = msdzk.connect({
  connectionURL: "localhost:2181",
  basePath: "/services/endpoints/test"
});

let msdzkConnection;
msdzkP.then(conn => {
  msdzkConnection = conn;
  msdzkConnection.on("zk-disconnected", () => {
    // listen for zk-disconnected event, and do the clean up.
    console.log("zk-disconnected stopping the process");
    process.exit(1);
  });

  // watch for event's
  msdzkConnection.on("NODE_CHILDREN_CHANGED", e => {
    console.log("Node Children changed on the path");
    console.log(e);
  });
});

const registerServiceParam = {
  name: "service-name",
  port: "4000",
  protocol: "http",
  api: "/api/v1",
  ip: "localhost",
  release: "1.1.0",
  metadata: {
    check: {
      status: "/status",
      health: "health",
      interval: "30s",
      user: "node",
      tags: ["api"]
    }
  }
};

// register service
msdzk.registerService(registerServiceParam).then(_p => {
  console.log("service registered at " + _p);
});

//allows service to register and then check
setTimeout(() => {
  msdzk.getServiceEndpoints("service-name").then(arr => {
    console.log(arr);
    msdzk.getRandomServiceEndPoint(arr, "service-name").then(endpoint => {
      console.log(endpoint);
    });
  });
}, 2000);

More Examples in examples folder.

Api.

1. connect(connectionOptions) ⇒ Promise

connect Initiate the connection to the provided server list (ensemble). Examples:

connect({
  connectionURL: "localhost:2181",
  basePath: "/services/endpoints/test"
});

Kind: global function
Returns: Promise - that resolves to an event emitter instance Event.

| Param | Type | Description | | ------------------------------- | ------------------- | --------------------------------------------------------------------------- | | connectionOptions | object | | | connectionOptions.connectionURL | string | server list (ensemble) as string, Comma separated host:port pairs. | | connectionOptions.basePath | string | the base path in the ZooKeeper tree for services of the system or platform. |

All the services will get connected to the basePath and store their endpoints as ephemeral nodes, at the service-name node because ephemeral nodes are only active as long as the session that created it is active. So if the services goes down the ephemeral node that it created also comes to an end.

It's advisable to create the basePath as persistent znodes as Persistent znodes are useful for storing data that needs to be highly available and accessible and have a lifetime in the ZooKeeper's namespace until they're explicitly deleted.

2. registerService(serviceOptions) ⇒ Promise

registerService register the service to the zookeeper node using the passed options. Examples:

registerService({
  name: "service-name",
  port: "4000",
  protocol: "http",
  api: "/api/v1",
  ip: "localhost",
  release: "1.1.0",
  metadata: {
    check: {
      status: "/status",
      health: "health",
      interval: "30s",
      user: "node",
      tags: ["api"]
    }
  }
});

Kind: global function
Returns: Promise - - That resolves to complete service path if the service gets registered or reject with error.

| Param | Type | Description | | ----------------------- | ------------------- | -------------------------------------------------------------------- | | serviceOptions | object | | | serviceOptions.name | string | name of the service to register as ephemeral znode at basePath+name. | | serviceOptions.port | string | port number configuration of the microservice to save. | | serviceOptions.protocol | string | communication protocol [https | | http]. | | serviceOptions.api | string | base service api. | | serviceOptions.ip | string | ip address of the running service. | | serviceOptions.metadata | object | other metadata of the services line - healthcheck, tags, etc. |

3. getServiceEndpoints(serviceName) ⇒ Promise

getServiceEndpoints returns all endpoint from the list of registered endpoints, given a service-name.

Kind: global function
Returns: Promise - - that resloves to list of all endpoints

| Param | Type | Description | | ----------- | ------------------- | ------------ | | serviceName | string | service name |

4. getRandomServiceEndPoint(endPointsList, serviceName) ⇒ Promise

getRandomServiceEndPoint returns a random endpoint from the list of registered endpoints.

Kind: global function
Returns: Promise - - That resolves to a randomnly selected endpoint.

| Param | Type | Description | | ------------- | ------------------- | ------------------ | | endPointsList | array | list of endpoints. | | serviceName | string | service name. |

5. getService(endPoint) ⇒ Promise

getService return a object containing endpoint and metadata from the registered endpoint.

Kind: global function
Returns: Promise - - That resolves to a selected endpoint and metadata.

| Param | Type | Description | | -------- | ------------------- | ------------------------- | | endPoint | string | Full Path of the endPoint |

6. getAllChildren() ⇒ Promise

getAllChildren returns all service registered at the basePath.

Kind: global function
Returns: Promise - - that resloves to list of all services.

7. getServiceConfigData(serviceConfigPath) ⇒ Promise

getServiceConfigData returns the configuration data for the particular service.

Kind: global function
Returns: Promise - - that resolves with the config data

| Param | Type | Default | Description | | ----------------- | ------------------- | ----------------- | ---------------------------- | | serviceConfigPath | string | null | config store path of service |

8. setServiceConfigData(serviceConfigPath) ⇒ Promise

setServiceConfigData sets the configuration data on the particular path.

Kind: global function
Returns: Promise - - that resolves with stat of the node.

| Param | Type | Default | Description | | ----------------- | ------------------- | ----------------- | ---------------------------- | | serviceConfigPath | string | null | config store path of service |


Event

The watcher function triggers the follwoing event on the instance of event emitter resolved with connect function.

Properties

  • NODE_DELETED - watched node is deleted.
  • NODE_DATA_CHANGED - Data of watched node is changed.
  • NODE_CHILDREN_CHANGED - Children of watched node is changed.

License

MIT <3