thrift安装记录

thrift homepage : http://incubator.apache.org/thrift/

 

http://hi.baidu.com/hy0kl/blog/item/950fcd248ece383e8644f94d.html


要安装thrift,得先安装boost库

 

一、安装boost库

1.sudo apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-* 
若此命令执行失败,可以在System->Administration->Synaptic Package Manager中安装,最好把以libboost-开头的都安装了,免得以后出错。

 

安装好后

boost库在ubuntu的
/usr/lib/libboost_下面

 

2.安装好后就可以写一个小程序测试了

vim test.cpp

 

#include <boost/lexical_cast.hpp>
#include <iostream>
 
int main()
{
   using boost::lexical_cast;
   int a = lexical_cast<int>("123456");
   double b = lexical_cast<double>("123.456");
   std::cout << a << std::endl;
   std::cout << b << std::endl;
   return 0;
}

 

编译:
g++ -o test test.cpp
运行结果:
./test
123456
123.456

 

二、安装配置thrift
1.cd /home/panbook/workspace/boost_project

(ps:this directory is a little deep, you can use a shallow directory like cd /home/panbook/workspace)

wget http://apache.etoak.com/incubator/thrift/0.2.0-incubating/thrift-0.2.0-incubating.tar.gz

2.panbook@panbook-desktop:~/workspace/boost_project$ tar -zvxf thrift-0.2.0-incubating.tar.gz 

3. sudo apt-get install autoconf
sudo apt-get install autotools-dev(usually this program has installed)
sudo apt-get install flex
sudo apt-get install libtool
sudo apt-get install byacc

 

4.

cd thrift-0.2.0/
./bootstrap.sh
./configure
make

sudo make install (must use root )

 

5 测试

 

panbook@panbook-desktop:~/workspace/boost_project/thrift$ thrift
Usage: thrift [options] file
Options:
  -version    Print the compiler version
  -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
  -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):
  cocoa (Cocoa):
    log_unexpected:  Log every time an unexpected field ID or type is encountered.
  cpp (C++):
    dense:           Generate type specifications for the dense protocol.
    include_prefix:  Use full include paths in generated files.
  csharp (C#):
  erl (Erlang):
  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.
  ocaml (OCaml):
  perl (Perl):
  php (PHP):
    inlined:         Generate PHP inlined files
    server:          Generate PHP server stubs
    autoload:        Generate PHP with autoload
    oop:             Generate PHP with object oriented subclasses
    rest:            Generate PHP REST processors
  py (Python):
    new_style:       Generate new-style classes.
    twisted:         Generate Twisted-friendly RPC services.
  rb (Ruby):
  st (Smalltalk):
  xsd (XSD):

恭喜你,你的thrift已经安装成功咯~

 

三、thrift使用入门

使用入门

使用源代码里的Tutorial做例子。它包含了两个thrift文件。

shared.thrift

namespace cpp shared

namespace java shared

namespace perl shared struct

SharedStruct {

1: i32 key

2: string value

}

service SharedService {

SharedStruct getStruct(1: i32 key)

}


tutorial.thrift

include "shared.thrift"

namespace cpp tutorial

namespace java tutorial

namespace php tutorial

namespace perl tutorial

namespace smalltalk.category Thrift.Tutorial

typedef i32 MyInteger

const i32 INT32CONSTANT = 9853

const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}

enum Operation { ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4 }

struct Work {

1: i32 num1 = 0,

2: i32 num2,

3: Operation op,

4: optional string comment,

}

service Calculator extends shared.SharedService {

void ping(),

i32 add(1:i32 num1, 2:i32 num2),

i32 calculate(1:i32 logid, 2:Work w)

throws (1:InvalidOperation ouch),

oneway void zip()

}


声称c++代码:
thrift thrift -r --gen cpp tutorial.thrift
声称的代码会在gen-cpp目录中,进入gen-cpp发现有下面几个文件:
Calculator.cpp                  SharedService_server.skeleton.cpp
Calculator.h                    shared_types.cpp
Calculator_server.skeleton.cpp  shared_types.h
shared_constants.cpp            tutorial_constants.cpp
shared_constants.h              tutorial_constants.h
SharedService.cpp               tutorial_types.cpp
SharedService.h                 tutorial_types.h
shared.thrift定义的SharedStruct生成在shared_types.h和shared_types.cpp中,service SharedService生成在SharedService.h和SharedService.cpp中,shared.thrift相关的还有shared_constants.h和shared_constants.cpp。同样,tutorial.thrift定义的对象和服务生成的源文件也类似。这里我们可以看出thrift生成代码的规则。

下面我们看看如何利用生成的代码。

Server端:

实现接口

class CalculatorHandler : public CalculatorIf {
 public :
  CalculatorHandler( ) { }

  void ping( ) {
    printf ( "ping()/n" ) ;
  }

  int32_t add( const int32_t n1, const int32_t n2) {
    printf ( "add(%d,%d)/n" , n1, n2) ;
    return n1 + n2;
  }

  int32_t calculate( const int32_t logid, const Work & work) {
    printf ( "calculate(%d,{%d,%d,%d})/n" , logid, work. op, work. num1, work. num2) ;
    int32_t val;

    switch ( work. op) {
    case ADD:
      val = work. num1 + work. num2;
      break ;
    case SUBTRACT:
      val = work. num1 - work. num2;
      break ;
    case MULTIPLY:
      val = work. num1 * work. num2;
      break ;
    case DIVIDE:
      if ( work. num2 = = 0) {
        InvalidOperation io;
        io. what = work. op;
        io. why = "Cannot divide by 0" ;
        throw io;
      }
      val = work. num1 / work. num2;
      break ;
    default :
      InvalidOperation io;
      io. what = work. op;
      io. why = "Invalid Operation" ;
      throw io;
    }

    SharedStruct ss;
    ss. key = logid;
    char buffer[ 12] ;
    snprintf( buffer, sizeof ( buffer) , "%d" , val) ;
    ss. value = buffer;

    log [ logid] = ss;

    return val;
  }

  void getStruct( SharedStruct & ret, const int32_t logid) {
    printf ( "getStruct(%d)/n" , logid) ;
    ret = log [ logid] ;
  }

  void zip( ) {
    printf ( "zip()/n" ) ;
  }

protected :
  map < int32_t , SharedStruct> log ;

} ;


main里面创建服务

int main( int argc, char * * argv) {

  shared_ptr< TProtocolFactory> protocolFactory( new TBinaryProtocolFactory( ) ) ;
  shared_ptr< CalculatorHandler> handler( new CalculatorHandler( ) ) ;
  shared_ptr< TProcessor> processor( new CalculatorProcessor( handler) ) ;
  shared_ptr< TServerTransport> serverTransport( new TServerSocket( 9090) ) ;
  shared_ptr< TTransportFactory> transportFactory( new TBufferedTransportFactory( ) ) ;

  TSimpleServer server( processor,
                       serverTransport,
                       transportFactory,
                       protocolFactory) ;
  printf ( "Starting the server.../n" ) ;
  server. serve( ) ;
  printf ( "done./n" ) ;
  return 0;
}


client端:

int main( int argc, char * * argv) {
  shared_ptr< TTransport> socket ( new TSocket( "localhost" , 9090) ) ;
  shared_ptr< TTransport> transport( new TBufferedTransport( socket ) ) ;
  shared_ptr< TProtocol> protocol( new TBinaryProtocol( transport) ) ;
  CalculatorClient client( protocol) ;

  try {
    transport- > open ( ) ;

    client. ping( ) ;
    printf ( "ping()/n" ) ;

    int32_t sum = client. add( 1, 1) ;
    printf ( "1+1=%d/n" , sum) ;

    Work work;
    work. op = DIVIDE;
    work. num1 = 1;
    work. num2 = 0;

    try {
      int32_t quotient = client. calculate( 1, work) ;
      printf ( "Whoa? We can divide by zero!/n" ) ;
    } catch ( InvalidOperation & io) {
      printf ( "InvalidOperation: %s/n" , io. why. c_str( ) ) ;
    }

    work. op = SUBTRACT;
    work. num1 = 15;
    work. num2 = 10;
    int32_t diff = client. calculate( 1, work) ;
    printf ( "15-10=%d/n" , diff) ;

    
// Note that C++ uses return by reference for complex types to avoid

    
// costly copy construction

    SharedStruct ss;
    client. getStruct( ss, 1) ;
    printf ( "Check log: %s/n" , ss. value. c_str( ) ) ;

    transport- > close ( ) ;
  } catch ( TException & tx) {
    printf ( "ERROR: %s/n" , tx. what ( ) ) ;
  }

}

 

 

安装过程中出现的错误:
1.configure: creating ./config.status
config.status: error: cannot find input file: `Makefile.in'
解决办法:sudo apt-get install autotools-dev

2.../../ylwrap: line 109: yacc:找不到命令
解决办法:sudo apt-get install byacc

3.required file `./ltmain.sh' not found
解决办法: sudo apt-get install libtool

4.
warning: underquoted definition of AM_PATH_LIBMCRYPT
run info '(automake)Extending aclocal'
or see http://sources.redhat.com/automake/automake.html#Extending-aclocal
解决办法:sudo apt-get autoremove libmcrypt


5. /bin/sh ../../libtool --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I. -I../..  -I/usr/include -I./src  -Wall -g -O2 -MT Thrift.lo -MD -MP -MF .deps/Thrift.Tpo -c -o Thrift.lo `test -f 'src/Thrift.cpp' || echo './'`src/Thrift.cpp
../../libtool: line 841: X--tag=CXX: command not found
../../libtool: line 874: libtool: ignoring unknown tag : command not found
../../libtool: line 841: X--mode=compile: command not found
../../libtool: line 1008: *** Warning: inferring the mode of operation is deprecated.: command not found
../../libtool: line 1009: *** Future versions of Libtool will require --mode=MODE be specified.: command not found
../../libtool: line 1152: Xg++: command not found
../../libtool: line 1152: X-DHAVE_CONFIG_H: command not found
../../libtool: line 1152: X-I.: command not found
../../libtool: line 1152: X-I../..: No such file or directory
../../libtool: line 1152: X-I/usr/include: No such file or directory
../../libtool: line 1152: X-I./src: No such file or directory
../../libtool: line 1152: X-Wall: command not found
../../libtool: line 1152: X-g: command not found
../../libtool: line 1152: X-O2: command not found
../../libtool: line 1152: X-MT: command not found
../../libtool: line 1152: XThrift.lo: command not found
../../libtool: line 1152: X-MD: command not found
../../libtool: line 1152: X-MP: command not found
../../libtool: line 1152: X-MF: command not found
../../libtool: line 1152: X.deps/Thrift.Tpo: No such file or directory
../../libtool: line 1152: X-c: command not found
../../libtool: line 1205: XThrift.lo: command not found
../../libtool: line 1210: libtool: compile: cannot determine name of library object from `': command not found
make[3]: *** [Thrift.lo] Error 1
解决办法:libtool的问题,目前只在服务器上有这个问题
1).卸载原服务器的libtool
2).下载最新的libtool安装,wget
http://ftp.gnu.org/gnu/libtool/libtool-2.2.6b.tar.gz .
3). ./configure ;make;make install
4). 重新thrift安装步骤的第四步即可

6../CppServer: error while loading shared libraries: libthrift.so.0: cannot open shared object file: No such file or directory
解决办法: sudo ln -s /usr/local/lib/libthrift.so.0   /usr/lib/libthrift.so.0

7.PHP Notice: Use of undefined constant E_NONE - assumed 'E_NONE' in /
home/wr_webroot/thrift/tutorial/php/PhpClient.php on line 38
解决办法: Just replace E_NONE with 0 (int), that will fix it

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值