Thrift

Example

1).thrift

struct Student{
 1: i32 sno,
 2: string sname,
 3: bool ssex,
 4: i16 sage,
}
service Serv{
 void put(1: Student s),
}

2)自动生成文件

thrift -r --gen cpp student.thrift

3)修改.skeleton.cpp

class ServerServiceHandler : virtual public ServerServiceIf {
 public:
  ServerServiceHandler() {
    // Your initialization goes here
    //这里写初始化
    //构造函数
    printf("Initialize\n");
  }

  void show(const Student& student) {
    // Your implementation goes here
    //这里写需要实现的服务
    //用于处理客户端传送来的参数
    printf("show\n");
	//用printf写一个简单的数据处理服务
    printf("ID = %d Name = %s Sex = %d Age = %d/n", s.ID, s.Name.c_str(), s.Sex, s.Age);
  }
};

4)使用thrift包自动生成的client类来实现客户端

就是用来调用client类就可以了,真正的client功能其实已经实现了

#include "ServerService.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.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) {
	boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
	boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

	transport->open();

	Student student;
    s.ID = 2016220102031;
    s.Name = "WYZ";
    s.Sex = 1;
    s.sage = 22;

    ServerServiceClient client(protocol);

    client.put(student);

	transport->close();

	return 0;
}

Reference

由浅入深了解Thrift(一)——Thrift介绍与用法

thrift一个例子

Thrift入门

Thrift基础-linux环境搭建+cmake编译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值