我应该如何使用 nodejs 访问 thrift TNonblockingServer 或与 TNonblockingSocket 通信
How should I use nodejs visit thrift TNonblockingServer or to communicate with TNonblockingSocket
我正在尝试使用 thrift 来重新实现 nodejs 客户端和 Java 服务器之间的通信
Thrift 提供了不同种类的 java 服务器,这些服务器已经实现
· 简单服务器
· TNonblockingServer
· THsHa服务器
· 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);
});
嗯,我刚找到答案
这个问题确实是由 TTFramedTransport 引起的
我发现即使我在我的代码中使用它
transport = ThriftTransports.TFramedTransport();
传输似乎没有成功分配 TFramedTransport
我就这样试试
transport = require('./node_modules/thrift/framed_transport.js')
成功了
我不熟悉 node.js ,也许我稍后会弄清楚为什么会这样
我正在尝试使用 thrift 来重新实现 nodejs 客户端和 Java 服务器之间的通信
Thrift 提供了不同种类的 java 服务器,这些服务器已经实现 · 简单服务器 · TNonblockingServer · THsHa服务器 · 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);
});
嗯,我刚找到答案 这个问题确实是由 TTFramedTransport 引起的 我发现即使我在我的代码中使用它
transport = ThriftTransports.TFramedTransport();
传输似乎没有成功分配 TFramedTransport
我就这样试试
transport = require('./node_modules/thrift/framed_transport.js')
成功了
我不熟悉 node.js ,也许我稍后会弄清楚为什么会这样