博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThriftUsageC++ - Thrift Wiki
阅读量:5793 次
发布时间:2019-06-18

本文共 6916 字,大约阅读时间需要 23 分钟。

Getting started

 

The first thing you need to know is that the C++ code generated by Thrift compiles only on Unix based systems, although some success has been reported using Cygwin on Win32 in .   

 

Requirements

 

Make sure that your system meets the requirements as noted in   

  • Thrift library files  
  • Thrift header files.   

 

Installing the Thrift library

 

Installing the Thrift library is trivial to link with the generated code.   

  1. Download a snapshot of Thrift and extract if you haven't done so already -  

  2. Navigate to lib/cpp using the terminal of your choice  

  3. Run  make to compile  

  4. Run  make install to install the library. Your user needs root permissions.   

 

Generating the server code

 

In this example we use an imaginary file called your_thrift_file.thrift:   

    

#!/usr/local/bin/thrift --gen cppnamespace cpp Testservice Something {  i32 ping()}

Now run:

 

thrift --gen cpp your_thrift_file.thrift

 

Exploring the generated code - The Server

The generated code should be under the gen-cpp directory. You will see a number of generated C++ and header files along with an automatically generated server skeleton code (in bold).

  • Something.cpp
  • Something.h
  • Something_server.skeleton.cpp

  • your_thrift_file_constants.cpp
  • your_thrift_file_constants.h
  • your_thrift_file_types.cpp
  • your_thrift_file_types.h

 

Implementing the Server

Copy the generated server skeleton to a file named Something_server.cpp and keep the original:

 

cp Something_server.skeleton.cpp Something_server.cpp

When this server is run in console it prints "ping" on the console window each time the function is called from a client.

Here's the autogenerated skeleton file to illustrate how to write a server: Something_server.cpp

 

#include "Something.h"#include 
#include
#include
#include
using namespace apache::thrift;using namespace apache::thrift::protocol;using namespace apache::thrift::transport;using namespace apache::thrift::server;using boost::shared_ptr;using namespace Test;class SomethingHandler : virtual public SomethingIf { public: SomethingHandler() { // Your initialization goes here } int32_t ping() { // Your implementation goes here printf("ping\n"); }};int main(int argc, char **argv) { int port = 9090; shared_ptr
handler(new SomethingHandler()); shared_ptr
processor(new SomethingProcessor(handler)); shared_ptr
serverTransport(new TServerSocket(port)); shared_ptr
transportFactory(new TBufferedTransportFactory()); shared_ptr
protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0;}

 

Compiling/Building the server

To quickly build a binary using a single command use:

 

g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift *.cpp -L/usr/local/lib -lthrift -o something

 

Compiling

You need to point your compiler to the thrift include path (CXX flag:  -I/usr/local/include/thrift)

 

g++ -Wall -I/usr/local/include/thrift -c Something.cpp -o something.og++ -Wall -I/usr/local/include/thrift -c Something_server.cpp -o server.og++ -Wall -I/usr/local/include/thrift -c your_thrift_file_constants.cpp -o constants.og++ -Wall -I/usr/local/include/thrift -c your_thrift_file_types.cpp -o types.o

 

Linking

You need to point your linker to the thrift library. (Linker flag:  -lthrift  or  -l/usr/local/lib/libthrift.so )

 

g++ -L/usr/local/lib *.o -o Something_server -lthrift

 

Writing the client code

thrift does not auto generate a client interface, so you have to create the file.

 

#include "Something.h"  // As an example#include 
#include
#include
using namespace apache::thrift;using namespace apache::thrift::protocol;using namespace apache::thrift::transport;using namespace Test;int main(int argc, char **argv) { boost::shared_ptr
socket(new TSocket("localhost", 9090)); boost::shared_ptr
transport(new TBufferedTransport(socket)); boost::shared_ptr
protocol(new TBinaryProtocol(transport)); SomethingClient client(protocol); transport->open(); client.ping(); transport->close(); return 0;}

 

Compiling

 

g++ -Wall -I/usr/local/include/thrift -c Something_client.cpp -o client.o

 

Linking

 

g++ -L/usr/local/lib client.o Something.o constants.o types.o -o Something_client -lthrift

 

Compiling/Building everything with Makefile

 

GEN_SRC := Something.cpp your_thrift_file_constants.cpp your_thrift_file_types.cppGEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC))THRIFT_DIR := /usr/local/include/thriftBOOST_DIR := /usr/local/includeINC := -I$(THRIFT_DIR) -I$(BOOST_DIR).PHONY: all cleanall: something_server something_client%.o: %.cpp        $(CXX) -Wall -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H $(INC) -c $Slt; -o $@something_server: Something_server.o $(GEN_OBJ)        $(CXX) $^ -o $@ -L/usr/local/lib -lthrift something_client: Something_client.o $(GEN_OBJ)        $(CXX) $^ -o $@ -L/usr/local/lib -lthrift clean:        $(RM) *.o something_server something_client

 

Appendix: About TNonblockingServer

If you are writing an application that will serve a lot of connection (like php front end calling thrift service), you'd better use TNonblockingServer. TNonblockingServer can accept a lot of connections while throttling the processor threads using a pool.

* TNonblockingServer with a thread pool is the c++ alternative of the JAVA THsHaServer; * TNonblockingServer withOUT a thread pool is the c++ alternative of the JAVA TNonblockingServer;

Server code with thread pool:

shared_ptr
processor(new SomethingProcessor(handler)); shared_ptr
protocolFactory(new TBinaryProtocolFactory()); // using thread pool with maximum 15 threads to handle incoming requests shared_ptr
threadManager = ThreadManager::newSimpleThreadManager(15); shared_ptr
threadFactory = shared_ptr
(new PosixThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); TNonblockingServer server(processor, protocolFactory, 8888, threadManager); server.serve(); // ...

C++ client code (you have to use TFramedTransport here):

boost::shared_ptr
socket(new TSocket("localhost", 8888)); boost::shared_ptr
transport(new TFramedTransport(socket)); boost::shared_ptr
protocol(new TBinaryProtocol(transport)); SomethingClient client(protocol); transport->open(); // do something here... transport->close();

PHP client code (you have to use TFramedTransport here):

$transport = new TFramedTransport(new TSocket("localhost", 8888));    $transport->open();    $protocol = new TBinaryProtocol($transport);    $client= new SomethingClient($protocol, $protocol);    // do something here...    transport->close();

转载地址:http://liffx.baihongyu.com/

你可能感兴趣的文章
jQuery插件的开发
查看>>
基础,基础,还是基础之JAVA基础
查看>>
HTTP库Axios
查看>>
填坑记:Uncaught RangeError: Maximum call stack size exceeded
查看>>
SpringCloud之消息总线(Spring Cloud Bus)(八)
查看>>
【348天】每日项目总结系列086(2018.01.19)
查看>>
【294天】我爱刷题系列053(2017.11.26)
查看>>
2016/08/25 The Secret Assumption of Agile
查看>>
(Portal 开发读书笔记)Portlet间交互-PortletSession
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
Java虚拟机管理的内存运行时数据区域解释
查看>>
人人都会深度学习之Tensorflow基础快速入门
查看>>
ChPlayer播放器的使用
查看>>
js 经过修改改良的全浏览器支持的软键盘,随机排列
查看>>
探寻Interpolator源码,自定义插值器
查看>>
一致性哈希
查看>>
Web日志安全分析工具 v2.0发布
查看>>
第十六章:脚本化HTTP
查看>>
EXCEL表中如何让数值变成万元或亿元
查看>>
L104
查看>>