Thrift 调试:我的 finagle 服务器出现问题

Thrift Debugging: Problems with my finagle server

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

    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)
      }
    }

这是我的 Thrift 文件:

    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' 启动了服务器。控制台的输出:

[信息] 运行 ThriftServer 2016 年 5 月 25 日 4:18:39 上午 com.twitter.finagle.Init$$anonfun$1 申请$mcV$sp 信息:Finagle 版本 6.34.0 (rev=44f444f606b10582c2da8d5770b7879ddd961211) 构建于 20160310-155158

此时一切看起来都很好。我在 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);
        }
    });

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

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

查看 node.js Thrift 库代码,似乎 TBufferedTransport 是默认值,您可能需要 TFramedTransport

node.js Thrift tutorial page 上,他们展示了如何使用覆盖选项初始化连接:

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 并查看是否有帮助。

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

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

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

    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]);
        }
    });