Ubuntu 8.04 で Thrift を試してみた
準備
% sudo apt-get install build-essential automake libtool flex bison libboost\* % sudo apt-get install libbit-vector-perl libclass-accessor-perl
インストール
% cd program/thrift % wget -O thrift.tgz "http://gitweb.thrift-rpc.org/?p=thrift.git;a=snapshot;h=HEAD;sf=tgz" % tar zxvf thrift.tgz % cd thrift % ./bootstrap.sh % ./configure --disable-gen-java --without-java --disable-gen-csharp --disable-gen-st --disable-gen-ocaml --disable-gen-cocoa % make % sudo make install % cd lib/perl % perl Makefile.PL % sudo make install % cd ../rb % sudo ruby setup.rb
サンプル
WEB+DB Vol.46 のサンプルを実行してみる.
- Hello.thrift
#!/usr/bin/thrift service Hello { string hello(1: string name) }
- スケルトンの作成
% thrift --gen rb --gen cpp --gen perl hello.thrift
- gen-cpp/Hello_server.cpp
差分だけ
void hello(std::string& _return, const std::string& name) { // Your implementation goes here _return.append("Hello ").append(name); }
- サーバのコンパイル
% g++ -g Hello_server.cpp Hello.cpp -o Hello_server -lthrift -I/usr/local/include/thrift
- Perlクライアントの作成(hello.pl)
#!/usr/bin/env perl use strict; use warnings; use lib './gen-perl'; use Thrift; use Thrift::BinaryProtocol; use Thrift::Socket; use Hello; my $transport = Thrift::Socket->new('localhost', 9090); my $client = HelloClient->new( Thrift::BinaryProtocol->new($transport) ); $transport->open(); printf "%s\n", $client->hello('conceal-rs'); $transport->close();
- Rubyクライアントの作成(hello.rb)
#!/usr/bin/env ruby require 'thrift/thrift' require 'thrift/transport/tsocket' require 'thrift/transport/ttransport' require 'thrift/protocol/tbinaryprotocol' require 'gen-rb/Hello' transport = TBufferedTransport.new(TSocket.new('localhost', 9090)) client = Hello::Client.new(TBinaryProtocol.new(transport)) transport.open puts client.hello('conceal-rs san') transport.close
- サーバの実行とテスト
% cp gen-cpp % ./Hello_server % perl hello.pl Hello conceal-rs % ruby hello.rb Warning: class TSocket is deprecated from hello.rb:10 Warning: class TBufferedTransport is deprecated from hello.rb:10 Warning: class TBinaryProtocol is deprecated from hello.rb:11 Hello conceal-rs san
なんか Warning が出てるな.まあ,とりあえず動いた.