版本对应
| Nacos Server | Node.js SDK |
|---|---|
| 1.x | 0.x.0 |
| 2.x | 1.0.0 |
使用说明
服务发现示例
'use strict';
const NacosNamingClient = require('nacos').NacosNamingClient;const logger = console;
const client = new NacosNamingClient({ logger, serverList: '${addr}', // replace to real nacos serverList namespace: 'public',});await client.ready();
const serviceName = 'nodejs.test.domain';
// registry instanceawait client.registerInstance(serviceName, { ip: '192.168.0.1', port: 8080,});await client.registerInstance(serviceName, { ip: '192.168.0.2', port: 8080,});
// subscribe instanceclient.subscribe(serviceName, hosts => { console.log(hosts);});
// deregister instanceawait client.deregisterInstance(serviceName, { ip: '192.168.0.2', port: 8080,});
配置管理示例
import {NacosConfigClient} from 'nacos'; // tsconst NacosConfigClient = require('nacos').NacosConfigClient; // js
// for direct modeconst configClient = new NacosConfigClient({ serverAddr: '${addr}', namespace: 'public',});
// get config onceconst content= await configClient.getConfig('test', 'DEFAULT_GROUP');console.log('getConfig = ',content);
// listen data changedconfigClient.subscribe({ dataId: 'test', group: 'DEFAULT_GROUP',}, content => { console.log(content);});
// publish configconst content= await configClient.publishSingle('test', 'DEFAULT_GROUP', '测试');console.log('getConfig = ',content);
// remove configawait configClient.remove('test', 'DEFAULT_GROUP');
API使用
服务注册
注册实例
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, [groupName])` 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
配置服务
查询配置
* async function getConfig(dataId, group) * {String} dataId - data id * {String} group - group name
发布配置
* `async function publishSingle(dataId, group, content)` * {String} dataId - data id * {String} group - group name * {String} content - content you want to publish
删除配置
* `async function remove(dataId, group)` * {String} dataId - data id * {String} group - group name
监听配置
* `function subscribe(info, listener)` * {Object} info * {String} dataId - data id * {String} group - group name * {Function} listener - callback handler 监听回调函数
取消配置监听
* `function unSubscribe(info, [listener])` * {Object} info * {String} dataId - data id * {String} group - group * {Function} listener - callback handler(optional,remove all listener when it is null) 监听取消回调函数