Thrift RPC 远程调用

Thrift 0.9.1 编译

https://github.com/apache/thrift

Thrift 直接从github上下载源码, 运行bootstrap.sh 

发现依赖libtool 和autoconfig   flex yacc 具体过程如下

./configure --with-cpp  --with-boost=/usr/local --with-python --without-csharp --without-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell --without-go --without-lua

或者

./configure --with-cpp  --with-boost=/usr/local --without-python --without-csharp --without-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell --without-go --without-lua

具体命令可以参考http://www.cnblogs.com/szlx/archive/2012/09/13/thrift_example.html

出错ylwrap: line 109: yacc: command not found

出错flex: command not found

apt-get install byacc

apt-get install flex


依赖还真多http://gushuizerotoone.iteye.com/blog/700294

/bin/sh ../../ylwrap `test -f 'src/thriftl.ll' || echo './'`src/thriftl.ll .c thriftl.cc -- /bin/sh /root/duanx/thrift-0.9.1/missing --run flex  

make[3]: *** [thriftl.cc] Error 1

怀疑是gcc的问题, 还不知道怎么改....

安完之前的依赖, 重新生成configure 重新生成 Makefile 重新make 问题解决, 怎么可以这样.

after install a lot libs, clean Makefile, re run ./configure, re make ,the problem solve.

最后 

make check
make install


ubuntu 安装

报错:configure: error: "Error: libcrypto required."

apt 安装 libssl-dev 就OK了

thrift 安装 make 失败 ar: .libs/ThriftTest_constants.o: No such file or directory


$cp test/cpp/*.o test/cpp/.libs/
$make
$make install

然后OK

原理

董的博客

http://dongxicheng.org/search-engine/thrift-internals/

Linux大棚

http://roclinux.cn/?p=3316

编译时各种error:

/root/thrift-0.8.0/lib/cpp/src/transport/TTransport.h:34: error: expected constructor, destructor, or type conversion before ?.eadAll?
/root/thrift-0.8.0/lib/cpp/src/transport/TTransport.h:107: error: ?.int32_t?.does not name a type
解决办法:

g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift -c *.cpp
g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I /root/thrift-0.8.0/lib/cpp/src/ -c -g *.cpp

还是 thrift 官网的教程比较给力

http://wiki.apache.org/thrift/ThriftUsageC%2B%2B

中途还有各种编译问题

# g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift -c client.cpp 
client.cpp: In function ?.nt main(int, char**)?.
client.cpp:19: error: ?.imeServiceClient?.was not declared in this scope
client.cpp:19: error: expected ?.?.before ?.lient?
client.cpp:18: warning: unused variable ?.ytime?
最后可以定位是TimeServiceClient未定义的感觉,发现是在server的.h中定义的.加上OK

python demo

http://blog.csdn.net/xiaojun1288/article/details/9223487

TNonblockingServer

自己想写一个

参考http://blog.csdn.net/jianbinhe1012/article/details/7726738

里边好多坑, 怀疑他自己编过没缺的东西包括

  #include <server/TNonblockingServer.h>
  #include <concurrency/PosixThreadFactory.h>
  using namespace ::apache::thrift::concurrency;

link的时候新加的库

g++ -L/usr/local/lib *.o -o Regist_server -lthrift -lthriftnb -levent
g++ -L /root/thrift-0.8.0/lib/cpp/ *.o -o Regist_server -lthrift -lthriftnb -levent


thrift的使用

thrift序列化和反序列化


thrift  sockPool 中绑定多个服务端IP和端口

shared_ptr<TSocketPool> sockPool =shared_ptr<TSocketPool>(new TSocketPool());
  sockPool->addServer("127.0.0.1",9090);
  sockPool->addServer("127.0.0.1",9091);
  sockPool->addServer("127.0.0.1",9092);
  sockPool->setRandomize(true);
  sockPool->setNumRetries(3);
  sockPool->setMaxConsecutiveFailures(1);
  shared_ptr<TTransport> socket(sockPool);

thrift 常见问题

http://blog.csdn.net/hbuxiaoshe/article/details/22106065


兼容问题!

(1)  不要修改已存在域的整数编号

(2)  新添加的域必须是optional的,以便格式兼容。对于一些语言,如果要为optional的字段赋值,需要特殊处理,比如对于C++语言,要为


中的optional字段age赋值,需要将它的__isset值设为true,这样才能序列化并传输或者存储(不然optional字段被认为不存在,不会被传输或者存储),

如:


(3)  非required域可以删除,前提是它的整数编号不会被其他域使用。对于删除的字段,名字前面可添加“OBSOLETE_”以防止其他字段使用它的整数编号。

(4) thrift文件应该是unix格式的(windows下的换行符与unix不同,可能会导致你的程序编译不过),如果是在window下编写的,可使用dos2unix转化为unix格式。

(5)  貌似当前的thrift版本(0.6.1)不支持常量表达式的定义(如 const i32 DAY = 24 * 60 * 60),这可能是考虑到不同语言,运算符不尽相同。

http://dongxicheng.org/search-engine/thrift-guide/


你可能感兴趣的:(Thrift RPC 远程调用)