thrift安装及python和c++版本调试

Posted Jayson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了thrift安装及python和c++版本调试相关的知识,希望对你有一定的参考价值。

一、安装过程

1.安装依赖库

]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant

2.安装thrift

先下载thrift-0.9.3.tar.gz,解压后进入thrift-0.9.3目录

//需要支持的语言用--with, 不需要支持的语言用--without, 像ruby等语言最好去掉,否则可能会有一些兼容问题
]# ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell  --without-go
]# make
]# make install
//成功会显示 BUILD SUCCESSFUL,通过thrift命令查看是否安装成功
]# thrift 
//安装Thrift的时候遇到,如下错误
#./configure --prefix=/usr/local/thrift
trhift configure: error: "Error: libcrypto required."
//解决办法:
//安装 openssl openssl-devel (centOS)
#yum -y install openssl openssl-devel
# ./configure --prefix=/usr/local/thrift

二、调通单机版thrift,python版本

1.安装依赖库

]# pip install thrift==0.9.3

2.编写schema文件

//创建schema目录,创建一个schema文件RecSys.thrift
[root@localhost schema]# cat RecSys.thrift 
service RecSys{
    string rec_data(1:string data)
}

3.使用thrift生成python文件,产生gen-py目录

]# thrift --gen python RecSys.thrift

4.开发python代码

// client代码:
#coding=utf=8

import sys
sys.path.append(\'../schema/gen-py\')

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from RecSys import RecSys


try:
    # 设置端口
    transport = TSocket.TSocket(\'localhost\', port=9090)

    # 设置传输层
    transport = TTransport.TBufferedTransport(transport)

    # 设置传输协议
    protocol = TBinaryProtocol.TBinaryProtocol(transport)

    client = RecSys.Client(protocol)

    transport.open()

    rst = client.rec_data("are you ok!!!")
    print "receive return data: ", rst

    transport.close()

except Thrift.TException, ex:
    print "%s" % (ex.message)

// server 代码
#coding=utf=8

import sys
sys.path.append(\'../schema/gen-py\')

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

from RecSys import RecSys
from RecSys.ttypes import *

class RecSysHandler(RecSys.Iface):
    def rec_data(self, a):
        print "Receive: %s" %(a)
        return "I\'m OK !!!"


if __name__ == "__main__":

    # 实例化handler
    handler = RecSysHandler()

    # 设置processor
    processor = RecSys.Processor(handler)

    # 设置端口
    transport = TSocket.TServerSocket(\'localhost\', port=9090)

    # 设置传输层
    tfactory = TTransport.TBufferedTransportFactory()

    # 设置传输协议
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)

    print \'Starting the server...\'
    server.serve()
    print \'done.\'

三、调通单机版thrift,c++版本

1.使用thrift生成c++文件,产生gen-cpp目录

//在schema目录下执行
]# thrift --gen cpp RecSys.thrift

2.进入gen-cpp目录,编译生成server的bin文件

]# g++ -g -Wall -I/usr/local/include/thrift RecSys_constants.cpp  RecSys.cpp  RecSys_server.skeleton.cpp RecSys_types.cpp -lthrift -o server

3.修改server代码(RecSys_server.skeleton.cpp)

 23   void rec_data(std::string& _return, const std::string& data) {
 24     // Your implementation goes here
 25     std::cout << "Recevie data: " << data << std::endl;
 26     _return = "I\'m OK !!!";
 27   } 

4.执行server代码

]# ./server
//如果执行报错: libthrift-0.9.3.so: cannot open shared object file: No such file or directory
解决方法:
]# vim /etc/ld.so.conf
在末尾添加下面一行
/usr/local/lib/
然后再执行:
]# ldconfig

5.执行python的client端代码

//python的client端调用c++的server端
]# python client.py

6.可以写一个Makefile文件来编译生成server的bin文件

在gen-cpp目录下创建一个Makefile文件

GXX = g++
FLAGS = -g -Wall
INCLUDES = -I/usr/local/include/thrift
LIBS = -L/usr/local/lib/*.so -lthrift

SERVER_OBJECTS = RecSys_constants.cpp  RecSys.cpp RecSys_server.skeleton.cpp RecSys_types.cpp
CLIENT_OBJECTS = RecSys.cpp client.cpp

server:
	$(GXX) $(INCLUDES) $(SERVER_OBJECTS) $(LIBS) -o server

client:
	$(GXX) $(INCLUDES) $(CLIENT_OBJECTS) $(LIBS) -o client

.PHONY: clean 
clean:
	rm -rf server
//备注:.PHONY: clean 是为了防止当前目录下有clean同名文件,导致clean删除命令无法执行

7.编写c++的client端代码

#include "RecSys.h"
#include <iostream>
#include <string>

#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace std;


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

    boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

    transport->open();

    RecSysClient client(protocol);

    string send_data = "can you help me ???";
    string recevie_data;
    client.rec_data(recevie_data, send_data);

    cout << "Send data:" << send_data << endl;
    cout << "Receive data:" << recevie_data << endl;

    transport->close();
}

以上是关于thrift安装及python和c++版本调试的主要内容,如果未能解决你的问题,请参考以下文章

c++访问hbase

Thrift安装介绍

Apache thrift 安装及使用

thrift在C++中的应用

如何在 perforce 和 VMS 上为 C++ 进行代码审查/调试/编码/测试/版本控制

用 C++ 编写的 Apache Thrift 服务器抛出 TTransportException