我正在尝试使用node.js访问由Java实现的TThreadedSelectorServer
我已经能够使用TThreadPoolServer调用thrift中定义的函数,但是在使用TThreadPoolSelectorServer时我遇到了一些问题
这是我的node.js客户端代码
var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var CalculatorService = require('./gen-nodejs/CalculatorService.js');
//transport = ThriftTransports.TBufferedTransport();
transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();
//protocol = ThriftProtocols.TCompactProtocol();
//protocol = ThriftProtocols.TDebugProtocol();
var connection = thrift.createConnection("192.168.129.186", 9090, {
transport : transport,
protocol : protocol
});
connection.on('error', function(err) {
console.log(err);
});
// Create a Calculator client with the connection
var client = thrift.createClient(CalculatorService, connection);
var strList=new Array("Audi","BMW","Volvo");
var strMap={fistname:"bill",lastname:"gates",id:"1111"};
client.send_print(1,"hello,world",strList,strMap,function(err, response) {
console.log("send_print result:" + response);
});
这是我的java服务器代码
try
{
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(
9090);
CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
new CalculatorImpl());
TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(
serverTransport).processor(processor);
args.inputTransportFactory(new TFramedTransport.Factory())
.outputTransportFactory(new TFramedTransport.Factory())
.inputProtocolFactory(new TBinaryProtocol.Factory())
.outputProtocolFactory(new TBinaryProtocol.Factory());
TServer server = new TThreadedSelectorServer(args);
System.out.println("Starting server on port 9090 ...");
server.serve();
} catch (TTransportException e)
{
e.printStackTrace();
}
我能够使用java客户端成功访问此服务器
@Test
public void test() throws TException
{
TSocket socket = new TSocket("192.168.129.186", 9090);
TTransport transport = new TFramedTransport(socket);
TProtocol protocol = new TBinaryProtocol(transport);
CalculatorService.Client client = new CalculatorService.Client(protocol);
transport.open();
//socket.open();
int num = 1;
String str = "hello,world";
ArrayList<String> strList = new ArrayList<String>();
strList.add("hello,world1");
strList.add("hello,world2");
strList.add("hello,world3");
HashMap<String, String> strMap = new HashMap<String, String>();
strMap.put("hello1", "world1");
strMap.put("hello2", "world2");
strMap.put("hello3", "world3");
int result = client.send_print(num, str, strList, strMap);
//socket.close();
transport.close();
assertEquals(result, 3);
}
但是当我使用node.js客户端访问该服务器时,我遇到了错误。
服务器端的错误日志是
ERROR server.AbstractNonblockingServer $ FrameBuffer (AbstractNonblockingServer.java:read(348)) - 读取无效帧 大小-2147418111。你在客户端使用TFramedTransport吗? 侧?
但我已经在node.js代码中分配了TFramedTransport
transport = ThriftTransports.TFramedTransport();
任何帮助将不胜感激,提前致谢
答案 0 :(得分:0)
好吧,我刚刚找到答案 此问题是由TTFramedTransport引起的 即使我在我的代码中使用它,我也发现了
transport = ThriftTransports.TFramedTransport();
传输似乎未成功分配TFramedTransport
我只是这样试试
transport = require('./node_modules/thrift/framed_transport.js')
成功了
我不熟悉node.js,也许我想出为什么会发生这种情况
答案 1 :(得分:0)
http://thrift.apache.org/tutorial/nodejs中的教程是谎言或者过时,你必须这样写,才能使它工作:
var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var CalculatorService = require('./gen-nodejs/CalculatorService.js');
var connection = thrift.createConnection("192.168.129.186", 9090, {
transport : ThriftTransports.TFramedTransport,
protocol : ThriftProtocols.TBinaryProtocol
});