Table of Contents
1. 創建thrift文件2. 生成C++代碼3. 編寫C++客戶端代碼4. 例子項目4.1. 項目目錄4.2. 編譯服務端4.3. 編譯客戶端4.4. 運行thrift文件非常簡單,一個WorkerManager提供了一個ping方法,讓客戶端通過RPC方式遠程調用,模擬icmp協議的ping,看看服務端是否正常.
# worker.thrift # Dean Chen ([email protected]) # /** * Thrift files can namespace, package, or prefix their output in various * target languages. */ namespace cpp freebird /** * Defining a removed class named WorkerManager */ service WorkerManager { /** * client calls ping method to make sure service process is active or dead */ void ping() }
用thrift命令行就可以生成C++代碼,包括服務器端代碼:
thrift -r --gen cpp -o ../ worker.thrift
上面的命令會在../目錄創建gen-cpp目錄,裡面包含了所有生成的C++代碼.
worker_constants.cpp worker_constants.h WorkerManager.cpp WorkerManager.h WorkerManager_server.skeleton.cpp worker_types.cpp worker_types.h
WorkerManager_server.skeleton.cpp就是C++服務端的main函數入口文件,裡面使用了TSimpleServer作為TCP服務,性能較低,但是實現簡單,比較合適做進程管理類的接口. 這目前就是我需要的.因為我的一個程序中就是需要能夠遠程對worker進程進行狀態查詢和任務控制,而不是傳遞大量的數據. 下面是自動產生的代碼:
// This autogenerated skeleton file illustrates how to build a server. // You should copy it to another filename to avoid overwriting it. #include "WorkerManager.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 ::freebird; class WorkerManagerHandler : virtual public WorkerManagerIf { public: WorkerManagerHandler() { // Your initialization goes here } /** * client calls ping method to make sure service process is active or dead */ void ping() { // Your implementation goes here printf("ping\n"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr handler(new WorkerManagerHandler()); shared_ptr processor(new WorkerManagerProcessor(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; }
客戶端程序需要自己編寫:
#include#include #include #include #include "../../server/gen-cpp/WorkerManager.h" using namespace std; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace freebird; int main() { boost::shared_ptr socket(new TSocket("localhost", 9090)); boost::shared_ptr transport(new TBufferedTransport(socket)); boost::shared_ptr protocol(new TBinaryProtocol(transport)); WorkerManagerClient client(protocol); try { transport->open(); client.ping(); cout << "ping()" << endl; transport->close(); } catch (TException& tx) { cout << "ERROR: " << tx.what() << endl; } }
項目路徑
thrift_base$ tree -L 2 . ├── client │ ├── builder │ ├── include │ └── src └── server ├── builder ├── gen-cpp └── thrift
sever目錄下除了thrift目錄裡的thrift文件外,沒有任何額外的C++代碼gen-cpp是命令行產生的. 用下面的命令進行編譯
cd builder ./rebuild.lsp debug_config.lsp
builder/bin目錄下的thrift_server是可執行程序
client目錄下src目錄裡的main.cc文件是唯一手寫的代碼,編譯後在builder/bin目錄下出現thrift_client可執行程序. 用下面的命令進行編譯
cd builder ./rebuild.lsp debug_config.lsp
先啟動服務端程序,監聽9099端口,然後啟動客戶端程序,調用服務端的ping方法,服務端控制台打印出文字. 一切工作正常.