0.1.4 • Published 4 years ago

zen-dubbo v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

zoodubbo

Build Status Coverage Status NPM Downloads NPM Version License

A Javascript module for Node.js to connect Dubbo service by node-zookeeper-client.

Features

  • 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.

Installation

You can install it using npm:

$ npm install zoodubbo

Example

var ZD = require('zoodubbo');
var zd = new ZD({
    // config the addresses of zookeeper
    conn: 'localhost:2181'
});

// connect to zookeeper
zd.connect();

// get a invoker with a service path
var invoker = zd.getInvoker('com.demo.Service');

// excute a method with parameters
var method = 'getUserByID';
var arg1={$class:'int',$:123};
invoker.excute(method, [arg1], function (err, data) {
    if (err) {
        console.log(err);
        return;
    }
    console.log(data)
});

Documentation

new ZD(conf)

Arguments

  • conf {Object} - An object to set the instance options. Currently available options are:

Example

var zd = new ZD({
    conn: 'localhost:2181,localhost:2182',
    dubbo: '2.5.3'
});

client

The client instance created by node-zookeeper-client. To listen event such as follows:

zd.client.on('connected', function connect() {
    console.log('zookeeper client connected!');
});

The list of events could reference: https://github.com/alexguan/node-zookeeper-client#state


void connect()

Connect to the Dubbo Registration Center by node-zookeeper-client. Equivalent to the following code:

zd.connect();
// just like
zd.client.connect();

void close()

Close the connection of node-zookeeper-client. Equivalent to the following code:

zd.close();
// just like
zd.client.close();

Invoker getInvoker(path, opt)

Arguments

  • path {String} - Path of service.
  • opt {Object} - An object to set the Invoker options. Currently available options are:

    • version {String} - Service version information.
    • timeout {Number} - The timeout (in milliseconds) to excute.

Example

var invoker = zd.getInvoker('com.demo.Service', {
    version: '0.0.0'
});

Invoker

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.

Example

var method = 'getUserByID';
var arg1={$class:'int',$:123};

// use callback
invoker.excute(method, [arg1], function (err, data) {
    if (err) {
        console.log(err);
        return;
    }
    console.log(data)
});

// or return a Promise instance
invoker.excute(method, [arg1])
    .then(function(data){
        console.log(data);
    }).catch(function(err){
        console.log(err);
    });

Thanks

Thank node-zookeeper-dubbo provide reference and thoughts.

License

Licensed under the MIT license.