Hatena::ブログ(Diary)

ソフトなぶろぐ

2008-11-10

Thriftを触ってみる(1)

Thriftを知ったキッカケは、雑誌WEB+DB(vol.46)のはてなの伊藤さんの記事「Resent Perl World 第15回 Thriftで多言語RPC」。恥ずかしながら今までこの存在を知りませんでした。この記事とインターネットの情報から、こんな便利そうなソフトを試さずにはいられなくなり、早速インストールして触ってみました。

portinstall devel/thrift

LIB_DEPENDSになっているdevel/boostは既にインストール済み。

インストールされる一覧は次の通り。

> pkg_info -Lx thrift

Information for thrift-20070917_2:

Files:

/usr/local/bin/thrift

/usr/local/include/thrift/TLogging.h

/usr/local/include/thrift/TProcessor.h

/usr/local/include/thrift/TReflectionLocal.h

/usr/local/include/thrift/Thrift.h

/usr/local/include/thrift/config.h

/usr/local/include/thrift/reflection_limited_types.h

/usr/local/include/thrift/concurrency/Exception.h

/usr/local/include/thrift/concurrency/Monitor.h

/usr/local/include/thrift/concurrency/Mutex.h

/usr/local/include/thrift/concurrency/PosixThreadFactory.h

/usr/local/include/thrift/concurrency/Thread.h

/usr/local/include/thrift/concurrency/ThreadManager.h

/usr/local/include/thrift/concurrency/TimerManager.h

/usr/local/include/thrift/concurrency/Util.h

/usr/local/include/thrift/processor/PeekProcessor.h

/usr/local/include/thrift/processor/StatsProcessor.h

/usr/local/include/thrift/protocol/TBinaryProtocol.h

/usr/local/include/thrift/protocol/TDebugProtocol.h

/usr/local/include/thrift/protocol/TDenseProtocol.h

/usr/local/include/thrift/protocol/TOneWayProtocol.h

/usr/local/include/thrift/protocol/TProtocol.h

/usr/local/include/thrift/protocol/TProtocolException.h

/usr/local/include/thrift/server/TNonblockingServer.h

/usr/local/include/thrift/server/TServer.h

/usr/local/include/thrift/server/TSimpleServer.h

/usr/local/include/thrift/server/TThreadPoolServer.h

/usr/local/include/thrift/server/TThreadedServer.h

/usr/local/include/thrift/transport/TFileTransport.h

/usr/local/include/thrift/transport/THttpClient.h

/usr/local/include/thrift/transport/TServerSocket.h

/usr/local/include/thrift/transport/TServerTransport.h

/usr/local/include/thrift/transport/TSocket.h

/usr/local/include/thrift/transport/TSocketPool.h

/usr/local/include/thrift/transport/TTransport.h

/usr/local/include/thrift/transport/TTransportException.h

/usr/local/include/thrift/transport/TTransportUtils.h

/usr/local/include/thrift/transport/TZlibTransport.h

/usr/local/lib/libthrift.a

/usr/local/lib/libthrift.la

/usr/local/lib/libthrift.so

/usr/local/lib/libthrift.so.0

/usr/local/lib/libthriftnb.a

/usr/local/lib/libthriftnb.la

/usr/local/lib/libthriftnb.so

/usr/local/lib/libthriftnb.so.0

/usr/local/lib/libthriftz.a

/usr/local/lib/libthriftz.la

/usr/local/lib/libthriftz.so

/usr/local/lib/libthriftz.so.0

/usr/local/share/thrift/if/reflection_limited.thrift

/usr/local/share/doc/thrift/thrift.pdf

「Thrift: Scalable Cross-Language Services Implementation」(Mark Slee, Aditya Agarwal and Marc Kwiatkowski)という文献が含まれていて、/usr/local/share/doc/thrift/thrift.pdfインストールされます。これは次のURLPDFと同じものです。http://developers.facebook.com/thrift/thrift-20070401.pdf

#!/usr/local/bin/thrift

service Hello

{

string hello(1: string name)

}

上記のようにHelloを定義してファイル名hello.thriftとして保存し、次のコマンドを順に実行。

> thrift -cpp -perl ./hello.thrift

> cp gen-cpp/Hello_server.skeleton.cpp gen-cpp/Hello_server.cpp

> cd gen-cpp

> g++ ./Hello_server.cpp ./Hello.cpp -o ./Hello_server -lthrift -I/usr/local/include/thrift -L/usr/local/lib -I/usr/local/include

これで、Hello_serverができる。

次回に続く。


参考にさせていただいたサイト、雑誌