@midwayjs/consul 4.0.0-beta.7 → 4.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/extension/helper.js
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TCPHealthCheck = exports.HTTPHealthCheck = exports.
|
|
3
|
+
exports.TCPHealthCheck = exports.HTTPHealthCheck = exports.TTLHeartbeat = void 0;
|
|
4
|
+
exports.calculateTTL = calculateTTL;
|
|
4
5
|
const ms_1 = require("ms");
|
|
5
6
|
const http_1 = require("http");
|
|
6
7
|
const url_1 = require("url");
|
|
7
8
|
const net_1 = require("net");
|
|
8
9
|
class TTLHeartbeat {
|
|
10
|
+
consul;
|
|
11
|
+
checkId;
|
|
12
|
+
interval;
|
|
13
|
+
timer = null;
|
|
14
|
+
logger;
|
|
9
15
|
/**
|
|
10
16
|
* @param {TTLHeartbeatOptions} options - 配置项
|
|
11
17
|
*/
|
|
12
18
|
constructor(options) {
|
|
13
|
-
this.timer = null;
|
|
14
19
|
this.consul = options.consul;
|
|
15
20
|
this.checkId = options.checkId;
|
|
16
21
|
this.interval = options.interval ?? 5000;
|
|
@@ -53,18 +58,20 @@ function calculateTTL(ttl) {
|
|
|
53
58
|
// 最小不能低于 1 秒
|
|
54
59
|
return Math.max(ttlMs, 1000);
|
|
55
60
|
}
|
|
56
|
-
exports.calculateTTL = calculateTTL;
|
|
57
61
|
/**
|
|
58
62
|
* HTTP 健康检查工具类
|
|
59
63
|
* 传入 http url,自动启动一个健康检查服务
|
|
60
64
|
*/
|
|
61
65
|
class HTTPHealthCheck {
|
|
66
|
+
server = null;
|
|
67
|
+
port;
|
|
68
|
+
path;
|
|
69
|
+
message;
|
|
62
70
|
/**
|
|
63
71
|
* @param httpUrl 形如 'http://127.0.0.1:3000/health'
|
|
64
72
|
* @param message 健康时返回的内容,默认 'ok'
|
|
65
73
|
*/
|
|
66
74
|
constructor(httpUrl, message = 'ok') {
|
|
67
|
-
this.server = null;
|
|
68
75
|
const url = new url_1.URL(httpUrl);
|
|
69
76
|
this.port = Number(url.port) || 80;
|
|
70
77
|
this.path = url.pathname;
|
|
@@ -106,11 +113,13 @@ exports.HTTPHealthCheck = HTTPHealthCheck;
|
|
|
106
113
|
* 传入 tcp url,自动启动一个 TCP 服务
|
|
107
114
|
*/
|
|
108
115
|
class TCPHealthCheck {
|
|
116
|
+
server = null;
|
|
117
|
+
port;
|
|
118
|
+
host;
|
|
109
119
|
/**
|
|
110
120
|
* @param tcpUrl 形如 'tcp://127.0.0.1:3000'
|
|
111
121
|
*/
|
|
112
122
|
constructor(tcpUrl) {
|
|
113
|
-
this.server = null;
|
|
114
123
|
const url = new url_1.URL(tcpUrl);
|
|
115
124
|
this.port = Number(url.port) || 80;
|
|
116
125
|
this.host = url.hostname || '0.0.0.0';
|
|
@@ -35,7 +35,7 @@ export declare class ConsulServiceDiscovery extends ServiceDiscovery<ConsulClien
|
|
|
35
35
|
private listenerStore;
|
|
36
36
|
private configService;
|
|
37
37
|
init(): Promise<void>;
|
|
38
|
-
protected getServiceClient(): import("consul
|
|
38
|
+
protected getServiceClient(): import("consul");
|
|
39
39
|
protected createServiceDiscoverClientImpl(options: ConsulServiceDiscoveryOptions): ConsulServiceDiscoverClient;
|
|
40
40
|
protected getDefaultServiceDiscoveryOptions(): ConsulServiceDiscoveryOptions;
|
|
41
41
|
private getListener;
|
|
@@ -19,6 +19,10 @@ const helper_1 = require("./helper");
|
|
|
19
19
|
* @since 4.0.0
|
|
20
20
|
*/
|
|
21
21
|
class ConsulDataListener extends core_1.DataListener {
|
|
22
|
+
client;
|
|
23
|
+
options;
|
|
24
|
+
logger;
|
|
25
|
+
watcher;
|
|
22
26
|
constructor(client, options, logger) {
|
|
23
27
|
super();
|
|
24
28
|
this.client = client;
|
|
@@ -60,6 +64,13 @@ class ConsulDataListener extends core_1.DataListener {
|
|
|
60
64
|
* @since 4.0.0
|
|
61
65
|
*/
|
|
62
66
|
class ConsulServiceDiscoverClient extends core_1.ServiceDiscoveryClient {
|
|
67
|
+
consul;
|
|
68
|
+
applicationManager;
|
|
69
|
+
webRouterService;
|
|
70
|
+
logger;
|
|
71
|
+
ttlHeartbeat;
|
|
72
|
+
httpHealthCheck;
|
|
73
|
+
tcpHealthCheck;
|
|
63
74
|
constructor(consul, serviceDiscoveryOptions, applicationManager, webRouterService, logger) {
|
|
64
75
|
super(consul, serviceDiscoveryOptions);
|
|
65
76
|
this.consul = consul;
|
|
@@ -170,10 +181,14 @@ exports.ConsulServiceDiscoverClient = ConsulServiceDiscoverClient;
|
|
|
170
181
|
* @since 4.0.0
|
|
171
182
|
*/
|
|
172
183
|
let ConsulServiceDiscovery = class ConsulServiceDiscovery extends core_1.ServiceDiscovery {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
184
|
+
consulServiceFactory;
|
|
185
|
+
consulServiceDiscoveryOptions;
|
|
186
|
+
applicationManager;
|
|
187
|
+
webRouterService;
|
|
188
|
+
coreLogger;
|
|
189
|
+
defaultServiceClient;
|
|
190
|
+
listenerStore = new Map();
|
|
191
|
+
configService;
|
|
177
192
|
async init() {
|
|
178
193
|
this.consulServiceDiscoveryOptions = this.configService.getConfiguration('consul.serviceDiscovery', {});
|
|
179
194
|
}
|
package/dist/manager.js
CHANGED
|
@@ -13,11 +13,13 @@ exports.ConsulService = exports.ConsulServiceFactory = void 0;
|
|
|
13
13
|
const core_1 = require("@midwayjs/core");
|
|
14
14
|
const Consul = require("consul");
|
|
15
15
|
let ConsulServiceFactory = class ConsulServiceFactory extends core_1.ServiceFactory {
|
|
16
|
+
consulConfig;
|
|
16
17
|
async init() {
|
|
17
18
|
await this.initClients(this.consulConfig, {
|
|
18
19
|
concurrent: true,
|
|
19
20
|
});
|
|
20
21
|
}
|
|
22
|
+
logger;
|
|
21
23
|
async createClient(config, clientName) {
|
|
22
24
|
this.logger.info('[midway:consul] init %s at %s:%s', clientName, config.host, config.port);
|
|
23
25
|
return new Consul(config);
|
|
@@ -49,6 +51,8 @@ exports.ConsulServiceFactory = ConsulServiceFactory = __decorate([
|
|
|
49
51
|
(0, core_1.Singleton)()
|
|
50
52
|
], ConsulServiceFactory);
|
|
51
53
|
let ConsulService = class ConsulService {
|
|
54
|
+
serviceFactory;
|
|
55
|
+
instance;
|
|
52
56
|
async init() {
|
|
53
57
|
this.instance = this.serviceFactory.get(this.serviceFactory.getDefaultClientName?.() || 'default');
|
|
54
58
|
if (!this.instance) {
|
package/dist/utils.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.isObjectError = isObjectError;
|
|
4
|
+
exports.formatObjectErrorToError = formatObjectErrorToError;
|
|
5
|
+
exports.hashServiceOptions = hashServiceOptions;
|
|
4
6
|
const core_1 = require("@midwayjs/core");
|
|
5
7
|
function isObjectError(obj) {
|
|
6
8
|
return obj && typeof obj === 'object' && 'errno' in obj;
|
|
7
9
|
}
|
|
8
|
-
exports.isObjectError = isObjectError;
|
|
9
10
|
function formatObjectErrorToError(obj) {
|
|
10
11
|
const err = new Error(obj.message);
|
|
11
12
|
err.stack = obj.stack;
|
|
@@ -19,7 +20,6 @@ function formatObjectErrorToError(obj) {
|
|
|
19
20
|
err.cause = commonErr;
|
|
20
21
|
return commonErr;
|
|
21
22
|
}
|
|
22
|
-
exports.formatObjectErrorToError = formatObjectErrorToError;
|
|
23
23
|
/**
|
|
24
24
|
* 对于 1 万个不同 ServiceOptions 组合,使用当前这个基于 DJB2 + base36 的方法,冲突概率大约是 0.1%,也就是理论上每 1 万个中只有 10 个左右可能冲突,而且这还是在完全随机分布的情况下估算的
|
|
25
25
|
* 适用于对象类型是扁平的,没有嵌套
|
|
@@ -41,5 +41,4 @@ function hashServiceOptions(options) {
|
|
|
41
41
|
}
|
|
42
42
|
return (hash >>> 0).toString(36); // 无符号,base36 更短
|
|
43
43
|
}
|
|
44
|
-
exports.hashServiceOptions = hashServiceOptions;
|
|
45
44
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/consul",
|
|
3
3
|
"description": "midway consul",
|
|
4
|
-
"version": "4.0.0-beta.
|
|
4
|
+
"version": "4.0.0-beta.8",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"index.d.ts"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@midwayjs/core": "^4.0.0-beta.
|
|
14
|
-
"@midwayjs/koa": "^4.0.0-beta.
|
|
15
|
-
"@midwayjs/mock": "^4.0.0-beta.
|
|
13
|
+
"@midwayjs/core": "^4.0.0-beta.8",
|
|
14
|
+
"@midwayjs/koa": "^4.0.0-beta.8",
|
|
15
|
+
"@midwayjs/mock": "^4.0.0-beta.8",
|
|
16
16
|
"@types/sinon": "17.0.4",
|
|
17
17
|
"nock": "13.5.6"
|
|
18
18
|
},
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"type": "git",
|
|
40
40
|
"url": "https://github.com/midwayjs/midway.git"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "355e55949fdd132b0bdcb4830222a0a027e92ded"
|
|
43
43
|
}
|