consul-service-registrator

2.6.0 • Public • Published

Consul Service Registrator

NPM version Release Status Build Status Coverage Status

Please, check the full documentation below.

Table of Contents

Installation and Usage

Using npm:

$ npm install --save consul-service-registrator

Using yarn:

$ yarn add consul-service-registrator

Usage

Lib provides 3 classes:

  • IpAddressDetector
  • ServiceObserver
  • Registrator

IpAddressDetector

Class has a method getLanAndWanFromConsul that return Promise.

Method fetches from consul a response of /v1/agent/self and

  • if both Config.AdvertiseAddr and Config.AdvertiseAddrWan are present returns them back as result.lanIp and result.wanIp correspondingly;
  • if both DebugConfig.AdvertiseAddrLAN and DebugConfig.AdvertiseAddrWAN are present returns them back as result.lanIp and result.wanIp correspondingly.

Note. Config shows the explicitly defined configuration. DebugConfig shows 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 example folder 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);

Development

Package Sidebar

Install

npm i consul-service-registrator

Weekly Downloads

74

Version

2.6.0

License

MIT

Unpacked Size

79.9 kB

Total Files

22

Last publish

Collaborators

  • woz
  • legenage
  • fxmaxvl
  • jangolle
  • lcmbot
  • fox_hellraiser