0

我有一个简单的 Thrift 服务器,在 scala 中用 finagle 实现:

    import com.twitter.util.{ Await, Future }
    import com.jakiku.thriftscala.{ RealTimeDatabasePageImpressions, PageImpressions }
    import com.twitter.finagle.Thrift
    import com.twitter.finagle.thrift.ThriftServerFramedCodec
    import com.twitter.finagle.builder.{ ServerBuilder, Server }

    object ThriftServer {

      def main(args: Array[String]) {
        val server = Thrift.serveIface("localhost:9090", new RealTimeDatabasePageImpressions[Future] {
          def getByTrackIdAndDay(trackId: Int, day: Int) = {
            Future(Seq(PageImpressions.apply(123, 3, 4,  3,  2000L, 2000L)))
          }
        })
        Await.ready(server)
      }
    }

这是我的节俭文件:

    namespace   java com.jakiku.thriftjava
    #@namespace scala com.jakiku.thriftscala

    typedef i64 long
    typedef i32 int

    struct PageImpressionsSum{
        1: required int         trackId;
        2: required int         day;
        3: required int         hour;
        4: required int         minute;
        5: required string      pageId;
        6: required long        uniqueImpressions;
        7: required long        sumImpressions;
    }

    struct PageImpressions{
        1: required int         trackId;
        2: required int         day;
        3: required int         hour;
        4: required int         minute;
        6: required long        uniqueImpressions;
        7: required long        sumImpressions;
    }

    service RealTimeDatabase_pageImpressions{
      list<PageImpressions> getByTrackIdAndDay(1:int trackId, 2:int day);
    }

    service RealTimeDatabase_pageImpressionsSum {
      list<PageImpressionsSum> getByTrackIdAndDay(1:int trackId, 2:int day);
    }

我用 scrooge 生成了 scala thrift 类。添加了 thriftlib、scrooge-core、scrooge-generator 等作为依赖项。我还添加了 scrooge sbt 插件:

 addSbtPlugin("com.twitter" % "scrooge-sbt-plugin" % "4.5.0")

我用 sudo sbt 'run' 启动了服务器。控制台的输出:

[信息] Running ThriftServer Mai 25, 2016 4:18:39 AM com.twitter.finagle.Init$$anonfun$1 apply$mcV$sp 信息:Finagle 版本 6.34.0 (rev=44f444f606b10582c2da8d5770b7879ddd961211) 建于 20160310-155851

在这一刻,一切看起来都很好。我在nodejs中测试了服务器。不工作。我还使用了 thrift python 测试套件。每次检查都会超时。

这是 nodejs 客户端:

    var thrift = require('thrift');
    var Realtime_pageImpressions = require('./gen-nodejs/RealTimeDatabase_pageImpressions.js');
    var ttypes = require('./gen-nodejs/RealTimeDatabase_types.js');


    var connection = thrift.createConnection("localhost", 9090);

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

    var client = thrift.createClient(Realtime_pageImpressions, connection);

    client.getByTrackIdAndDay(123124, 4, function(err, response) {
        if (err) {
        console.error(err);
        } else {
        console.log(response);
        }
    });

我真的不知道我做错了什么。

4

2 回答 2

1

不确定这是否是问题(特别是因为我不熟悉 scrooge 或 finagle),但看起来您在服务器上使用了 Framed 传输,而可能不在客户端上。

查看 node.js Thrift 库代码,它似乎TBufferedTransport是默认的,而您可能想要TFramedTransport

node.js Thrift 教程页面上,它们展示了如何使用覆盖选项初始化连接:

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

transport = ThriftTransports.TBufferedTransport()
protocol = ThriftProtocols.TBinaryProtocol()

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

您可以尝试像上面那样实现您的 node.js 客户端,除了替换为TFramedTransport并查看是否有帮助。

于 2016-05-26T00:48:57.693 回答
0

好的,我修好了。两件事:@BCG给了我一个提示。它是 TFramedTransport。但是 Apache Docu 有点太旧了。

 var ThriftTransports = require('thrift/transport');
 var ThriftProtocols = require('thrift/protocol');

thrift/transport 和 thrift/protocol 不再存在。你只需要 Thrift。我还删除了函数执行的两个括号。

    var thrift = require('thrift');
    var Int64 = require('node-int64');
    var Realtime_pageImpressions = require('./gen-nodejs/RealTimeDatabase_pageImpressions.js');
    var ttypes = require('./gen-nodejs/RealTimeDatabase_types.js');

    transport   = thrift.TFramedTransport;
    protocol    = thrift.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(Realtime_pageImpressions, connection);

    client.getByTrackIdAndDay(123124, 4, function(err, response) {
        if (err) {
        console.error(err);
        } else {
        console.log(response);
        console.log(response[0].uniqueImpressions[0]);
        }
    });
于 2016-05-28T15:30:05.440 回答