1、简介
Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架。它有一个代码生成器来对它所定义的IDL定义文件自动生成服务代 码框 架。用户只要在其之前进行二次开发就行,对于底层的RPC通讯等都是透明的。目前它支持的语言有C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.
环境:
OS:Ubuntu 12.04.1 LTS
内核:3.2.0-33-generic-pae
2、下载thrift
1
|
wget http:
//labs
.renren.com
/apache-mirror//thrift/0
.9.0
/thrift-0
.9.0.
tar
.gz(下载版本为0.9.0)
|
3、安装依赖包
1
|
sudo
apt-get
install
libboost-dev libboost-
test
-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant
|
4、解压
1
|
tar
-zxvf thrift-*.
tar
.gz
|
进入目录cd thrift-*
5、安装编译
1
2
3
|
.
/configure
make
sudo
make
install
|
6、安装测试
安装后thrift -version查看版本信息,表示安装成功。
Thrift version 0.9.0
7、自带例子测试
thrift中有自带的例子,在tutorial目录内,我们测试其中cpp的例子。
进入tutorial目录,shared.thrift和tutorial.thrift是接口定义文件。
执行 thirft -r --gen cpp tutorial.thrift,可以生成gen-cpp目录,这是thrift编译器自动生成的代码。
然后进入tutorial/cpp目录,执行make,生成可执行文件。不过make之后会有各式各样的错误,我们一一改正。
8、make之后可能出现的错误及改正
(1) 错误1:非常多的错误,找到第一个错误是
大意是/usr/local/include/thrift/concurrency/Thread.h中的pthread_self、pthread_equal等没有声明,搜了一下好像很少有人遇到过这个问题。不过不打紧,错误的意思可以理解,搜到pthread_self、pthread_equal对应的头文件是#include <pthread.h>,加到那个Thread.h中就好了。
(2)错误2:/usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147:35: 错误: ‘htons’的实参不依赖模板参数,
解决:‘htons’的声明必须可用[-fpermissive],则修改Makefile,加上编译选项-DHAVE_NETINET_IN_H
修改后的Makefile(部分)
1
2
3
4
|
server: CppServer.cpp
g++ -DHAVE_NETINET_IN_H -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} -I..
/gen-cpp
-L${LIB_DIR} -lthrift CppServer.cpp ${GEN_SRC}
client: CppClient.cpp
g++ -DHAVE_NETINET_IN_H -o CppClient -I${THRIFT_DIR} -I${BOOST_DIR} -I..
/gen-cpp
-L${LIB_DIR} -lthrift CppClient.cpp ${GEN_SRC}
|
(3)错误3:仍然是非常多的错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
g++ -DHAVE_NETINET_IN_H -o CppServer -I
/usr/local/include/thrift
-I
/usr/local/boost/include/boost-1_33_1/
-I..
/gen-cpp
-L
/usr/local/lib
-lthrift CppServer.cpp ..
/gen-cpp/SharedService
.cpp ..
/gen-cpp/shared_types
.cpp ..
/gen-cpp/tutorial_types
.cpp ..
/gen-cpp/Calculator
.cpp
/tmp/cc3gKFHw
.o: In
function
`main':
CppServer.cpp:(.text+0xd1): undefined reference to `apache::thrift::transport::TServerSocket::TServerSocket(int)'
CppServer.cpp:(.text+0x15f): undefined reference to `apache::thrift::server::TSimpleServer::serve()'
/tmp/cc3gKFHw
.o: In
function
`apache::thrift::server::TSimpleServer::~TSimpleServer()':
CppServer.cpp:(.text._ZN6apache6thrift6server13TSimpleServerD2Ev[_ZN6apache6thrift6server13TSimpleServerD5Ev]+0xb): undefined reference to `vtable
for
apache::thrift::server::TSimpleServer'
/tmp/cc3gKFHw
.o: In
function
`apache::thrift::transport::TBufferedTransport::TBufferedTransport(boost::shared_ptr<apache::thrift::transport::TTransport>)':
CppServer.cpp:(.text._ZN6apache6thrift9transport18TBufferedTransportC2EN5boost10shared_ptrINS1_10TTransportEEE[_ZN6apache6thrift9transport18TBufferedTransportC5EN5boost10shared_ptrINS1_10TTransportEEE]+0x17): undefined reference to `vtable
for
apache::thrift::transport::TBufferedTransport'
/tmp/cc3gKFHw
.o: In
function
`apache::thrift::server::TSimpleServer::TSimpleServer<apache::thrift::TProcessor>(boost::shared_ptr<apache::thrift::TProcessor> const&, boost::shared_ptr<apache::thrift::transport::TServerTransport> const&, boost::shared_ptr<apache::thrift::transport::TTransportFactory> const&, boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> const&, boost::enable_if<boost::is_convertible<apache::thrift::TProcessor*, apache::thrift::TProcessor*>::
type
, void*>::
type
)':
CppServer.cpp:(.text._ZN6apache6thrift6server13TSimpleServerC2INS0_10TProcessorEEERKN5boost10shared_ptrIT_EERKNS6_INS0_9transport16TServerTransportEEERKNS6_INSB_17TTransportFactoryEEERKNS6_INS0_8protocol16TProtocolFactoryEEENS5_9enable_ifINS5_14is_convertibleIPS7_PS4_E4typeEPvE4typeE[_ZN6apache6thrift6server13TSimpleServerC5INS0_10TProcessorEEERKN5boost10shared_ptrIT_EERKNS6_INS0_9transport16TServerTransportEEERKNS6_INSB_17TTransportFactoryEEERKNS6_INS0_8protocol16TProtocolFactoryEEENS5_9enable_ifINS5_14is_convertibleIPS7_PS4_E4typeEPvE4typeE]+0x3a): undefined reference to `vtable
for
apache::thrift::server::TSimpleServer'
/tmp/ccBCSkj6
.o: In
function
`shared::SharedServiceClient::recv_getStruct(shared::SharedStruct&)':
SharedService.cpp:(.text+0x6f3): undefined reference to `apache::thrift::TApplicationException::
read
(apache::thrift::protocol::TProtocol*)'
/tmp/ccBCSkj6
.o: In
function
`shared::SharedServiceProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)':
SharedService.cpp:(.text+0xb05): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccBCSkj6
.o: In
function
`shared::SharedServiceProcessor::process_fn(apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, void*)':
SharedService.cpp:(.text+0xd87): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccBCSkj6
.o: In
function
`shared::SharedServiceProcessor::process_getStruct(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)':
SharedService.cpp:(.text+0x1409): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccdeMoGV
.o: In
function
`tutorial::CalculatorClient::recv_ping()':
Calculator.cpp:(.text+0x12af): undefined reference to `apache::thrift::TApplicationException::
read
(apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV
.o: In
function
`tutorial::CalculatorClient::recv_add()':
Calculator.cpp:(.text+0x173b): undefined reference to `apache::thrift::TApplicationException::
read
(apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV
.o: In
function
`tutorial::CalculatorClient::recv_calculate()':
Calculator.cpp:(.text+0x1c7b): undefined reference to `apache::thrift::TApplicationException::
read
(apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV
.o: In
function
`tutorial::CalculatorProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)':
Calculator.cpp:(.text+0x227f): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const
|
解决: 把-lthrift放到后面去
修改后的Makefile
1
2
3
4
5
6
7
8
9
10
11
|
BOOST_DIR =
/usr/local/boost/include/boost-1_33_1/
THRIFT_DIR =
/usr/local/include/thrift
LIB_DIR =
/usr/local/lib
GEN_SRC = ..
/gen-cpp/SharedService
.cpp ..
/gen-cpp/shared_types
.cpp ..
/gen-cpp/tutorial_types
.cpp ..
/gen-cpp/Calculator
.cpp
default: server client
server: CppServer.cpp
g++ -DHAVE_NETINET_IN_H -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} -I..
/gen-cpp
-L${LIB_DIR} CppServer.cpp ${GEN_SRC} -lthrift
client: CppClient.cpp
g++ -DHAVE_NETINET_IN_H -o CppClient -I${THRIFT_DIR} -I${BOOST_DIR} -I..
/gen-cpp
-L${LIB_DIR} CppClient.cpp ${GEN_SRC} -lthrift
clean:
$(RM) -r CppClient CppServer
|
9、运行
再次执行make,运行成功,得到可执行程序CppServer和CppClient
在两个窗口依次运行./CppServer和./CppClient
输出如下:
1
2
3
4
5
6
|
$ .
/CppClient
ping
()
1+1=2
InvalidOperation: Cannot divide by 0
15-10=5
Check log: 5
|
1
2
3
4
5
6
7
|
$ .
/CppServer
Starting the server...
ping
()
add(1,1)
calculate(1,{4,1,0})
calculate(1,{2,15,10})
getStruct(1)
|