寒风之家 » Thrift压缩

Thrift压缩
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;
}

寒风之家 » Thrift压缩相关推荐

  1. Thrift使用教程(Java版本)

    Thrift简介 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC ...

  2. protobuf和thrift对比

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt383 数据类型 protobuf thrift protobuf thrif ...

  3. pdf压缩大小,一个简单的pdf压缩方法

    办公的时候你还在愁没有pdf压缩大小的方法?遇到过大的pdf文件,比较好的解决办法就是将其进行压缩,这样也方便自己的工作,小编经常会进行pdf压缩的操作,今天就来给大家介绍一下pdf压缩的简单方法! ...

  4. 设计数据密集型应用 第四章:编码与演化

    设计数据密集型应用 第四章:编码与演化 唯变所适 --以弗所的赫拉克利特,为柏拉图所引(公元前360年) 文章目录 设计数据密集型应用 第四章:编码与演化 编码数据的格式 术语冲突 语言特定的格式 J ...

  5. 华为实施微服务架构的五大军规

    前言 随着业务的发展,代码量的膨胀和团队成员的增加,传统单体式架构的弊端越来越凸显,严重制约了业务的快速创新和敏捷交付.为了解决传统单体架构面临的挑战,先后演进出了SOA服务化架构.RPC框架.分布式 ...

  6. 长城电脑或收购夏新电子笔记本业务

    长城电脑或收购夏新电子笔记本业务 [url]http://www.sina.com.cn[/url] 2008年03月29日 12:52  华夏时报 本报记者 罗小卫 北京报道 身陷亏损中的夏新电子( ...

  7. Netty 高性能特性

    转自:http://www.infoq.com/cn/articles/netty-high-performance/ 1. 背景 1.1. 惊人的性能数据 最近一个圈内朋友通过私信告诉我,通过使用N ...

  8. js中应用protocol buffer

    前段时间公司项目需要用到protocol buffer数据传输协议,这是什么东西,根本没接触过,好好的json干嘛不用?怀着好奇心去了解学习,最后顺利运用.下面是一些是经验,希望能帮到一些人. 首先我 ...

  9. 华为内部实施微服务架构

    随着业务的发展,代码量的膨胀和团队成员的增加,传统单体式架构的弊端越来越凸显,严重制约了业务的快速创新和敏捷交付.为了解决传统单体架构面临的挑战,先后演进出了SOA服务化架构.RPC框架.分布式服务框 ...

最新文章

  1. spring-amqp整合rabbitmq消费者配置和代码
  2. openStack工具集
  3. 面试java后端开发之后的一些感受
  4. 【深度学习】60题PyTorch简易入门指南,做技术的弄潮儿
  5. log4j在web中的使用
  6. IOS自定义UITableViewCell的高亮背景色
  7. 高性能网站架构的思考
  8. 百会ZOHO发布基于SaaS面向中小企业的CRM平台
  9. 错误解决:java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to star
  10. Oracle数据库恢复删除数据的方法
  11. Java工作流引擎学习----JBPM(一)
  12. android sim卡应用程序,Android双SIM卡API
  13. Spire.doc实现对word的操作(包括文字,表格,图片)
  14. 【metahumanUE】虚幻引擎面捕动捕调研
  15. Qt之调用FFTW3实现音频频谱(原理)
  16. javascript 0基础入门
  17. 医院录用计算机专业信息科试题,(完整word版)医院信息科考试试题及答案,推荐文档...
  18. Jmeter压力测试中的相关参数(QPS、TPS)
  19. 为程序员精心定制的对联,有没有感觉中招
  20. SharePoint Designer (FrontPage) 2007 简体中文正式版

热门文章

  1. C++ Ouput Exactly 2 Digits After Decimal Point 小数点后保留三位数字
  2. (转)《AS3 Expert》动态语言的基石:函数闭包
  3. 在客户端为UltraWebGrid增加行
  4. C# 重写WndProc 消息循环
  5. 基于MFC的socket编程(异步非阻塞通信)
  6. C语言中“指针”和“指针变量”的区别是什么
  7. Java—Collection、Map、树
  8. IOS9+基础之警报框弹出和操作表弹出
  9. win10蓝牙允许设备连接到此计算机,Win7自由天空专业版系统配置蓝牙时“允许Bluetooth设备连接到此计算机”选项灰...
  10. java jnlp被阻止_JNLP应用程序Web服务访问被拒绝 - java.io.FilePermission