sockets - 何时以及为什么使用 Thrift 比使用简单的套接字/网络编程更好?

标签 sockets network-programming rpc thrift

我想在一个项目中使用 Thrift,但我需要很多理由说明它比仅使用通过网络发送的简单套接字和结构更好。我试图提出的每一个论点都归结为一个事实,即简单的套接字编程对于小型应用程序来说更容易和更快地实现。显然是否使用它在很大程度上取决于项目,但我的案例尤其是 c/c++ 中的 linux 应用程序与 Windows 服务应用程序(c++ 或 c#)交谈。我正在尝试编译使用节俭的利弊列表(主要是利弊),而不仅仅是通过套接字的简单发送功能。这是我迄今为止收集的关于节俭的信息(我承认其中一些可能不准确,或者我可能需要更多的解释/澄清)(我在 http://martin.kleppmann.com/2012/12/05/schema-evolution-in-avro-protocol-buffers-thrift.html 上找到了很多这样的信息):

Another RPC and serialization framework option, Thrift consists of a library for handling of distributed object communication/RPC and serialization, and a compiler. Thrift is a free, open source framework under the Apache License 2.0, which allows the user of the software the freedom to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software, under the terms of the license, without concern for royalties. In addition it can be combined with GPL 3.0 licensed content as long as the license of the combined work is also GPL 3.0. Thrift is a rather new framework, that grew out of the RPC framework developed by Facebook and then released as open source. It has existed since around 2008 and has a thriving community of users.

Thrift by default uses the industry standard JSON or other built in protocol choices for defining data types and protocols; however it also supports custom alternate interface description languages. Thrift libraries themselves can be compiled in multiple languages (platform independent) and the Thrift compiler can auto-generate classes, server, client, and stub/skeleton code from interface/config files in multiple languages. Thrift has blocking/nonblocking server options to choose from. Limited networking code would need to be written if Thrift is used, since it is all included. IDL files would need to be written for defining the packet data/commands for serialization/deserialization.

Thrift supports the following primitive types:

  • bool: A Boolean value (true or false)
  • byte: An 8-bit signed integer
  • i16: A 16-bit signed integer
  • i32: A 32-bit signed integer
  • i64: A 64-bit signed integer
  • double: A 64-bit floating point number
  • string: A text string encoded using UTF-8 encoding

and the following complex types:

  • records
  • structs
  • containers
  • exceptions
  • services

Thrift supports long term schema evolution, which allows for modifications to the schema (such as new fields and data types/attributes) without losing any backwards compatibility between older interface files. Client/server logic of course still needs to be modified to support new features from schema changes. Messages/commands are tagged with an identifier so receiving ends can match them to the schema. An extra step in compilation is necessary to compile stub/skeleton code for handling the messages defined in an interface file.

Using Thrift gets backwards compatibility between schema changes (allowing for software updates without breaking older fielded systems), platform independency, and drop-in RPC and server without any code needed to be written other than how to handle commands/data sent back and forth between the client and server.

最佳答案

Thrift 是好的,但当然不适用于所有类型的项目。好处:

  • thrift 优于异步套接字编程:在异步引擎中只有一种工作方式,在 thrift 中您可以选择最适合您的一种(单线程、并行或异步)
  • thrift 是一个框架。您编写的样板代码更少。你没有实现传输、协议(protocol)等。
  • thrift 基于定义,形成了一种自我文档。当新人加入您的团队时,这在大型项目中非常有用。查看定义可以让您了解系统内部的内容
  • Thrift 支持 20 多种语言。这意味着如果您已经有一个 thrift 定义文件,那么您已经提供了所有语言的客户端。此外,如果您想更改服务器的平台(例如 c++ -> java 或其他),那么这对您的所有基础架构都是自动透明的(工作量要少得多)。

  • 缺点:
  • thrift 比 google 的 protobuffers 稍慢(基准表明它是 10%,关于 TBinary 或 TCompact 协议(protocol))
  • thrift 永远不会击败专门的异步引擎,例如 boost::asio 或 python 的 twisted。它不是为这个目的而设计的。

  • 总结 - 如果您需要通过 api 提供具有复杂功能的服务,thrift 是一个不错的选择。此外,如果您需要在不同平台上拥有客户,thrift 是一个不错的选择。但是如果你需要非凡的性能,你可以自己和 protobuf 进行比较。如果您需要提供非常简单的结构(计算可能很复杂,但传输本身很容易),那么请考虑 boost::asio、twisted 或类似的东西。

    你可以看看my presentation about thrift ,有一节是关于好处和限制的。

    关于sockets - 何时以及为什么使用 Thrift 比使用简单的套接字/网络编程更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21266186/

    相关文章:

    c++ - 如何控制socket速率?

    c - 绑定(bind)失败 : : Address family not supported by protocol family - C

    iphone - 如何在 iOS 中将 CFReadStreamRead 设置为超时?

    用于通过 RPC 传递匿名函数的 GobEncoder

    node.js - 从 Node.JS 调用 Haskell

    c - 如何检测 TCP 套接字断开连接(使用 C Berkeley 套接字)

    sockets - 套接字编程资源

    node.js - express.io VS express + socket.io 的效用是什么?

    c# - Windows Phone 8.1不发送UDP广播

    java - RPC 模式问题