在Apache Thrift中跨多个文件定义服务

时间:2014-01-20 23:06:25

标签: thrift

我想在Apache Thrift中包装一个大型C ++库。 界面可能有几十个或几百个函数,所以把整个东西放在一个文件中会很麻烦。 所以,我想将服务定义分散到多个文件中。 有没有一些支持的方法来做到这一点?

顺便说一下,Thrift的单一遗产在这里是不够的。 多重继承可行,但是Thrift不支持它。

1 个答案:

答案 0 :(得分:1)

是的,您可以使用#include在多个文件中传播定义。 Thrift教程包含一个示例:(a)将一个IDL包含到另一个IDL中,以及(b)从另一个IDL继承服务。

文件shared.thrift:

struct SharedStruct {
  1: i32 key
  2: string value
}

service SharedService {
  SharedStruct getStruct(1: i32 key)
}

文件tutorial.thrift(仅摘录):

/**
 * Thrift files can reference other Thrift files to include common struct
 * and service definitions. These are found using the current path, or by
 * searching relative to any paths specified with the -I compiler flag.
 *
 * Included objects are accessed using the name of the .thrift file as a
 * prefix. i.e. shared.SharedObject
 */
include "shared.thrift"

// some more code

exception InvalidOperation {
  1: i32 what,
  2: string why
}

/**
 * Ahh, now onto the cool part, defining a service. Services just need a name
 * and can optionally inherit from another service using the extends keyword.
 */
service Calculator extends shared.SharedService {
   void ping(),
   i32 add(1:i32 num1, 2:i32 num2),
   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
   oneway void zip()
}

进一步的建议

将巨大的界面拆分为较小的主题分组界面可能是个好主意。使用Thrift 0.9.0引入的Multiplexer很容易,让多个服务共享相同的传输。今天Thift支持的语言的很大一部分已经支持多路复用器,有关详细信息,请参阅链接的JIRA票证。

相关问题