registrator

1.0.0-beta.2 • Public • Published

Build Status

Registrator

A library to manage service registry and discover over etcd.

You will need a connection to etcd (brew install etcd && etcd).

This module was designed to allow an edge server or middle later to provide a unified façade to other HTTP services.

Connect to etcd

const Registry = require('registrator')

const registry = new Registry()
const registry = new Registry('127.0.0.1', '4001')

Register a service

  name = 'NamelessService', ip = '127.0.0.1', port = 8080, router, tags = [ ], ttl = 300
registry.register({
  name   : 'MyServiceName',      // Name that will be used by your clients
  ip     : '10.244.1.105',       // IP that the service is bound to
  port   : '8080',               // Port that the service is bound to
  router : {                     // COnfiguration for how your service will be exposed
    '/foo' : [{
      method : 'GET',
      path   : '/snore/lax'
    }, {
      method : 'PUT',
      path   : '/poke/:mon'
    }]
  },
  tags   : ['Testing', 'V1.1'])  // Some metadata tags
.then(...)

Discover a service

A call to Discover will not fulfill until the required service has been registered into etcd.

registry.discover('MyServiceName')
.then(function(service) {
        console.log(util.inspect(service))
      })
.then(...)

// {
//    name: 'MyServiceName',
//    ip: '10.244.1.105',
//    port: '8080',
//    routes: { '/foo': [{ ... }] },
//    tags: ['Testing', 'V1.1']
// }

Discover several services at once

A call to DiscoverAll will not fulfill until all the services specified have been registered into etcd.

registry.discoverAll(['ServiceA', 'ServiceB'])
.then(function(services) {
        console.log(util.inspect(services));
      })
.then(...);

// { 
//     ServiceA:
//     { 
//        name: 'ServiceA',
//        ip: '192.168.1.1',
//        port: '80',
//        tags: [ 'Production', 'Version-1.12.3' ]
//     },
//     ServiceB:
//     {
//        name: 'ServiceB',
//        ip: '192.168.1.2',
//        port: '81',
//        routes: { '/foo' : [{ ... }] },
//        tags: [ 'Production', 'Version-1.12.4' ]
//     }
// }

Example

Running the example

node examples/proxy-server-restify/

Accessing a proxy endpoint

curl localhost:5000/foo/snore/lax

Package Sidebar

Install

npm i registrator

Weekly Downloads

0

Version

1.0.0-beta.2

License

MIT

Last publish

Collaborators

  • jameswomack