鍍金池/ 問(wèn)答/Java  PHP  HTML/ nodejs怎么調(diào)用thrift接口

nodejs怎么調(diào)用thrift接口

nodejs怎么像調(diào)http接口一樣調(diào)用thrift接口

回答
編輯回答
扯不斷

可以看下thrift的官網(wǎng)教程
http://thrift.apache.org/tuto...
同時(shí)可以配合例子適當(dāng)?shù)乜纯丛创a

2017年12月6日 17:40
編輯回答
女流氓

需要用工具生成代碼后調(diào)用,生成方法參見(jiàn)apache thrift 的get started部分

然后引用生成的代碼到工程,調(diào)用示例:

var Calculator = require('../gen-nodejs/Calculator');
var ttypes = require('../gen-nodejs/tutorial_types');
var wrapper = require('co-thrift');
var thrift = require('thrift');

var connection = thrift.createConnection('localhost', 9090, {
    transport: thrift.TBufferedTransport(),
    protocol: thrift.TBinaryProtocol()
});

connection.on('error', function(err) {
    console.log(err);
});

var client = thrift.createClient(wrapper(Calculator.Client), connection);

module.exports.add = function* (a, b) {
    yield client.add(a, b);
};

module.exports.ping = function* () {
    yield client.ping();
};

module.exports.establish = function *establish (name) {
    yield client.establishStream(name, '', 9090);
}

gen-nodejs 為生成的代碼

2018年9月20日 11:13