我应该如何使用nodejs访问thrift TNonblockingServer或与TNonblockingSocket通信

时间:2015-04-11 08:42:24

标签: java node.js thrift

我正在尝试使用thrift重新关联nodejs客户端和Java服务器之间的通信

Thrift提供已实现的各种java服务器 ·TSimpleServer ·TNonblockingServer ·THsHaServer ·TThreadedSelectorServer ·TThreadPoolServer

我已成功使用nodejs客户端调用TTSimpleServer和TThreadPoolServer中的函数,这两个函数都使用TServerSocket初始化

TServerSocket serverTransport = new TServerSocket(9090);

CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
                    new CalculatorImpl());

            TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport).processor(processor);
            args.maxWorkerThreads(100);

            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
                    serverTransport).processor(processor));

            System.out.println("Starting server on port 9090 ...");
            server.serve();

但是当我尝试使用TNonblockingServer,TThreadedSelectorServer和THaHsServer时,我在nodejs客户端出现了错误跟踪错误

{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

我意识到这可能是TNonblockingSocket造成的,有没有什么方法可以使用nodejs与TNonblockingSocket进行通信

try
        {
            TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(
                    9090);

            CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
                    new CalculatorImpl());

            TServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(
                    serverTransport).processor(processor));
            System.out.println("Starting server on port 9090 ...");
            server.serve();
        } catch (TTransportException e)
        {
            e.printStackTrace();
        }

我的nodejs客户端代码如下

 var thrift = require('thrift');

var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var Calculator = require('./gen-nodejs/CalculatorService.js');
var ttypes = require('./gen-nodejs/tutorial_types');

transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();

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

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

// Create a Calculator client with the connection
var client = thrift.createClient(Calculator, connection);

client.send_print(1,1, function(err, response) {
      console.log("send_print result:" + response);
    });

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚找到答案 这个问题确实是由TTFramedTransport引起的 即使我在我的代码中使用它,我也发现了

transport = ThriftTransports.TFramedTransport();

传输似乎未成功分配TFramedTransport

我只是这样试试

transport = require('./node_modules/thrift/framed_transport.js')

成功了

我不熟悉node.js,也许我想出为什么会发生这种情况