服务端代码 public class ThriftServer {
private final static int PORT_TMULTIPLEXED = 9980;
private final static int PORT_DEFAULT = 9981;
public static void main(String[] args) {
new Thread(new TypeTmultiplexed()).start();
new Thread(new TypeDefault()).start();
}
static class TypeTmultiplexed implements Runnable{
public void run() {
try {
LogCvt.info("thrift service (Tmultiplexed) init complete port: " + PORT_TMULTIPLEXED);
TMultiplexedProcessor processor = new TMultiplexedProcessor();
TServerTransport t = new TServerSocket(PORT_TMULTIPLEXED);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(t).processor(processor));
processor.registerProcessor(CaseThriftService.class.getName(), processor);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
static class TypeDefault implements Runnable{
public void run(){
try {
LogCvt.info("thrift service (Default) init complete port: " + PORT_DEFAULT);
TServerSocket serverTransport = new TServerSocket(PORT_DEFAULT);
CaseThriftService.Processor<?> processor = new Processor<>(new CaseThriftServiceImpl());
Factory portFactory = new TBinaryProtocol.Factory(true, true);
Args args = new Args(serverTransport);
args.processor(processor);
args.protocolFactory(portFactory);
TServer server = new TThreadPoolServer(args);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
}
如上,用java启动了两个thrift的服务器分别监听着9980 9981 端口,其中9980是使用的thrift中协议为:Tmultiplexed 的方式,9980则使用的是默认的协议
服务端启动完毕…
客户端nodejs代码 var thrift = require(‘./node_modules/thrift’);
var transport = require(‘./node_modules/thrift/lib/thrift/transport’);
var service_api = require(‘./sample/gen-nodejs/CaseThriftService’); var server_type = require(‘./sample/gen-nodejs/Tserver_types’);
var connection_def = thrift.createConnection("localhost", 9981); connection_def.on('error’, function(err) { if(err != null){ console.log(err); return; } }); var client_def = thrift.createClient(service_api, connection_def); client_def.ping('connection to server’,function (err,response){ if(err != null){ console.log(err); return; } console.log('service response msg: ' + response); var case_type = new server_type.CaseVo({ argStr:’typeOf String’, argInt: 1000 }); client_def.doCase(case_type,function(err,response){ if(err != null){ console.log(err); return; } console.log('service response msg: ' + JSON.stringify(response)); }); connection_def.end(); });
使用nodejs 中thrift模块 进行通讯,首先请求9981端口 ----------------- > 通讯正常 再将端口换成9980 -----------------> 服务端异常显示错误信息
很明显服务端需要使用TMultiplex协议,但是我并不知道nodejs 中thrift 是用TMultiplex协议如何请求,不知道有没有大神解答下?