1.0.0 • Published 10 months ago

kigo-nacos-naming v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

kigo-nacos-naming

nacos 注册中心客户端

Log4js category:

Naming

Install with:

$ cnpm install kigo-nacos-naming

Usage

服务订阅

'use strict'

const NamingClient = require('kigo-nacos-naming');

const client = new NamingClient({
  serverList: '127.0.0.1:8848',
  namespace: 'public'
})

// 订阅节点
client.subscribe('nodeim.comet', function(instances) {
  console.log(`节点变化: ${JSON.stringify(instances)}`);
})

// 取消订阅
client.unsubscribe('nodeim.comet');

服务注册

'use strict'

const NamingClient = require('kigo-nacos-naming');

const client = new NamingClient({
  serverList: '127.0.0.1:8848',
  namespace: 'public'
})

async function main() {
  // 服务注册
  await client.registerInstance('nodeim.comet', {
    host: '127.0.0.1',
    port: 3050
  })

  // 取消注册
  await client.deregisterInstance('nodeim.comet', {
    host: '127.0.0.1',
    port: 3051
  })
}

APIs

  • new NamingClient(opts)
    • opts {Object}
      • namespace {String} 命名空间 default is public
      • serverList {String|Array} nacos server list, if type is string, split by ','
      • ssl {boolean} default is false
      • ak {String}
      • sk {String}
      • appName {String}
  • async registerInstance(serviceName, instance) 注册服务.
    • serviceName {String} Service name
    • instance {Object}
      • host {String} Host of instance
      • port {Number} Port of instance
      • weight {Number} 权重 default is 1
      • enabled {boolean} 是否上线 default is true
      • healthy {boolean} 是否健康 default is true
      • metadata {Object} 扩展信息
      • clusterName {String} 集群名 default is DEFAULT
      • groupName {String} 分组名 default is DEFAULT_GROUP
      • ephemeral {boolean} 是否临时实例 default is true
  • async deregisterInstance(serviceName, instance) 取消注册.
    • serviceName {String} Service name
    • instance {Object}
      • host {String} Host of instance
      • port {Number} Port of instance
      • clusterName {String} 集群名 default is DEFAULT
      • groupName {String} 分组名 default is DEFAULT_GROUP
      • ephemeral {boolean} 是否临时实例 default is true
  • subscribe(params, listener) 订阅服务.
    • params {Object|String} service info, if type is string, it's the serviceName
      • serviceName {String} Service name
      • groupName {String} 分组名 default is DEFAULT_GROUP
      • clusters {String} 集群名称, 多个集群用逗号分隔
      • healthyOnly {boolean} 是否只返回健康实例 default is false
    • listener {Function} callback function
  • unsubscribe(params) 取消订阅.
    • params {Object|String} service info, if type is string, it's the serviceName
      • serviceName {String} Service name
      • groupName {String} 分组名 default is DEFAULT_GROUP
      • clusters {String} 集群名称, 多个集群用逗号分隔