nacos-naming2
v2.0.8
Published
nacos (https://nacos.io/en-us/) nodejs sdk
Downloads
4
Readme
nacos-sdk-nodejs
=======
Nacos Node.js SDK
Install
npm install nacos --save
Usage
Service Discovery
'use strict';
const NacosNamingClient = require('nacos').NacosNamingClient;
const logger = console;
const client = new NacosNamingClient({
logger,
serverList: '127.0.0.1:8848', // replace to real nacos serverList
namespace: 'public',
});
await client.ready();
const serviceName = 'nodejs.test.domain';
// registry instance
await client.registerInstance(serviceName, {
ip: '1.1.1.1',
port: 8080,
});
await client.registerInstance(serviceName, {
ip: '2.2.2.2',
port: 8080,
});
// subscribe instance
client.subscribe(serviceName, hosts => {
console.log(hosts);
});
// deregister instance
await client.deregisterInstance(serviceName, {
ip: '1.1.1.1',
port: 8080,
});
APIs
Service Discovery
registerInstance(serviceName, instance, [groupName])
Register an instance to service.- serviceName {String} Service name
- instance {Instance}
- ip {String} IP of instance
- port {Number} Port of instance
- [weight] {Number} weight of the instance, default is 1.0
- [ephemeral] {Boolean} active until the client is alive, default is true
- [clusterName] {String} Virtual cluster name
- [groupName] {String} group name, default is
DEFAULT_GROUP
deregisterInstance(serviceName, ip, port, [cluster])
Delete instance from service.- serviceName {String} Service name
- instance {Instance}
- ip {String} IP of instance
- port {Number} Port of instance
- [weight] {Number} weight of the instance, default is 1.0
- [ephemeral] {Boolean} active until the client is alive, default is true
- [clusterName] {String} Virtual cluster name
- [groupName] {String} group name, default is
DEFAULT_GROUP
getAllInstances(serviceName, [groupName], [clusters], [subscribe])
Query instance list of service.- serviceName {String} Service name
- [groupName] {String} group name, default is
DEFAULT_GROUP
- [clusters] {String} Cluster names
- [subscribe] {Boolean} whether subscribe the service, default is true
getServerStatus()
Get the status of nacos server, 'UP' or 'DOWN'.subscribe(info, listener)
Subscribe the instances of the service- info {Object}|{String} service info, if type is string, it's the serviceName
- listener {Function} the listener function
unSubscribe(info, [listener])
Unsubscribe the instances of the service- info {Object}|{String} service info, if type is string, it's the serviceName
- listener {Function} the listener function, if not provide, will unSubscribe all listeners under this service
Questions & Suggestions
Please let us know how can we help. Do check out issues for bug reports or suggestions first.
PR is welcome.