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:
- port - consul port to connect default 8500 (default 8500)
- host - consul host to connect default 8500 (default localhost)
- baseUrl - some default path to load configs from, will prefix the path to each get request (default '')
- validateOptions - object with defaults { fail: true, timeout: 5000, retries: 6, factor: 2, onRetry: null }
can override each one of the defaults.
- defaultWatchOptions - object with defaults { backoffFactor: 100, backoffMax: 30000, maxAttempts: 10000 }
can override each one of the defaults.
- 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')
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"}})
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)
},
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'] });
const values = await consul.getAll()
watchAll(({ key, value, changedValue }) => {
})