Disconsulate
A light-weight loadbalancing service discovery lib for Consul
Disconsulate is an opinionated wrapper for Consul's service discovery API. It's heavily inspired by Consulite but has a few more tricks up its sleeve.
Disconsulate is built with 100% test coverage, minimal dependencies, and a tiny API.
Installation
npm install disconsulate
Usage
// Disconsulate uses async functions when talking to Consul. { // Create a client by passing it the address of a consul server. // If you don't provide a server address, Disconsulate will use // environment variables. const client = "http://consul.local:8500"; // Disconsulate will raise events when a watched service changes, you can // use this to tear down connections, or reload config. client; // getService returns a promise of a registered service. // Disconsulate watches services in the background to automatically update // its cache. try let database = await client; // this second call to getService will be served from cache. // if there are multiple addresses available for the database service // Disconsulate will return each of them in turn. database = await client; console; catch e // If we can't find any services, we'll raise an error at this point. console; }
Filtering results
In some scenarios we might need to filter services by tag, query services in a separate datacentre, or search for services that are hosted on a particular class of node.
Disconsulate's getService
method is able to watch multiple configurations for the same service. In the following example, we set up three separate watches which can update individually.
const client = ; const live = client; const dev = client; const europe = client;
Retrying & Error Handling
When you first request a service, Disconsulate will have nothing in its cache, and will fetch the latest data from Consul. A failure at this point will return an error to the client.
{ const client = ; try const db = await client; catch e console; }
Disconsulate will then keep its cache up to date using Consul's blocking queries. These queries happen in the background, automatically. If a refresh fails, Disconsulate will raise an event.
{ const client = ; client; return client;}
By default, Disconsulate will try 20 times to refresh the service before giving up. You can set the retry policy when creating a client. If Disconsulate reaches the maximum number of retries, it'll raise the "fail" event and stop retrying.
{ const client = consulAddr retry: maxTries: 3 ; client; await client;}
Logging
Disconsulate ships with a stub logger that logs error details to the console. You can pass your own logger to the client. A logger is any object that has the following methods:
- debug (str)
- info (str)
- error (str)
- fatal (str)
;const logger = winston; { const client = logger ;}
API
new Disconsulate(options)
Create a new instance of the Consulite class.
options
- configuration options for managing the connection to Consul:consul
- consul host to connect to. Defaults to either:http://${process.env.CONSUL_ADDR}
orhttp://${process.env.CONSUL_HOST}:${process.env.CONSUL_PORT}
orhttp://consul:8500
- as a last resort
retry
: an object describing the retry policy, comprising:seedWait
: The minimum time to wait before retrying a failed request (default: 100 ms)maxWait
: The maximum delay to wait between retries (default 30,000 millisecs)maxTries
: The maximum number of times to retry a failed request (default: 20)
logger
: an object exposing debug, info, error, and fatal methods.
getService(options)
Start watching a service, and return a registered address and port.
options
: an object with the following propertiesservice
: the service name as registered with Consul.dc
: the datacentre to search for registered services.tags
: an array of that must be registered on an instance, used for filtering the registered addresses.node
: an object of key/value pairs that will be used to filter by node metadata.
Returns a promise of a service instance { address: string, port: string, tags: [string] }