thrift 搭建服务器框架

本文介绍Apache Thrift,一种支持跨语言服务开发的框架。通过示例详细讲解如何使用Thrift定义服务接口、生成代码及搭建服务端与客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apache thrift是一个可用来进行可扩展的跨语言(C++,java,python等)的服务架构的开发的框架,facebook的服务基于此开发。它采用接口描述语言定义并创建服务,所包含的代码生成引擎可以在多种语言中使用。下面详细介绍thrift服务搭建方法:

首先根据应用规则,即客户端与服务器的交互逻辑设计.thrift脚本。

thrift支持的基本类型包括预定义的基本类型(byte,i32,i64,double,string)、结构体(struct)、容器(list,set,map)、枚举等。

例如设计如下thrift文件:

struct Content{
	1:string content,
}

struct Ret
{
	1:string word,
	2:i32 property,
	3:double pos,
	4:string ref,
}

struct RetVec{
	1:string mid,
	2:list<Ret> rets,
}

service Serv{
	list<RetVec> getContentRets(1:list<Content> con),
}
上述服务设计了3个struct和一个服务接口,对于客户端请求的每一组Content返回一组RetVec,每一个RetVec包含对特定Content的处理结果,包括一个mid和一组Ret。

生成c++,java和python代码框架:

thrift -r --gen cpp hello.thrift
thrift -r --gen java hello.thrift
thrift -r --gen py hello.thrift


这时生成子目录gen-cpp,gen-java,gen-py 


将会在./gen-cpp/目录下生成如下文件

Serv.cpp
Serv.h
Serv_server.skeleton.cpp
hello_constants.cpp
hello_constants.h
hello_types.cpp
hello_types.h
其中Serv_server.skeleton.cpp用于服务端修改代码,Serv.h提供服务接口,hello_*.cpp和hello_*.h是根据定义的struct生成的数据类型文件。

Serv_server.skeleton.cpp类似如下内容:

#include "Serv.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <concurrency/ThreadManager.h>
#include <concurrency/PosixThreadFactory.h>
#include <server/TThreadPoolServer.h>
#include <server/TThreadedServer.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::concurrency;

using boost::shared_ptr;

class ServHandler : virtual public ServIf {
 public:
  ServHandler() {
    // Your initialization goes here
  }

  void getContentRets(vector<RetVec> _ret_vec_RetVec, const vector<Content> con){
    // Your implementation goes here
  }
};

int main(int argc, char **argv) {
  shared_ptr<ServHandler> handler(new ServHandler());
  shared_ptr<TProcessor> processor(new ServProcessor(handler));
  shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  shared_ptr<TServerTransport> serverTransport(new TServerSocket(9090));

  shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);
  shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
  threadManager->threadFactory(threadFactory);
  threadManager->start();
  printf("start user server...\n");

  TThreadPoolServer server(processor, serverTransport, transportFactory, protocolFactory, threadManager);
  server.serve();
  return 0;
}

编写客户端HelloClient.cpp如下:

#include "Serv.h"  // 替换成你的.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();

	// 我们的代码写在这里

	transport->close();

	return 0;
}

编写Makefile文件:

BOOST_DIR = /usr/local/include/boost/
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = ./gen-cpp/hello_types.cpp ./gen-cpp/hello_constants.cpp ./gen-cpp/Serv.cpp
default: server client
server: Serv_server.skeleton.cpp
        g++ -g -o HelloServer -I${THRIFT_DIR} -I${BOOST_DIR}  -I./gen-cpp -L${LIB_DIR} -lthrift Serv_server.skeleton.cpp ${GEN_SRC}
client: UserClient.cpp
        g++ -g -o HelloClient -lm -pthread -lz -lrt -lssl -I${THRIFT_DIR} -I${BOOST_DIR}  -I./gen-cpp -L${LIB_DIR} -lthrift HelloClient.cpp ${GEN_SRC}
clean:
        $(RM) -r HelloServer HelloClient
Thrift允许你选择 protocol ,transport和server。因为Thrift 最早是有C++开发的,Thrift在C++实现中有最大的变化。
Thrift支持 text 和 binary protocols,binary protocols要比text protocols,但是有时候 text protocols比较有用(例如:调试的时候)。支持的协议有:
TBinaryProtocol  -直接的二进制格式
TCompactProtocol  效率和高压缩编码数据
TDenseProtocoal  和TCompactProtocol相似,但是省略了meta信息,从哪里发送的,增加了receiver。还在实验中,java实现还不可用。
TJSONProtocoal 使用JSON
TSImpleJSONProtocoal  只写的protocol使用JSON。适合被脚本语言转化
TDebugProtocoal  使用人类可读的text 格式 帮助调试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值