Giter Club home page Giter Club logo

node-consulite's Introduction

consulite

Tiny consul Node.js module for client discovery with round-robin support

Npm Version Node Version Build Status

API

new Consulite(options)

Create a new instance of the Consulite class. options can include the following properties:

  • consul - consul host to connect to. Defaults to either:
    • ${process.env.CONSUL_HOST}:${process.env.CONSUL_PORT}
    • consul:8500 - as a last resort

getServiceNames([, callback])

Get all service names from consul, regardless of health status.

  • callback: function with the signature (err, services) where services is an array of service names.

This function returns a Promise if no callback is provided.

getServiceStatus(name, [, callback])

Get all nodes for the service and include their health status.

  • callback: function with the signature (err, nodes) where nodes is an array of node data, which is formatted with the properties shown in the example below.
[
  {
    node: 'foobar',
    address: '10.1.10.12',
    port: 8000,
    status: 'passing'
  }
]

This function returns a Promise if no callback is provided.

getService(name [, callback])

Get service address information from cache or from consul. When multiple service instances are registered with consul the first instance that hasn't been executed or the oldest executed service is returned.

  • name: the service name registered with consul. If no services are found then an error will be returned to the callback. If multiple services are found then the service that hasn't been executed or hasn't been executed most recently will be returned in the callback.

  • callback: function with the signature (err, service) where service has the following properties:

    • address: the host address where the service is located
    • port: the port that the service is exposed on

This function returns a Promise if no callback is provided.

getServiceHosts(name [, callback])

Get an array of all service instances from cache or from consul.

  • name: the service name registered with consul. If no services are found then an error will be returned to the callback.

  • callback: function with the signature (err, hosts) where hosts is an array of objects with the following properties:

    • address: the host address where the service is located
    • port: the port that the service is exposed on

This function returns a Promise if no callback is provided.

getCachedService(name)

Get the next service from the cache if it exists, otherwise return null;

getCachedServiceHosts(name)

Get an array of all service instances from the cache if it exists, otherwise null.

refreshService(name [, callback])

Makes a request to consul for the given service name and caches the results. Only services that are healthy are cached.

  • name: the service name to fetch from consul.
  • callback: function with signature (err, services)

This function returns a Promise if no callback is provided.

Example Usage

const Consulite = require('consulite');
const Wreck = require('wreck');

const consulite = new Consulite();
consulite.getService('users', (err, service) {
  if (err) {
    console.error(err);
    return;
  }

  Wreck.get(`http://${service.address}:${service.port}/users`, (err, res, payload) => {
    // handle error and do something with results
  });
});

If you want to set the consul address to use and don't want to depend on environment variables you can use the config() function as demonstrated below:

const Consulite = require('consulite');
const Wreck = require('wreck');

const consulite = new Consulite({ consul: 'http://myconsul.com' });

consulite.getService('users', (err, service) {
  if (err) {
    console.error(err);
    return;
  }

  Wreck.get(`http://${service.address}:${service.port}/users`, (err, res, payload) => {
    // handle error and do something with results
  });
});

node-consulite's People

Contributors

geek avatar teutat3s avatar jasonpincin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.