我正在使用thrift进行python服务器和java客户端之间的rpc通信。在我的thrift Handler中,我有一个函数,我想传递一个thrift结构。对于基本数据类型传递,我可以直接执行:
def assignValue(self, variable, value):
但现在而不是变量(字符串类型)我想传递struct。我怎么能这样做?
PS:我是节俭的新手,我正在关注节俭的官方documentation,如果还有其他任何可能有用的文件,请随意传递。谢谢!struct MyStruct { 1:string var1; 2:string var2; }
答案 0 :(得分:2)
这是一个在Thrift IDL中传递结构的简单示例:
https://github.com/RandyAbernethy/ThriftBook/blob/master/part1/arch/halibut.thrift
struct Date {
1: i16 year
2: i16 month
3: i16 day
}
service HalibutTracking {
i32 get_catch_in_pounds_today()
i32 get_catch_in_pounds_by_date(1: Date dt, 2: double tm)
}
这里有一个Java客户端和服务器示例:
https://github.com/RandyAbernethy/ThriftBook/tree/master/part3/ws/thrift
它显示返回一个结构(但传递一个很容易推断)。在Python和Java的ThriftBook仓库中也有很多例子(所有来自程序员的Apache Thrift指南):
示例struct return thrift idl看起来像:
struct TradeReport {
1: string symbol,
2: double price,
3: i32 size,
4: i32 seq_num
}
service TradeHistory {
TradeReport get_last_sale(1: string Symbol)
}
列表中的Java客户端如下所示:
import java.io.IOException;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.protocol.TBinaryProtocol;
public class ThriftClient {
public static void main(String[] args)
throws IOException, TException {
TSocket trans = new TSocket("localhost", 9090);
TBinaryProtocol proto = new TBinaryProtocol(trans);
TradeHistory.Client client = new TradeHistory.Client(proto);
trans.open();
for (int i = 0; i < 1000000; i++) {
TradeReport tr = client.get_last_sale("APPL");
}
trans.close();
}
}
其他IDL示例(包括几个传递结构):
https://github.com/RandyAbernethy/ThriftBook/tree/master/part2/IDL