Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey600 committed Sep 18, 2017
2 parents 17ebff8 + 45b35fa commit e2e8fd9
Show file tree
Hide file tree
Showing 23 changed files with 2,202 additions and 700 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
17 changes: 17 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": [
"eslint:recommended",
"./rules/best-practices.js",
"./rules/errors.js",
"./rules/node.js",
"./rules/style.js",
"./rules/variables.js"
],
"env": {
"node": true,
"mocha": true
},
"globals": {
"Promise": true
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Webstorm
.idea
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

language: node_js
sudo: true
node_js:
- '0.12'
# - '0.12' 模块 generic-pool 不支持低版本
- '4'
- '6'
- '8'
cache:
directories:
- node_modules
script:
- npm run lint
- npm test
after_script:
- npm run coverage
51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ to connect Dubbo service by
- Invoke Dubbo service as a Customer.
- Use Zookeeper as the Dubbo Registration Center.
- Only supports the use of the default hessian2 protocol for serialization and deserialization.
- It is not very friendly to support the return value containing an enum type.
- It is not very friendly to support the return value containing an enum type.
- Use [Random LoadBalance](https://dubbo.gitbooks.io/dubbo-user-book/demos/loadbalance.html) to choose Provider.
- Use [generic-pool](https://github.com/coopernurse/node-pool) to manage net.Socket.

## Installation

Expand Down Expand Up @@ -59,7 +61,7 @@ invoker.excute(method, [arg1], function (err, data) {

*Arguments*

* conf {*Object*} - An object to set the instance options. Currently available options are:
* conf {*String|Object*} - A string of host:port pairs like `conn`. Or an object to set the instance options. Currently available options are:

* `dubbo` {*String*} - Dubbo version information.

Expand All @@ -72,6 +74,10 @@ invoker.excute(method, [arg1], function (err, data) {
*Example*

```javascript
// use a string of host:port pairs
var zd = new ZD('localhost:2181,localhost:2182');

// use an object to set the instance options
var zd = new ZD({
conn: 'localhost:2181,localhost:2182',
dubbo: '2.5.3'
Expand Down Expand Up @@ -118,7 +124,7 @@ zd.client.close();

----

#### Invoker getInvoker(path, opt)
#### Invoker getInvoker(path[, opt])

*Arguments*

Expand All @@ -128,6 +134,10 @@ zd.client.close();
* `version` {*String*} - Service version information.
* `timeout` {*Number*} - The timeout (in milliseconds) to excute.

*The following content could reference: [generic-pool](https://github.com/coopernurse/node-pool)*
* `poolMax` {*Number*} - Maximum number of net.Socket to create from pool at any given time. Defaults to 1 .
* `poolMin` {*Number*} - Minimum number of net.Socket to keep in pool at any given time. If this is set >= poolMax, the pool will silently set the min to equal max. Defaults to 0 .

*Example*

```javascript
Expand All @@ -138,15 +148,38 @@ var invoker = zd.getInvoker('com.demo.Service', {

----

## Invoker
### new Invoker(zk[, opt])

Also you can create Invoker instance by URIs of providers directly.

*Arguments*

* zk {*Client|String|Array*} - The ZD instance or the URIs of providers.
* opt {*Object*} - An object to set the instance options. Currently available options are:

* `path` {*String*} - Path of service.
* `dubbo` {*String*} - Dubbo version information.

*The other content is same as the `opt` in [getInvoker(path[, opt])](#invoker-getinvokerpath-opt)*

### void excute(method, args, cb)
*Example*

```javascript
var invoker = new ZD.Invoker(
'dubbo://127.0.0.1:20880/com.demo.DemoService?interface=com.demo.DemoService&methods=sayHello',
{ version: '0.0.0' }
);
```

----

#### void excute(method, args[, cb])

*Arguments*

* method {*String*} - Method to excute.
* args {*Array*} - Argument list.
* `[`cb(err, data)`]` {*Function*} - The data is the returned value. When the cb is undefined, the function return a Promise instance.
* cb(err, data) {*Function*} - The data is the returned value. When the cb is undefined, the function return a Promise instance.

*Example*

Expand Down Expand Up @@ -174,12 +207,6 @@ invoker.excute(method, [arg1])

----

## Thanks

Thank
[node-zookeeper-dubbo](https://github.com/p412726700/node-zookeeper-dubbo)
provide reference and thoughts.

## License

Licensed under the
Expand Down
119 changes: 39 additions & 80 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@

'use strict';

// require core modules
var url = require('url');
var querystring = require('querystring');

// require thirdpart modules
var zookeeper = require('node-zookeeper-client');

// require custom modules
var Invoker = require('./lib/invoker');

/**
* Expose `ZD`.
*
* @type {ZD}
*/
module.exports = ZD;

/**
* Constructor of ZD.
*
* @param {Object} conf
* @param {String|Object} conf // A String of host:port pairs or an object to set options.
* {
* dubbo: String // dubbo version information
* dubbo: String // Dubbo version information.
*
* // The following content could reference:
* // https://github.com/alexguan/node-zookeeper-client#client-createclientconnectionstring-options
Expand All @@ -40,17 +29,14 @@ module.exports = ZD;
* @constructor
*/
function ZD(conf) {
if (!(this instanceof ZD)) return new ZD(conf);
conf = conf || {};
this._dubbo = conf.dubbo;
this._conn = conf.conn;
this._client = this.client = zookeeper.createClient(this._conn, {
sessionTimeout: conf.sessionTimeout,
spinDelay: conf.spinDelay,
retries: conf.retries
});

this._cache = this.cache = {};
if (!(this instanceof ZD)) return new ZD(conf);
var config = ('string' === typeof conf) ? { conn: conf } : (conf || {});
this.dubbo = config.dubbo;
this.client = zookeeper.createClient(config.conn, {
sessionTimeout: config.sessionTimeout,
spinDelay: config.spinDelay,
retries: config.retries
});
}

/**
Expand All @@ -59,10 +45,10 @@ function ZD(conf) {
* @public
*/
ZD.prototype.connect = function () {
if (!this._client || !this._client.connect) {
return;
}
this._client.connect();
if (!this.client || !this.client.connect) {
return;
}
this.client.connect();
};

/**
Expand All @@ -71,10 +57,10 @@ ZD.prototype.connect = function () {
* @public
*/
ZD.prototype.close = function () {
if (!this._client || !this._client.close) {
return;
}
this._client.close();
if (!this.client || !this.client.close) {
return;
}
this.client.close();
};

/**
Expand All @@ -85,62 +71,35 @@ ZD.prototype.close = function () {
* {
* version: String
* timeout: Number
* poolMax: Number
* poolMin: Number
* }
* @returns {Invoker}
* @public
*/
ZD.prototype.getInvoker = function (path, opt) {
opt = opt || {};
return new Invoker(this, {
path: path,
timeout: opt.timeout,
version: (opt.version || '0.0.0').toUpperCase()
});
var self = this;
var option = opt || {};
return new Invoker(self.client, {
path: path,
dubbo: self.dubbo,
timeout: option.timeout,
version: option.version,
poolMax: option.poolMax,
poolMin: option.poolMin
});
};

/**
* Get a provider from the registry.
* Expose `Invoker`. To create a Invoker with URIs directly.
*
* @param {String} path
* @param {String} version
* @param {Function} cb
* @returns {*}
* @public
* @type {Invoker}
*/
ZD.prototype.getProvider = function (path, version, cb) {
var self = this;
var _path = '/dubbo/' + path + '/providers';
return self._client.getChildren(_path, function (err, children) {
var child, parsed, provider, i, l;
if (err) {
return cb(err);
}
ZD.Invoker = Invoker;

if (children && !children.length) {
return cb('Can\'t find children from the node: ' + _path +
' ,please check the path!');
}

try {
for (i = 0, l = children.length; i < l; i++) {
child = querystring.parse(decodeURIComponent(children[i]));
//console.log(child);
if (child.version === version) {
break;
}
}

parsed = url.parse(Object.keys(child)[0]);
provider = {
host: parsed.hostname,
port: parsed.port,
methods: child.methods.split(',')
};
self._cache[path] = provider;
} catch (err) {
return cb(err);
}

return cb(false, provider);
});
};
/**
* Expose `ZD`.
*
* @type {ZD}
*/
module.exports = ZD;
Loading

0 comments on commit e2e8fd9

Please sign in to comment.