@logzio-node-toolbox/consul
TypeScript icon, indicating that this package has built-in type declarations

0.0.32 • Public • Published

consul

easy to use warper for consul to register to service discovery and get, merge and watch keys

Usage

import { Consul } from '@logzio-node-toolbox/consul';

const consul = new Consul({ port: 18500, host: '127.0.0.1', baseUrl = 'some_base_url' });

initialized params:

  1. port - consul port to connect default 8500 (default 8500)
  2. host - consul host to connect default 8500 (default localhost)
  3. baseUrl - some default path to load configs from, will prefix the path to each get request (default '')
  4. validateOptions - object with defaults { fail: true, timeout: 5000, retries: 6, factor: 2, onRetry: null } can override each one of the defaults.
  5. defaultWatchOptions - object with defaults { backoffFactor: 100, backoffMax: 30000, maxAttempts: 10000 } can override each one of the defaults.
  6. defaultRegisterRetryOptions - object with defaults { factor: 2, retries: 6, onRetry: null } can override each one of the defaults.

methods

validateConnected - make use connected to consul service (will retry (connectMaxRetries) of times);

receive same params as validateOptions and will merge with th one passed to the initializer

  await consul.validateConnected(validateOptions)

get - will get a key from consul service, (if initlized with base path it will prefix it)

  const { key, value } = await consul.get('somepath/config.json')
  // if have base path will fetch 'some_base_url/somepath/config.json'

set - will set a value to consul, (if initlized with basebath it will prefix it)

   await consul.set({key: 'somepath/config.json', value: {some: "value"}})
  // if have base path will set to 'some_base_url/somepath/config.json'

keys - list of keys in path

   await consul.keys('somepath/config.json')

merge - deepmerge current values with new values

it will fetch current values will deep merge all keys and set it

   await consul.merge({key: 'somepath/config.json', value: {toOverride: "newValue" }})

watch - listen to key change and invoke handler

options - same watchOptions object as the initializer ( will merge them together)

   await consul.watch({
   key: 'somepath/config.json',
   onChange: ({key, value}) => {
       console.log(key)
       console.log(value) // new values
   },
   onError:(err) => {
    console.log(err)
   },
   options
   })

register - will register service to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   options
   })

registerInterval - will create interval to validate service always register to consul

options - same as registerRetryOptions object as the initializer ( will merge them together)

    interface RegisterData {
      meta?: AnyObject;
      checks?: AnyObject;
      address?: string;
      id?: string;
      name?: string;
      port?: number;
    }

   await consul.register({
   data,
   interval: 3000,
   onError:(err) => {
    console.log(err)
   },
   options
   })

close - deregister and close all watchers

options - same as registerRetryOptions object as the initializer ( will merge them together)

   await consul.close(options)

multiConsul

extend consul to work on multiple keys and merged config

import { MultiConsul } from '@logzio-node-toolbox/consul';
const consul = new MultiConsul({ port: 18500, paths:['config1', 'config2', 'config3'] });
/*
config1 in consul:
{
  "key1": "value1"
  key2": "value1"
  key3": "value1"
}

config2 in consul:
{
  key2": "value2"
}
 // no config 3
*/

const values = await consul.getAll() // {key1: value1, key2: value2, key3: value1};

watchAll(({ key, value, changedValue }) => {
  /* settings new consul file config3
    { key: value3 }
    will invoke watch with
    key: config3
    changedValue: { key: value3 }
    values: {key1: value1, key2: value2, key3: value3};
  */
})

Readme

Keywords

none

Package Sidebar

Install

npm i @logzio-node-toolbox/consul

Weekly Downloads

1,017

Version

0.0.32

License

MIT

Unpacked Size

126 kB

Total Files

6

Last publish

Collaborators

  • dimad
  • ifatregev
  • miletsky
  • mad_vinking
  • alonmiz
  • nofar9792