【问题标题】:Apache Thrift with nodejs exampleApache Thrift 与 nodejs 示例
【发布时间】:2012-10-31 22:36:25
【问题描述】:

我正在尝试使用 Apache Thrift 在以不同语言实现的应用程序之间传递消息。它不一定用作 RPC,而更多用于序列化/反序列化消息。 一个应用程序位于 node.js 中。我试图弄清楚 Apache thrift 如何与 node.js 一起工作,但我找不到太多的文档和示例,除了一个关于 Cassandra 的小文档和示例: https://github.com/apache/thrift/tree/trunk/lib/nodejs

同样,我不需要在 .thrift 文件中声明任何过程,我只需要序列化一个简单的数据结构,例如:

struct Notification {
   1: string subject,
   2: string message
 }

谁能帮我举个例子?

【问题讨论】:

    标签: node.js serialization deserialization thrift


    【解决方案1】:

    在浪费了很多时间之后,我终于找到了这个问题的答案,只是通过查看 nodejs 的库。

    //SERIALIZATION:
    var buffer = new Buffer(notification);
    var transport = new thrift.TFramedTransport(buffer);
    var binaryProt = new thrift.TBinaryProtocol(transport);
    notification.write(binaryProt);
    

    此时可以在transport.outBuffers字段中找到字节数组:

    var byteArray = transport.outBuffers;
    

    对于反序列化:

    var tTransport = new thrift.TFramedTransport(byteArray);
    var tProtocol = new thrift.TBinaryProtocol(tTransport);
    var receivedNotif = new notification_type.Notification();
    receivedNotif.read(tProtocol);
    

    还需要将以下几行添加到 nodejs 库中的 index.js 文件中以进行节俭:

    exports.TFramedTransport = require('./transport').TFramedTransport;
    exports.TBufferedTransport = require('./transport').TBufferedTransport;
    exports.TBinaryProtocol = require('./protocol').TBinaryProtocol;
    

    另外,nodejs 库中也至少存在一个错误。

    【讨论】:

      【解决方案2】:

      上面的答案是错误的,因为它试图直接使用outBuffers,它是一个缓冲区数组。这是一个使用 thrift 和 nodejs 的工作示例:

      var util = require('util');
      var thrift = require('thrift');
      
      var Notification = require('./gen-nodejs/notification_types.js').Notification;
      
      var TFramedTransport = require('thrift/lib/thrift/transport').TFramedTransport;
      var TBufferedTransport = require('thrift/lib/thrift/transport').TBufferedTransport;
      var TBinaryProtocol = require('thrift/lib/thrift/protocol').TBinaryProtocol;
      
      var transport = new TFramedTransport(null, function(byteArray) {
        // Flush puts a 4-byte header, which needs to be parsed/sliced.
        byteArray = byteArray.slice(4);
      
        // DESERIALIZATION:
        var tTransport = new TFramedTransport(byteArray);
        var tProtocol = new TBinaryProtocol(tTransport);
        var receivedNotification = new Notification();
        receivedUser.read(tProtocol);
      
        console.log(util.inspect(receivedNotification, false, null));
      });
      
      var binaryProt = new TBinaryProtocol(transport);
      
      // SERIALIZATION:
      var notification = new Notification({"subject":"AAAA"});
      console.log(util.inspect(notification, false, null));
      notification.write(binaryProt);
      transport.flush();
      

      【讨论】:

      • 抱歉,您在同一个脚本中反序列化了创建的相同数据。但是如果使用一些中间存储( rabbitmq )究竟要在 rabbit 中存储什么数据?切出缓冲区?还是每次都调用flush并在回调中保存数据?并且每次都对byteArray使用切片并不是一个好主意,因为它不是复制数据,而是引用它。
      【解决方案3】:

      DigitalGhost 是对的,前面的例子是错误的。 恕我直言,outBuffers 是传输类的私有属性,不应访问。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 2015-08-10
        • 2012-10-04
        • 2013-04-08
        • 2020-12-02
        • 2021-10-05
        相关资源
        最近更新 更多