Revision 1 as of 2010-06-08 03:08:17

Clear message
Locked History Actions

thrift

Thrift

参考

-Apache Thrift -Facebook Developers -Thrift: Scalable Cross-Language Services Implementation -Ubuntu 8.04 で Thrift を試してみた -Thriftを触ってみる(1) -Thrift wiki

バイナリの取得とビルド

現時点(2009/1/1)で、バイナリを配布しているところは無い模様。上記Apache Thriftからもソースコードしか取得できない。 ただし、上記「Thriftを触ってみる」ではバイナリダウンロードをしているようだが、これはFreeBSDバージョンであるらしい。

現時点では、上記「Ubuntu 8.04 で Thrift を試してみた」の通り、Ubuntu(Debian系列?)でビルドするのが最も手早い。

-Redhat系列(CentOS-5.2, Fedora-10)ではどうしてもうまくビルドできなかった。 -Windows上では非常に難しいと思われる。

困難の理由はひとえに、boostというC++ライブラリに依存していることがあげられる。これはとても巨大なライブラリであり、redhat系列のrpmパッケージはきちんとビルドされていないのか(あるいは相性が悪いのか)、Thriftをどうしてもビルドできない。また、Windows用のバイナリも提供されていない模様。

フラグ

Usage: thrift [options] file
Options:
  -version    Print the compiler version
  -php        Generate PHP output files
  -phpi       Generate PHP inlined files
  -phps       Generate PHP server stubs (with -php)
  -phpl       Generate PHP-lite (with -php)
  -phpa       Generate PHP with autoload (with -php)
  -phpo       Generate PHP with object oriented subclasses (with -php)
  -xsd        Generate XSD output files
  -erl        Generate Erlang output files
  -o dir      Set the output directory for gen-* packages
               (default: current directory)
  -I dir      Add a directory to the list of directories
                searched for include directives
  -rest       Generate PHP REST processors (with -php)
  -nowarn     Suppress all compiler warnings (BAD!)
  -strict     Strict compiler warnings on
  -v[erbose]  Verbose mode
  -r[ecurse]  Also generate included files
  -debug      Parse debug trace to stdout
  --gen STR   Generate code with a dynamically-registered generator.
                STR has the form language[:key1=val1[,key2,[key3=val3]]].
                Keys and values are options passed to the generator.
                Many options will not require values.

Available generators (and options):
  cpp (C++):
    dense:           Generate type specifications for the dense protocol.
    include_prefix:  Use full include paths in generated files.
  hs (Haskell):
  html (HTML):
  java (Java):
    beans:           Generate bean-style output files.
    nocamel:         Do not use CamelCase field accessors with beans.
    hashcode:        Generate quality hashCode methods.
  perl (Perl):
  py (Python):
    new_style:       Generate new-style classes.
  rb (Ruby):

-phpと-phpiはインラインとされるかどうか、処理内容は変わりない模様。 -phpiにすると、gen-phpiというディレクトリに出力される。 <li>-phpと-phpsは全く同じ <li>-phplは、-phpからProcessorを除去したもの。 <li>-phpaは、-phpとはかなり異なる。-phpaのみにて作成されるファイルもある。 <li>-phpoは、-phpのクラスにextends TBase等が付加される。 </ul> <ul> <li>java:beansは全く効果なし。 <li>java:hashcodeを指定すると、常に0を返していたhashCodeメソッドがきちんと値を返すようになる。 <li>java:nocamelも特に効果なし。 </ul>

<pre>

  • } else if (strcmp(arg, "-php") == 0) {
    • gen_php = true;
    } else if (strcmp(arg, "-phpi") == 0) {
    • gen_phpi = true;
    } else if (strcmp(arg, "-phps") == 0) {
    • gen_php = true; gen_phps = true;
    } else if (strcmp(arg, "-phpl") == 0) {
    • gen_php = true; gen_phps = false;
    } else if (strcmp(arg, "-phpa") == 0) {
    • gen_php = true; gen_phps = false; gen_phpa = true;
    } else if (strcmp(arg, "-phpo") == 0) {
    • gen_php = true; gen_phpo = true;
    } else if (strcmp(arg, "-rest") == 0) {
    • gen_rest = true;

</pre>

<h2>サービスのマルチプレックス化</h2> <p> 例えばJavaのサーバを作成する場合、 </p> <pre> public class Server {

  • public static class DebugHandler implements Debug.Iface {

    • public void info(String value) {
      • System.out.println(value);
      }
    } public static void main(String[]args) throws Exception {
    • DebugHandler debugHandler = new DebugHandler(); Debug.Processor processor = new Debug.Processor(debugHandler); TServerTransport transport = new TServerSocket(9090); TServer server = new TSimpleServer(processor, transport); server.serve();

    }

} </pre> <p> などとすればよいが、一つのサーバには一つのサービスしか登録できず、一つのサーバには一つのポートが必要である。したがって、上記Debug.IFaceのほかにSample.IFaceやTest.IFaceのサーバを作成する場合は、それぞれのプロセッサ・トランスポート・ポートが必要になる。 </p> <p> これを避けるために、<a href="https://issues.apache.org/jira/browse/THRIFT-66">https://issues.apache.org/jira/browse/THRIFT-66</a>というパッチがリリースされている。これを用いると、一つのポートで複数のサービスを提供できるらしいが、ただしクライアント側も変更しなければならない。 </p> <p> 現在はJava用しか提供されておらず、また正式なリリースではなくテスト的なものであるため、とりあえず避けておくのが無難と思われる。 </p>