Node.JS開源:thrift - 基于Nodejs 下的thrift 應用 ,測試與java 能進行互相調用
thriftNodejs
基于Nodejs 下的thrift 應用 ,測試與java 能進行互相調用
版本號 0.10.0
客戶端
var thrift = require('thrift');
var IThriftInfoTestService = require("./gen-nodejs/IThriftInfoTestService.js");
var IThriftInfoTestService_Types = require('./gen-nodejs/IThriftInfoTestService_Types');
var IThriftTestService = require("./gen-nodejs/IThriftTestService.js");
var IThriftTestService_Types = require("./gen-nodejs/IThriftTestService_Types");
var transport = thrift.TFramedTransport;
var protocol = thrift.TBinaryProtocol;
var connection = thrift.createConnection("localhost", 29999, {
transport : transport,
protocol : protocol
});
connection.on('error', function(err) {
assert(false, err);
});
// Create a Calculator client with the connection
// var client = thrift.createClient(IThriftInfoTestService, connection);
// 多方法 multiplexed_protocol 的調用
var multiplexer =new thrift.Multiplexer();
var map ={};
map.name = '我是莊杰森';
map.content = '我是node js!!!';
//服務一方法一調用
var thriftInfoTestServiceClient = multiplexer.createClient('com.java.core.rpc.thrift.service.IThriftInfoTestService',IThriftInfoTestService,connection);
thriftInfoTestServiceClient.showInfoData("我是從 nodejs 過來的! showInfoData()方法! 。。",true, map, function(err, response) {
console.log("showInfoData : " + response);
});
var map2 ={};
map2.name = '我是莊杰森';
map2.content = '我是node js!!!showThriftResult() 方法!!。';
//服務二方法二調用
var thriftTestService = multiplexer.createClient('com.java.core.rpc.thrift.service.IThriftTestService',IThriftTestService,connection);
thriftTestService.showThriftResult("我是從 nodejs 過來的! showThriftResult() 方法 !。。。。",true, map2, function(err, response) {
console.log("showThriftResult : " + response);
//關閉連接
connection.end();
});</code></pre>
服務端
var thrift = require("thrift");
var IThriftInfoTestService = require("./gen-nodejs/IThriftInfoTestService.js");
var IThriftInfoTestService_Types = require('./gen-nodejs/IThriftInfoTestService_Types');
var IThriftTestService = require("./gen-nodejs/IThriftTestService.js");
var IThriftTestService_Types = require("./gen-nodejs/IThriftTestService_Types");
var data = {};
//實現方法
var thriftInfoTestService = new IThriftInfoTestService.Processor({
'showInfoData' : function(name, b2, m2, callback){
console.log('showInfoData....');
callback(null,"i am node thriftInfoTestService !!! ");
}
});
//實現方法
var thriftTestService = new IThriftTestService.Processor({
'showThriftResult' : function(name, b2, m2, callback){
console.log('showThriftResult....');
callback(null,"i am node thriftTestService !!! ");
}
});
//多服務注冊
var processor = new thrift.MultiplexedProcessor();
processor.registerProcessor('com.java.core.rpc.thrift.service.IThriftTestService',thriftTestService);
processor.registerProcessor('com.java.core.rpc.thrift.service.IThriftInfoTestService',thriftInfoTestService);
// var framedTransport = new thrift.TFramedTransport();
//TFramedTransport 一定要這個避免報錯
var transport = thrift.TFramedTransport;
var protocol = thrift.TBinaryProtocol;
var options = {
transport : transport,
protocol : protocol
};
var server = thrift.createMultiplexServer(processor,options);
server.listen(29999);
console.log('start listening....');</code></pre>