forked from omnip620/node-zookeeper-dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (154 loc) · 4.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Created by panzhichao on 16/8/2.
*/
'use strict';
const net = require('net');
const url = require('url');
const zookeeper = require('node-zookeeper-client');
const qs = require('querystring');
const reg = require('./libs/register');
const decode = require('./libs/decode');
const Encode = require('./libs/encode').Encode;
require('./utils');
let SERVICE_LENGTH = 0;
let COUNT = 0;
/**
* @param {Object} opt {conn:'zk.dev.pajkdc.com:2181',
* dubbo:{version:PZC,
* dversion:2.3.4.6,
* group:'xxx'},
* dependencies:{}}
* @constructor
*/
var NZD = function (opt) {
const self = this;
this.dubboVer = opt.dubboVer;
this.application = opt.application;
this._root = opt.root||'dubbo';
this.dependencies = opt.dependencies || {};
SERVICE_LENGTH = Object.keys(this.dependencies).length;
this.client = zookeeper.createClient(opt.register, {
sessionTimeout: 30000,
spinDelay : 1000,
retries : 5
});
this.client.connect();
this.client.once('connected', function () {
self._applyServices();
self._consumer();
});
};
NZD.prototype._consumer = reg.consumer;
NZD.prototype._applyServices = function () {
const refs = this.dependencies;
const self = this;
for (let key in refs) {
NZD.prototype[key] = new Service(self.client, self.dubboVer, refs[key], this._root);
}
};
var Service = function (zk, dubboVer, depend, root) {
this._zk = zk;
this._hosts = [];
this._version = depend.version;
this._group = depend.group;
this._interface = depend.interface;
this._root = root;
this._encodeParam = {
_dver : dubboVer || '2.5.3.6',
_interface: depend.interface,
_version : depend.version,
_group : depend.group,
_timeout : depend.timeout || 6000
}
this._find(depend.interface);
};
Service.prototype._find = function (path, cb) {
const self = this;
self._hosts = [];
this._zk.getChildren(`/${this._root}/${path}/providers`, watch, handleResult);
function watch(event) {
self._find(path)
}
function handleResult(err, children) {
let zoo;
if (err) {
if (err.code === -4) {
console.log(err);
}
return console.log(err);
}
if (children && !children.length) {
return console.log(`can\'t find the zoo: ${path} ,pls check dubbo service!`);
}
for (let i = 0, l = children.length; i < l; i++) {
zoo = qs.parse(decodeURIComponent(children[i]));
if (zoo.version === self._version && zoo.group === self._group) {
self._hosts.push(url.parse(Object.keys(zoo)[0]).host);
const methods = zoo.methods.split(',');
for (let i = 0, l = methods.length; i < l; i++) {
self[methods[i]] = (function (method) {
return function () {
return self._execute(method, Array.from(arguments));
};
})(methods[i]);
}
}
}
if (typeof cb === 'function') {
return cb();
}
if (++COUNT === SERVICE_LENGTH) {
console.log('\x1b[32m%s\x1b[0m', 'Dubbo service init done');
}
}
};
Service.prototype._flush = function (cb) {
this._find(this._interface, cb)
};
Service.prototype._execute = function (method, args) {
const self = this;
this._encodeParam._method = method;
this._encodeParam._args = args;
const buffer = new Encode(this._encodeParam);
return new Promise(function (resolve, reject) {
const client = new net.Socket();
let host = self._hosts[Math.random() * self._hosts.length | 0].split(':');
const chunks = [];
let heap;
let bl = 16;
client.connect(host[1], host[0], function () {
client.write(buffer);
});
client.on('error', function (err) {
self._flush(function () {
host = self._hosts[Math.random() * self._hosts.length | 0].split(':');
client.connect(host[1], host[0], function () {
client.write(buffer);
});
})
});
client.on('data', function (chunk) {
if (!chunks.length) {
var arr = Array.prototype.slice.call(chunk.slice(0, 16));
var i = 0;
while (i < 3) {
bl += arr.pop() * Math.pow(256, i++);
}
}
chunks.push(chunk);
heap = Buffer.concat(chunks);
(heap.length >= bl) && client.destroy();
});
client.on('close', function (err) {
if (!err) {
decode(heap, function (err, result) {
if (err) {
return reject(err);
}
return resolve(result);
})
}
});
});
};
module.exports = NZD;