Skip to content

Commit 2c0a53c

Browse files
committed
release
1 parent d872f68 commit 2c0a53c

3 files changed

Lines changed: 147 additions & 28 deletions

File tree

build/BrakerClient.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _Logger = require('./Logger');
8+
9+
var _Logger2 = _interopRequireDefault(_Logger);
10+
11+
var _Brakes = require('./Brakes');
12+
13+
var Brakes = _interopRequireWildcard(_Brakes);
14+
15+
var _Exception = require('./Exception');
16+
17+
var _Exception2 = _interopRequireDefault(_Exception);
18+
19+
var _ExternalException = require('./ExternalException');
20+
21+
var _ExternalException2 = _interopRequireDefault(_ExternalException);
22+
23+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
24+
25+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26+
27+
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
28+
29+
/**
30+
* An proxy client with load balance and circuit.
31+
*/
32+
class BrakerClient {
33+
constructor(serviceName, options) {
34+
this.options = options = options || {};
35+
this.logger = new _Logger2.default(options.logger);
36+
this.serviceName = serviceName;
37+
this.brake = Brakes.getBrakes(serviceName, options);
38+
}
39+
40+
/**
41+
* Set the circuit's health check callback.
42+
*
43+
* @param fn
44+
*/
45+
setHealthCheck(fn) {
46+
this.brake.healthCheck(() => {
47+
this.logger.info(`Check the service: '${this.serviceName}''s health status.`);
48+
return fn();
49+
});
50+
}
51+
52+
register(clientInterface) {
53+
let exports = {};
54+
for (let key in clientInterface) {
55+
if (!clientInterface.hasOwnProperty(key)) {
56+
continue;
57+
}
58+
59+
const func = clientInterface[key];
60+
const circuit = this.brake.slaveCircuit(func, this.fallback.bind(this));
61+
62+
this.logger.info(`Register the http api '${key}' to cloud client.`);
63+
64+
exports[key] = {
65+
id: '',
66+
circuit: circuit,
67+
exec: (() => {
68+
var _ref = _asyncToGenerator(function* (...params) {
69+
const response = yield circuit.exec(...params);
70+
if (response.statusCode < 300) {
71+
return response.body;
72+
} else {
73+
let body = response.body || {};
74+
75+
//If body.message is exist, throw body.message or throw body.
76+
throw new _Exception2.default(body.id, body.message || body, null, response.statusCode);
77+
}
78+
});
79+
80+
return function exec() {
81+
return _ref.apply(this, arguments);
82+
};
83+
})()
84+
};
85+
}
86+
87+
return exports;
88+
}
89+
90+
/**
91+
* Register the http api to this client.
92+
*
93+
* @return {*}
94+
*/
95+
registerApi(clientInterface) {
96+
let exports = this.register(clientInterface);
97+
98+
return exports.map(item => item.exec);
99+
}
100+
101+
/**
102+
* Circuit fallback method.
103+
*
104+
* @param err
105+
* @param params
106+
* @return {Promise.<*>}
107+
*/
108+
fallback(err, ...params) {
109+
this.logger.error(`Invoke downstream service '${this.serviceName}' fail and fallback, the params is ${JSON.stringify(params)}`, err);
110+
return Promise.reject(new _ExternalException2.default('', 'Cannot invoke downstream service. please try again soon.', err));
111+
}
112+
}
113+
exports.default = BrakerClient;

package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "cloud-client",
2+
"name": "braker-client",
33
"version": "0.0.1",
4-
"description": "An http client with loadbalance and circuit.",
5-
"main": "build/CloudClient.js",
4+
"description": "An client with circuit.",
5+
"main": "build/BrakerClient.js",
66
"scripts": {
77
"build": "babel src -d build",
88
"test": "echo \"Error: no test specified\" && exit 1"
@@ -12,12 +12,9 @@
1212
"url": "git+https://github.com/node-cloud/cloud-client.git"
1313
},
1414
"keywords": [
15-
"consul",
16-
"http-client",
17-
"loadbanance",
1815
"circuit",
1916
"brakes",
20-
"node-consul"
17+
"node-cloud"
2118
],
2219
"author": "miaowing <i@zfeng.net>",
2320
"license": "MIT",
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import ExternalException from './ExternalException'
66
/**
77
* An proxy client with load balance and circuit.
88
*/
9-
export default class CloudClient {
10-
constructor(serviceName, clientInterface, options) {
9+
export default class BrakerClient {
10+
constructor(serviceName, options) {
1111
this.options = options = options || {};
1212
this.logger = new Logger(options.logger);
1313
this.serviceName = serviceName;
14-
this.clientInterface = clientInterface;
1514
this.brake = Brakes.getBrakes(serviceName, options);
1615
}
1716

@@ -27,39 +26,49 @@ export default class CloudClient {
2726
});
2827
}
2928

30-
/**
31-
* Register the http api to this client.
32-
*
33-
* @return {{}}
34-
*/
35-
registerApi() {
29+
register(clientInterface) {
3630
let exports = {};
37-
for (let key in this.clientInterface) {
38-
if (!this.clientInterface.hasOwnProperty(key)) {
31+
for (let key in clientInterface) {
32+
if (!clientInterface.hasOwnProperty(key)) {
3933
continue;
4034
}
4135

42-
const func = this.clientInterface[key];
36+
const func = clientInterface[key];
4337
const circuit = this.brake.slaveCircuit(func, this.fallback.bind(this));
4438

45-
this.logger.info(`Register the http api '${key}' to feign client.`);
39+
this.logger.info(`Register the http api '${key}' to cloud client.`);
4640

47-
exports[key] = async (...params) => {
48-
const response = await circuit.exec(...params);
49-
if (response.statusCode < 300) {
50-
return response.body;
51-
} else {
52-
let body = response.body || {};
41+
exports[key] = {
42+
id: '',
43+
circuit: circuit,
44+
exec: async (...params) => {
45+
const response = await circuit.exec(...params);
46+
if (response.statusCode < 300) {
47+
return response.body;
48+
} else {
49+
let body = response.body || {};
5350

54-
// if body.message is exist, throw body.message or throw body.
55-
throw new Exception(body.id, body.message || body, null, response.statusCode);
51+
//If body.message is exist, throw body.message or throw body.
52+
throw new Exception(body.id, body.message || body, null, response.statusCode);
53+
}
5654
}
5755
}
5856
}
5957

6058
return exports;
6159
}
6260

61+
/**
62+
* Register the http api to this client.
63+
*
64+
* @return {*}
65+
*/
66+
registerApi(clientInterface) {
67+
let exports = this.register(clientInterface);
68+
69+
return exports.map(item => item.exec);
70+
}
71+
6372
/**
6473
* Circuit fallback method.
6574
*

0 commit comments

Comments
 (0)