node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



Build Status Coverage Status

nodejs service discover

in dev now. document will be wrote later, thanks




npm i discoverer --save

use server

const DiscovererServer = require('discoverer').DiscovererServer;
const server = new DiscovererServer();

service provider

const restify = require('restify');
const DiscovererClient = require('discoverer').DiscovererClient;
const listenPort = process.env.PORT || 1234;
const server = restify.createServer();
const discovererClient = new DiscovererClient();
server.get('/api/v1/add', function (req, res, next) {
    sum: parseInt(req.query.a) + parseInt(req.query.b)
server.listen(listenPort, () => {
  console.log(`${} listen on ${server.url}`)
discovererClient._registe(info => console.log(info))

service consumer

const DiscovererClient = require('discoverer').DiscovererClient;
// construct a new Discoverer client 
const discover = new DiscovererClient();
// create a client load balance api client 
const compute_server = discover.create_api_of('add-compute-service');
// wrap a request as a function 
const add = (a, b) => {
  return compute_server
    .request(`/api/v1/add?a=${a}&b=${b}`, {
      json: true
    .then(result => result.sum)
    .catch(err => {
      throw err;
// another js file 
// out: 3 

environment variable

All config items can be configured by system env variables


  1. USE_AUTH, default is false, set "true" will enable http digest auth
  2. AUTH_USER, avoid server access by un auth user, system will generte a random str if it not set
  3. AUTH_PASS, avoid server access by un auth user, system will generte a random str if it not set
  4. MONGO_CONNECT_URI, required, default is mongodb://localhost/discoverer
  5. LISTEN_HOST, default is
  6. PORT, default is 3999
  7. CHECK_INTERVAL, default is 2s, 服务器会每隔几秒检测数据库中是否有超时的instance,有的话就会移除记录


  1. C_SERVER_URL, default is, 如果server配置了http basic auth, 需要在url中指明,例如:
  2. C_AUTH_USER, use to pass the server auth
  3. C_AUTH_PASS, use to pass the server auth
  4. C_SERVICE_NAME, required, default is hostname, 服务名非常重要, consumer也是通过这个名称拉取provider列表
  5. C_INSTANCE_URL, required, default is http://yourip:80, 这里配置的地址是被外部服务远程调用的地址
  6. C_INSTANCE_ID, if not set, server will give you one
  7. C_HEART_BREAK_INTERVAL, default is 15s
  8. C_NO_REGISTE, default is false, if set this flag is true, client无论如何都不会注册到服务器,即使显式的调用了_registe方法


  • mongodb storage
  • move operate to service layer, [cancel]
  • add log
  • client cli
  • client default url is ip
  • promisefy discoverer client
  • api client, load balance, round method
  • need a switch to adapt a client just want to consume services
  • if there is not client info when renew, server should registe
  • authorize should be more attention, security with http digest auth
  • center config file
  • command line var
  • server side health check
  • when ApiClient catch an exception, should refresh client list