Compare commits
20 Commits
Author | SHA1 | Date |
---|---|---|
|
8a5568de77 | |
|
57cd21060b | |
|
7fa34ff6a6 | |
|
22c0e57947 | |
|
cdea9a26b1 | |
|
50ac48e026 | |
|
3c6840bdd7 | |
|
4c9af68899 | |
|
cc6667c5b1 | |
|
d6ee163a36 | |
|
41e3965870 | |
|
a3feb781a7 | |
|
910f7e4bfe | |
|
7f1870778e | |
|
d044e6cef4 | |
|
005134d725 | |
|
e3cb1d5955 | |
|
6df7ece0a8 | |
|
27a8fa7651 | |
|
a3f33bd901 |
|
@ -177,6 +177,10 @@ Please let us know how can we help. Do check out [issues](https://github.com/nac
|
||||||
|
|
||||||
PR is welcome.
|
PR is welcome.
|
||||||
|
|
||||||
|
nacos-sdk-nodejs ding group : 44654232
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[Apache License V2](LICENSE)
|
[Apache License V2](LICENSE)
|
||||||
|
|
|
@ -3,8 +3,11 @@
|
||||||
const NacosConfigClient = require('nacos').NacosConfigClient;
|
const NacosConfigClient = require('nacos').NacosConfigClient;
|
||||||
|
|
||||||
const configClient = new NacosConfigClient({
|
const configClient = new NacosConfigClient({
|
||||||
serverAddr: '106.14.43.196:8848',
|
serverAddr: 'aliyun.nacos.net:80',
|
||||||
namespace: '',
|
namespace: '',
|
||||||
|
// 如果nacos开启了认证鉴权,需要在此处填写用户名密码
|
||||||
|
// username: 'xxx',
|
||||||
|
// password: 'xxx'
|
||||||
});
|
});
|
||||||
|
|
||||||
function sleep(time){
|
function sleep(time){
|
||||||
|
@ -18,7 +21,7 @@ function sleep(time){
|
||||||
(async () => {
|
(async () => {
|
||||||
await configClient.ready();
|
await configClient.ready();
|
||||||
|
|
||||||
const dataId = 'nacos.test.1';
|
const dataId = 'nacos.test.22';
|
||||||
const group = 'DEFAULT_GROUP';
|
const group = 'DEFAULT_GROUP';
|
||||||
const str = `example_test_${Math.random()}_${Date.now()}`;
|
const str = `example_test_${Math.random()}_${Date.now()}`;
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
"name": "nacos-example",
|
"name": "nacos-example",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"nacos": "^1.0.0"
|
"nacos": "^2.5.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,5 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "2.3.0"
|
"version": "2.6.0"
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
"ci": "npm run cov",
|
"ci": "npm run cov",
|
||||||
"build": "lerna run build && cp ./README.md ./packages/nacos/README.md"
|
"build": "lerna run build && cp ./README.md ./packages/nacos/README.md"
|
||||||
},
|
},
|
||||||
"license": "Apache",
|
"license": "Apache-2.0",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/nacos-group/nacos-sdk-nodejs/issues"
|
"url": "https://github.com/nacos-group/nacos-sdk-nodejs/issues"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "nacos-config",
|
"name": "nacos-config",
|
||||||
"version": "2.3.0",
|
"version": "2.6.0",
|
||||||
"description": "nacos config client",
|
"description": "nacos config client",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"nacos-config",
|
"nacos-config",
|
||||||
|
|
|
@ -361,7 +361,8 @@ export class ClientWorker extends Base implements IClientWorker {
|
||||||
group,
|
group,
|
||||||
content,
|
content,
|
||||||
tenant: this.namespace,
|
tenant: this.namespace,
|
||||||
type
|
type,
|
||||||
|
appName: this.appName
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -19,6 +19,7 @@ import { ClientOptionKeys, IConfiguration, IServerListManager } from './interfac
|
||||||
import * as urllib from 'urllib';
|
import * as urllib from 'urllib';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
import { encodingParams, transformGBKToUTF8 } from './utils';
|
import { encodingParams, transformGBKToUTF8 } from './utils';
|
||||||
|
import * as dns from 'dns';
|
||||||
|
|
||||||
export class HttpAgent {
|
export class HttpAgent {
|
||||||
|
|
||||||
|
@ -90,6 +91,10 @@ export class HttpAgent {
|
||||||
return this.configuration.get(ClientOptionKeys.IDENTITY_VALUE);
|
return this.configuration.get(ClientOptionKeys.IDENTITY_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get endpointQueryParams() {
|
||||||
|
return this.configuration.get(ClientOptionKeys.ENDPOINT_QUERY_PARAMS)
|
||||||
|
}
|
||||||
|
|
||||||
get decodeRes() {
|
get decodeRes() {
|
||||||
return this.configuration.get(ClientOptionKeys.DECODE_RES);
|
return this.configuration.get(ClientOptionKeys.DECODE_RES);
|
||||||
}
|
}
|
||||||
|
@ -191,6 +196,9 @@ export class HttpAgent {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
if (err.code === dns.NOTFOUND) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
err.url = `${method} ${url}`;
|
err.url = `${method} ${url}`;
|
||||||
err.data = data;
|
err.data = data;
|
||||||
err.headers = headers;
|
err.headers = headers;
|
||||||
|
|
|
@ -262,9 +262,12 @@ export interface ClientOptions {
|
||||||
serverAddr?: string; // 用于直连,包含端口
|
serverAddr?: string; // 用于直连,包含端口
|
||||||
unit?: string; // 内部单元化用
|
unit?: string; // 内部单元化用
|
||||||
nameServerAddr?: string; // 老的兼容参数,逐步废弃,同 endpoint
|
nameServerAddr?: string; // 老的兼容参数,逐步废弃,同 endpoint
|
||||||
|
username?: string; // 认证的用户名
|
||||||
|
password?: string; // 认证的密码
|
||||||
cacheDir?: string; // 缓存文件的路径
|
cacheDir?: string; // 缓存文件的路径
|
||||||
identityKey?: string; // Identity Key
|
identityKey?: string; // Identity Key
|
||||||
identityValue?: string; // Identity Value
|
identityValue?: string; // Identity Value
|
||||||
|
endpointQueryParams?: string; // endPoint 查询参数 e.g: param_1=1¶m_2=2
|
||||||
decodeRes?: (res: any, method?: string, encoding?: string) => any
|
decodeRes?: (res: any, method?: string, encoding?: string) => any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,6 +295,7 @@ export enum ClientOptionKeys {
|
||||||
IDENTITY_KEY = 'identityKey',
|
IDENTITY_KEY = 'identityKey',
|
||||||
IDENTITY_VALUE = 'identityValue',
|
IDENTITY_VALUE = 'identityValue',
|
||||||
DECODE_RES = 'decodeRes',
|
DECODE_RES = 'decodeRes',
|
||||||
|
ENDPOINT_QUERY_PARAMS = 'endpointQueryParams'
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IConfiguration {
|
export interface IConfiguration {
|
||||||
|
|
|
@ -108,6 +108,10 @@ export class ServerListManager extends Base implements IServerListManager {
|
||||||
return this.configuration.get(ClientOptionKeys.CLUSTER_NAME) || 'serverlist';
|
return this.configuration.get(ClientOptionKeys.CLUSTER_NAME) || 'serverlist';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get endpointQueryParams() {
|
||||||
|
return this.configuration.get(ClientOptionKeys.ENDPOINT_QUERY_PARAMS)
|
||||||
|
}
|
||||||
|
|
||||||
get requestTimeout(): number {
|
get requestTimeout(): number {
|
||||||
return this.configuration.get(ClientOptionKeys.REQUEST_TIMEOUT);
|
return this.configuration.get(ClientOptionKeys.REQUEST_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
@ -226,7 +230,8 @@ export class ServerListManager extends Base implements IServerListManager {
|
||||||
|
|
||||||
// 获取请求 url
|
// 获取请求 url
|
||||||
protected getRequestUrl(unit) {
|
protected getRequestUrl(unit) {
|
||||||
return `http://${this.nameServerAddr}/${this.contextPath}/${this.clusterName}`;
|
const endpointQueryParams = !!this.endpointQueryParams ? `?${this.endpointQueryParams}` : '';
|
||||||
|
return `http://${this.nameServerAddr}/${this.contextPath}/${this.clusterName}${endpointQueryParams}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -3,14 +3,35 @@
|
||||||
* Create Time: 2021/8/20 9:12
|
* Create Time: 2021/8/20 9:12
|
||||||
*/
|
*/
|
||||||
interface Instance {
|
interface Instance {
|
||||||
|
instanceId: string,
|
||||||
ip: string, //IP of instance
|
ip: string, //IP of instance
|
||||||
port: number, //Port of instance
|
port: number, //Port of instance
|
||||||
|
healthy: boolean,
|
||||||
|
enabled: boolean,
|
||||||
|
serviceName?: string,
|
||||||
weight?: number,
|
weight?: number,
|
||||||
ephemeral?: boolean,
|
ephemeral?: boolean,
|
||||||
clusterName?: string
|
clusterName?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Hosts = string[];
|
export interface Host {
|
||||||
|
instanceId: string;
|
||||||
|
ip: string;
|
||||||
|
port: number;
|
||||||
|
weight: number;
|
||||||
|
healthy: boolean;
|
||||||
|
enabled: boolean;
|
||||||
|
ephemeral: boolean;
|
||||||
|
clusterName: string;
|
||||||
|
serviceName: string;
|
||||||
|
metadata: any;
|
||||||
|
instanceHeartBeatInterval: number;
|
||||||
|
instanceIdGenerator: string;
|
||||||
|
instanceHeartBeatTimeOut: number;
|
||||||
|
ipDeleteTimeout: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hosts = Host[];
|
||||||
|
|
||||||
interface SubscribeInfo {
|
interface SubscribeInfo {
|
||||||
serviceName: string,
|
serviceName: string,
|
||||||
|
@ -18,11 +39,22 @@ interface SubscribeInfo {
|
||||||
clusters?: string
|
clusters?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface NacosNamingClientConfig {
|
||||||
|
logger: typeof console,
|
||||||
|
serverList: string | string[],
|
||||||
|
namespace?: string,
|
||||||
|
username?: string,
|
||||||
|
password?: string,
|
||||||
|
endpoint?: string,
|
||||||
|
vipSrvRefInterMillis?: number,
|
||||||
|
ssl?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nacos服务发现组件
|
* Nacos服务发现组件
|
||||||
*/
|
*/
|
||||||
export class NacosNamingClient {
|
export class NacosNamingClient {
|
||||||
constructor (config: { logger: typeof console, serverList: string | string[], namespace?: string })
|
constructor (config: NacosNamingClientConfig);
|
||||||
|
|
||||||
ready: () => Promise<void>;
|
ready: () => Promise<void>;
|
||||||
// Register an instance to service
|
// Register an instance to service
|
||||||
|
@ -44,6 +76,14 @@ export class NacosNamingClient {
|
||||||
clusters?: string, //Cluster names
|
clusters?: string, //Cluster names
|
||||||
subscribe?: boolean //whether subscribe the service, default is true
|
subscribe?: boolean //whether subscribe the service, default is true
|
||||||
) => Promise<Hosts>;
|
) => Promise<Hosts>;
|
||||||
|
// Select instance list of service.
|
||||||
|
selectInstances: (
|
||||||
|
serviceName: string,
|
||||||
|
groupName?: string,
|
||||||
|
clusters?: string,
|
||||||
|
healthy?: boolean,
|
||||||
|
subscribe?: boolean
|
||||||
|
) => Promise<Hosts>;
|
||||||
// Get the status of nacos server, 'UP' or 'DOWN'.
|
// Get the status of nacos server, 'UP' or 'DOWN'.
|
||||||
getServerStatus: () => 'UP' | 'DOWN';
|
getServerStatus: () => 'UP' | 'DOWN';
|
||||||
subscribe: (
|
subscribe: (
|
||||||
|
|
|
@ -22,6 +22,7 @@ const Base = require('sdk-base');
|
||||||
const Constants = require('../const');
|
const Constants = require('../const');
|
||||||
const ServiceInfo = require('./service_info');
|
const ServiceInfo = require('./service_info');
|
||||||
const PushReceiver = require('./push_receiver');
|
const PushReceiver = require('./push_receiver');
|
||||||
|
const equals = require('equals');
|
||||||
|
|
||||||
class HostReactor extends Base {
|
class HostReactor extends Base {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
|
@ -92,7 +93,7 @@ class HostReactor extends Base {
|
||||||
const key = host.ip + ':' + host.port;
|
const key = host.ip + ':' + host.port;
|
||||||
newHostMap.set(key, host);
|
newHostMap.set(key, host);
|
||||||
|
|
||||||
if (oldHostMap.has(key) && JSON.stringify(host) !== JSON.stringify(oldHostMap.get(key))) {
|
if (oldHostMap.has(key) && !equals(host, oldHostMap.get(key))) {
|
||||||
modHosts.push(host);
|
modHosts.push(host);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,12 @@ class ServiceInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
get isValid() {
|
get isValid() {
|
||||||
return !!this.hosts;
|
const valid = !!this.hosts;
|
||||||
|
// 如果 this.hosts 是空数组要返回 false
|
||||||
|
if (valid && Array.isArray(this.hosts)) {
|
||||||
|
return this.hosts.length > 0;
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
getKey() {
|
getKey() {
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "nacos-naming",
|
"name": "nacos-naming",
|
||||||
"version": "2.3.0",
|
"version": "2.6.0",
|
||||||
"description": "nacos (https://nacos.io/en-us/) nodejs sdk",
|
"description": "nacos (https://nacos.io/en-us/) nodejs sdk",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"lib",
|
"lib",
|
||||||
"index.js"
|
"index.js",
|
||||||
|
"index.d.ts"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"autod": "autod",
|
"autod": "autod",
|
||||||
|
@ -42,6 +43,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"address": "^1.1.0",
|
"address": "^1.1.0",
|
||||||
|
"equals": "^1.0.5",
|
||||||
"mz-modules": "^2.1.0",
|
"mz-modules": "^2.1.0",
|
||||||
"sdk-base": "^3.6.0",
|
"sdk-base": "^3.6.0",
|
||||||
"urllib": "^2.33.3",
|
"urllib": "^2.33.3",
|
||||||
|
|
|
@ -177,6 +177,10 @@ Please let us know how can we help. Do check out [issues](https://github.com/nac
|
||||||
|
|
||||||
PR is welcome.
|
PR is welcome.
|
||||||
|
|
||||||
|
nacos-sdk-nodejs ding group : 44654232
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[Apache License V2](LICENSE)
|
[Apache License V2](LICENSE)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "nacos",
|
"name": "nacos",
|
||||||
"version": "2.3.0",
|
"version": "2.6.0",
|
||||||
"description": "nacos client main package",
|
"description": "nacos client main package",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"nacos",
|
"nacos",
|
||||||
|
@ -12,8 +12,8 @@
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"author": "czy88840616@gmail.com",
|
"author": "czy88840616@gmail.com",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"nacos-config": "^2.3.0",
|
"nacos-config": "^2.6.0",
|
||||||
"nacos-naming": "^2.3.0"
|
"nacos-naming": "^2.6.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/mocha": "^5.2.5",
|
"@types/mocha": "^5.2.5",
|
||||||
|
|
Loading…
Reference in New Issue