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

node-service-discovery

v1.0.2

Published

Service Discovery framework built on top of Zookeeper

Downloads

568

Readme

Build Status

Service Discovery module for nodejs built on Zookeeper

This is an extension to the nicely built Zoologist module. This also enables dynamic service registration and de-registration with ZK. It has support for watchers and periodic polling on Zookeeper to get the latest node data. Has support for healthchecks so you can safely take the nodes Out of Rotation(OOR) and bring Back In Rotation(BIR) and it will be removed from clients list.

##Getting started

  1. Start the client. Client emits 'connected' event. You can listen to this if required
  2. Create a serviceInstance which will be your current node. Add healthChecks to it to enable OOR, BIR support
  3. Create a serviceDiscovery instance and register your serviceInstance on ZK. Also define the refresh interval for polling dependent services from ZK.
  4. Create instance providers to get the dependent services from ZK. It will automatically add watcher to it.
  5. Get instance using instance providers.

Note:

  1. Don't save a node. The getInstance query is extremely fast and in-memory. It doesn't make any remote calls

####Data Model Following data is stored on ZK path (/basePath/serviceName/HOST:PORT)

{
	"host": "localhost",
	"port": 31299,
	"nodeData": {
		"environment": "stage"
	},
	"healthcheckStatus": "healthy",
	"lastUpdatedTimeStamp": 1478246554601
}

####Health Check (Optional) Define a healthCheck. It should contain isHealthy() method. You can add an array of healthChecks if required

var healthCheck = function () {
                return {
                    isHealthy: function () {
                        return true; //add logic here for returning healthy/unhealthy status
                    }
                }};

Installation

npm install node-service-discovery --save

Examples

Service Discovery

'use strict';

var Ranger                  = require('node-service-discovery').Ranger;
var ServiceInstanceBuilder  = require("node-service-discovery").ServiceInstanceBuilder;
var ServiceDiscoveryBuilder = require('node-service-discovery').ServiceDiscoveryBuilder;

//Create a Ranger Client
var rangerClient = Ranger.newClient(process.env.ZK_CONNECTION_STRING || '127.0.0.1:2181');

/** start emits 'connected' event. You can initialize following things after the event
 *  Eg: rangerClient.once('connected') { //initialize following code }
 */
rangerClient.start();


/**
 * Create the Service Discovery Instance
 *
 * refreshInterval is the interval to check for the updation of nodes in ZK.
 */
var serviceDiscovery = ServiceDiscoveryBuilder
        .builder()
        .client(rangerClient)
        .serviceInstance(serviceInstance)
        .basePath('services')
        .refreshInterval(5000)
        .build();

// Create the instance provider (selectionStrategy: 'RoundRobin' or 'Random')
var instanceProvider = serviceDiscovery
        .instanceProviderBuilder()
        .serviceName('dependent-service')
        .selectionStrategy('RoundRobin')
        .build()

//init the provider, it adds watcher, adds periodic polling and gets instance data for first time
instanceProvider.init(function(error) {
    //check for error here
});

// Get an instance. The returned value will have 'data' model specified above
instanceProvider.getInstance();

Service Registration


'use strict';

var Ranger                  = require('node-service-discovery').Ranger;
var ServiceInstanceBuilder  = require("node-service-discovery").ServiceInstanceBuilder;
var ServiceDiscoveryBuilder = require('node-service-discovery').ServiceDiscoveryBuilder;

//Create a Ranger Client
var rangerClient = Ranger.newClient(process.env.ZK_CONNECTION_STRING || '127.0.0.1:2181');

/** start emits 'connected' event. You can initialize following things after the event
 *  Eg: rangerClient.once('connected') { //initialize following code }
 */
rangerClient.start();

/**
 * Register a Service
 *
 * process.env.environment is whatever is the environment you are using
 * Additionally, you can also bind the health-checks with the service instance (healthcheck is optional)
 */
var serviceInstance = ServiceInstanceBuilder
        .builder()
        .host("localhost")
        .port(8080) //integer
        .environment(process.env.environment || 'development')
        .healthChecks([healthCheck])
        .name('my-service')
        .build();
 
serviceDiscovery.registerService(function(error) {
    //check for error
});

Health Check change propagation


'use strict';

/* Register service using above example */

/** Emit following event on serviceDiscovery instance when health of you app changes on OOR/BIR
 * This will update the correct status for serviceInstance in Zookeeper
 * isHealthy() method mentioned above should return corresponding value after health status change
 */
 
serviceDiscovery.emit('healthCheckChange');