Thrift压缩 2012年9月25日 Bise 发表评论 阅读评论
关于Thrift的压缩有协议层面的压缩和传输时的压缩两种。
协议层面的压缩需要用到TCompactProtocol,而传输压缩需要用到TZlibTransport(其实就是采用zlib压缩)。
先给出接口定义文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 struct request_ad { 1: byte mode, 2: byte device, 3: byte loctype = 1, 4: string location, 5: i32 adbar_id, 6: i32 time, 7: i64 pangu_id, 8: string page_id, 9: string machine_id, 10: string agent }
struct result_ad { 1: i32 status, 2: string debug, 3: string result }
exception error_ad { 1: i32 errno; 2: string error }
service Ad { result_ad get_result(1: request_ad req) throws (1: error_ad err) } 协议压缩
编译连接选项是-lthrift,协议为TCompactProtoco。
在服务端和客服端必须先包含头文件“#include <config.h>”,否则会报错“Unable to determine the behavior of a signed right shift”
服务端代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #include <arpa/inet.h> #include "block/Ad.h" #include <config.h> #include <protocol/TCompactProtocol.h> #include <server/TSimpleServer.h> #include <transport/TServerSocket.h> #include <transport/TBufferTransports.h>
using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server;
using boost::shared_ptr;
class AdHandler : virtual public AdIf { public: AdHandler() { // Your initialization goes here }
void get_result(result_ad& _return, const request_ad& req) { // Your implementation goes here //printf("get_result\n"); _return.debug.resize(1024, 'm'); _return.result.resize(8192, 'n'); }
};
int main(int argc, char **argv) { int port = 9090; shared_ptr<AdHandler> handler(new AdHandler()); shared_ptr<TProcessor> processor(new AdProcessor(handler)); shared_ptr<TServerTransport> serverTransport( new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory( new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory( new TCompactProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }
客服端代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <arpa/inet.h> #include "block/Ad.h" #include <config.h> #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <protocol/TCompactProtocol.h> #include <sys/time.h> #include <unistd.h> using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport;
using boost::shared_ptr;
int main(int argc, char **argv) { shared_ptr<TSocket> socket( new TSocket("127.0.0.1", 9090)); shared_ptr<TTransport> transport( new TBufferedTransport(socket)); shared_ptr<TProtocol> protocol( new TCompactProtocol(transport)); AdClient client(protocol); transport->open(); request_ad req; result_ad res; req.page_id.resize(256, 'a'); req.machine_id.resize(256, 'b'); req.agent.resize(512, 'c'); client.get_result(res, req); transport->close(); return 0; } 传输压缩
编译连接选项是-lthrift -lthriftz 在服务端需要自己实现一个TZlibTransport 的工厂类 使用TZlibTransport时,为了提高效率,最好用TBufferedTransport来装饰下(效率相差比较大)。 服务端代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include <arpa/inet.h> #include "block/Ad.h" #include <protocol/TBinaryProtocol.h> #include <server/TSimpleServer.h> #include <transport/TServerSocket.h> #include <transport/TBufferTransports.h> #include <transport/TZlibTransport.h>
using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server;
using boost::shared_ptr; class AdHandler : virtual public AdIf { public: AdHandler() { // Your initialization goes here }
void get_result(result_ad& _return, const request_ad& req) { // Your implementation goes here printf("get_result\n"); _return.debug.resize(1024, 'm'); _return.result.resize(8192, 'n'); }
};
class TZlibTransportFactory : public TTransportFactory { public: TZlibTransportFactory() {}
virtual ~TZlibTransportFactory() {}
/** * Wraps the transport into a framed one. */ virtual boost::shared_ptr<TTransport> getTransport(shared_ptr<TTransport> trans) { return shared_ptr<TTransport>( new TBufferedTransport(shared_ptr<TTransport>( new TZlibTransport(trans)))); }
};
int main(int argc, char **argv) { int port = 9090; shared_ptr<AdHandler> handler(new AdHandler()); shared_ptr<TProcessor> processor(new AdProcessor(handler)); shared_ptr<TServerTransport> serverTransport( new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory( new TZlibTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory( new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }
客服端代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <arpa/inet.h> #include "block/Ad.h" #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <transport/TZlibTransport.h> #include <protocol/TBinaryProtocol.h>
using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport;
using boost::shared_ptr;
int main(int argc, char **argv) { shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 9090)); shared_ptr<TZlibTransport> zlibtransport( new TZlibTransport(socket)); shared_ptr<TTransport> transport( new TBufferedTransport(zlibtransport)); shared_ptr<TProtocol> protocol( new TBinaryProtocol(transport)); AdClient client(protocol); transport->open(); request_ad req; result_ad res; req.page_id.resize(256, 'a'); req.machine_id.resize(256, 'b'); req.agent.resize(512, 'c'); client.get_result(res, req); printf("%s\n", res.debug.c_str()); client.get_result(res, req); printf("%s\n", res.debug.c_str()); transport->close(); return 0; } | |