consul-service-registrator
v2.6.0
Published
Set of classes for service registration in Consul
Downloads
42
Readme
Consul Service Registrator
Please, check the full documentation below.
Table of Contents
Installation and Usage
Using npm:
$ npm install --save consul-service-registratorUsing yarn:
$ yarn add consul-service-registratorUsage
Lib provides 3 classes:
IpAddressDetectorServiceObserverRegistrator
IpAddressDetector
Class has a method getLanAndWanFromConsul that return Promise.
Method fetches from consul a response of /v1/agent/self and
- if both
Config.AdvertiseAddrandConfig.AdvertiseAddrWanare present returns them back asresult.lanIpandresult.wanIpcorrespondingly; - if both
DebugConfig.AdvertiseAddrLANandDebugConfig.AdvertiseAddrWANare present returns them back asresult.lanIpandresult.wanIpcorrespondingly.
Note.
Configshows the explicitly defined configuration.DebugConfigshows how Consul interpreted and applied the configuration in practice. TaggedAddresses show explicitly set values that are delivered through gossip
const {IpAddressDetector} = require('consul-service-registrator');
const consulConfig = {
host: '127.0.0.1',
port: 8500
};
const ipAddressDetector = new IpAddressDetector(consulConfig);
ipAddressDetector.getLanAndWanFromConsul().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
})Check the
examplefolder and you may find use cases.
Registrator
const consulConfig = {
"host": consulHost,
"port": consulPort
};
const serviceName = 'example_service_1';
const servicePort = 8080;
const serviceId = `${serviceName}_${servicePort}`;
const s = new ConsulServiceRegistrator(consulConfig, serviceName, serviceId);
s.setAddress("127.0.0.1");
s.setPort(servicePort);
s.setTags([`node-${serviceName}`, 'example']);
s.setTaggedAddress(ConsulServiceRegistrator.TAGGED_ADDRESS_TYPE_LAN, 'domain.priv', 8080);
s.setTaggedAddress(ConsulServiceRegistrator.TAGGED_ADDRESS_TYPE_WAN, 'domain.pub', 80801);
const overwrite = true;
await s.register(overwrite);