【学習ノート】thriftダウンロードとインストール

5127 ワード

一、紹介
thriftはフェイスブックから来ており、拡張可能で言語にまたがるサービスの開発を行うためのソフトウェアフレームワークです.簡単な定義ファイルのデータ型とサービスインタフェースを定義できます.入力ファイルとして、コンパイラは、RPCクライアントとサーバとの通信を容易に生成するためのシームレスなプログラミング言語を生成するためにコードを生成する.C++、Java、Python、PHP、C#などの主流の言語をサポートすることができ、Googleのprotobufに似ており、両者の比較についてはネット上でも多く、後発のショーavroにはhadoopの背景があり、注目に値する.私が最初に接触したのはthriftなので、ずっとこれを使っていて、いくつかのプロジェクトで使っていて、何の問題もなかったので、protobufとavroを研究する気もありませんでした.
二、ダウンロードとインストール
環境:Ubuntu
依存:boost、libevent
ダウンロード先:http://thrift.apache.org/download/
ダウンロードファイル:thrift-0.9.0.tar.gz
私はUbuntuを使っています.公式サイトのインストール手順に従ってインストールしても問題はありません.主にboostとlibeventに依存して、この2つを詰めておけばいいです.公式サイトからのコマンドを直接使用できます.
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

私はboost開発をよく使うので、boostをインストールしたので、直接接続すればいいです.
$>tar xvf thrift-0.9.0.tar.gz
$>ch thrift-0.9.0
$>./config --prefix=/usr/local/thrift --with-boost=/usr/local/boost
$>make
$>make install

三、テスト
公式サイトを直接使用した例
1.コード作成スクリプトファイル
UserStorage.thrift(注意:接尾辞はthriftでなければなりません.ファイル名はクラス名を生成します)
struct UserProfile {
    1: i32 uid,
    2: string name,
    3: string blurb
}
service UserStorage {
     void store(1: UserProfile user),
     UserProfile retrieve(1: i32 uid)
}

$THRIFT_をHOME/binは、環境変数のPATHに追加され、コマンドを実行します.
$>thrift -gen cpp UserStorage.thrift

これは現在のディレクトリの下にgen-cppがあるディレクトリで、生成された関連C++コード(thriftコマンドで他のコードを生成することもできます.例えば、thrift-gen java UserStorage.thrift、javaコードを生成します):
UserStorage.h
UserStorage.cpp
UserStorage_types.h UserStorage_types.cpp
UserStorage_constants.h
UserStorage_constants.cpp
UserStorage_server.skeleton.cpp上の赤いファイルは私たちのこの例に必要なコードで、他の3つはしばらく気にしないことができます.
2.サーバ側コードの作成
server.cpp
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/server/TSimpleServer.h>
#include "UserStorage.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 namespace boost;

class UserStorageHandler : virtual public UserStorageIf {
public:
	UserStorageHandler() {
		// Your initialization goes here
	}

	void store(const UserProfile& user) {
		// Your implementation goes here
		printf("store
"); } void retrieve(UserProfile& _return, const int32_t uid) { // Your implementation goes here printf("retrieve
"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr handler(new UserStorageHandler()); shared_ptr processor(new UserStorageProcessor(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; }

g++ -o server -I/usr/local/boost/include -I/usr/local/thrift/include -L/usr/local/boost/lib -L/usr/local/thrift/lib -lthrift -lthriftnb -levent server.cpp
 
  
 

3.客户端代码

client.cpp

 
 
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TProtocol.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "UserStorage.h"

using namespace apache::thrift;
using namespace apache::thrift::transport;
using namespace apache::thrift::protocol;
using namespace boost;

int main()
{
	boost::shared_ptr<TTransport> m_sock;
	boost::shared_ptr<TTransport> m_pTransport;
	boost::shared_ptr<TProtocol> m_protocol;
	shared_ptr<UserStorageClient> m_pClient;
	m_sock.reset(new TSocket("127.0.0.1", 9090));
	m_pTransport.reset(new TFramedTransport(m_sock));
	m_protocol.reset(new TBinaryProtocol(m_pTransport));
	m_pClient.reset(new UserStorageClient(m_protocol));
	m_pTransport->open();
	UserProfile up;
	up.uid = 1;
	up.name = "bocheng";
	up.blurb = "1";
	m_pClient->store(up);
	return 0;
}

g++ -o client -I/usr/local/boost/include -I/usr/local/thrift/include -L/usr/local/boost/lib -L/usr/local/thrift/lib -lthrift -lthriftnb -levent client.cpp
これは単純な例にすぎませんが、サーバはTSimpleServerを使用しています.単一スレッドをブロックするサーバです.一般的にオンラインで走る場合はこのサーバは使用されません.そのため、後述ではTnonblockingServerを使用して非ブロックマルチスレッドのサーバを作成する方法について説明します.