thrift 依赖libevent库和boost,需要先安装这两个
libevent 安装
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/home/xxxx/software/ (自己指定目录需要)
make && make install
boost安装
wget
http://jaist.dl.sourceforge.net/project/boost/boost/1.54.0/boost_1_54_0.tar.gz
tar zxvf boost_1_54_0.tar.gz
./bootstrap.sh --prefix=/home/xxx/software/
./b2 install
thrift安装
wget
http://mirror.esocc.com/apache/thrift/0.9.1/thrift-0.9.1.tar.gz
tar zxvf thrift-0.9.1.tar.gz
cd thrift-0.9.1
./configure --prefix=/home/xxx/software/ --with-boost=/home/xxx/software/ --with-libevent=/home/xxx/software/ --with-php_extension=no
如果不在默认目录则需要使用上面的参数指定boost libevent所在目录 没有php则不要启用php_extension python也一样
出错,则根据出错信息修改
#include <tr1/functional> 为
#include <boost/tr1/tr1/functional>
make &&make install
测试
thrift文件 hello.thrift
namespace cpp test
struct hello {
1: i32 id,
2: string name,
}
service EchoService {
string echo(1: string cmd);
hello echo2(1: hello cmd);
}
执行thrift --gen cpp hello.thrift 生成代码 在gen-cpp目录下
编写server代码server.cpp:
#include "gen-cpp/EchoService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using namespace test;
using namespace boost;
using boost::shared_ptr;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
class EchoService : virtual public EchoServiceIf {
public:
EchoService() {
}
void echo(std::string& ret, const std::string& cmd) {
printf("cmd is %s\n", cmd.c_str());
ret = cmd;
printf("ret is %s\n", ret.c_str());
}
void echo2(hello& ret, const hello& cmd) {
printf("cmd.id is %d\n", cmd.id);
printf("cmd.name is %s\n", cmd.name.c_str());
ret =cmd;
}
};
int main(int argc, char** argv) {
int port = 9090;
boost::shared_ptr<EchoService> handler(new EchoService());
boost::shared_ptr<TProcessor> processor(new EchoServiceProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transportFactory(new TFramedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocol(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocol);
server.serve();
return 0;
}
编写client.cpp 代码
include "gen-cpp/EchoService.h"
#include <thrift/transport/TSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
using boost::shared_ptr;
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace test;
int main(int argc, char** argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
EchoServiceClient client(protocol);
transport->open();
std::string ret;
client.echo(ret, "hello world");
printf("ret is %s\n", ret.c_str());
hello obj_h;
obj_h.id = 3;
obj_h.name = "hello world 2";
hello obj_r;
client.echo2(obj_r, obj_h);
printf("obj_r.id is %d\n", obj_r.id);
printf("obj_r.name is %s\n", obj_r.name.c_str());
transport->close();
return 0;
}
编译:
g++ -g -I/home/xxx/software/include -L/home/xxx/software/lib/ -lthrift EchoService.cpp hello_constants.cpp hello_types.cpp EchoService_server.skeleton.cpp -o server (使用生成的示例server)
g++ -g -I/home/xxx/software/include -L/home/xxx/software/lib/ -lthrift ./gen-cpp/EchoService.cpp ./gen-cpp/hello_constants.cpp ./gen-cpp/hello_types.cpp server.cpp -o server (自己编写的server)
g++ -g -I/home/xxx/software/include -L/home/xxx/software/lib/ -lthrift ./gen-cpp/EchoService.cpp ./gen-cpp/hello_constants.cpp ./gen-cpp/hello_types.cpp client.cpp -o client (自己编写的client)
执行结果
./server
cmd is hello world
ret is hello world
cmd.id is 3
cmd.name is hello world 2
cmd is hello world
ret is hello world
cmd.id is 3
cmd.name is hello world 2
./client
ret is hello world
obj_r.id is 3
obj_r.name is hello world 2
ret is hello world
obj_r.id is 3
obj_r.name is hello world 2