一、安装过程

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++版本调试的更多相关文章

  1. 【python】如何查看已经安装的python软件包和版本

    pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. pip freeze可以查看已经安装的python软件包和版本 pip list 也可以

  2. Pycharm选择pyenv安装的Python版本

    在macOS上使用pyenv实现Python多版本共存后,pyenv安装的Python版本存在于macOS下的 ~/.pyenv/versions/下. 在Pycharm时,选择此目录下对应的版本即可 ...

  3. windows安装多个python及pip版本

    windows安装多个python及pip版本 1.下载所需要的python2和python3安装包 2.一路next 3.设置环境变量 4.修改python安装目录下的可执行程序名称 5.在cmd中 ...

  4. 安装的 Python 版本太多互相干扰?pyenv 建议了解一下。

    写在之前 我们都知道现在的 Python 有 Python2 和 Python3,但是由于各种乱七八糟的原因导致这俩哥们要长期共存,荣辱与共,尴尬的是这哥俩的差异还比较大,在很多时候我们可能要同时用到 ...

  5. python的多版本安装以及常见错误(长期更新)

    (此文长期更新)Python安装常见错误汇总 注:本教程以python3.6为基准 既然是总结安装过程中遇到的错误,就顺便记录一下我的安装过程好了. 先来列举一下安装python3.6过程中可能需要的 ...

  6. 查看python和NumPy版本和安装路径

    记录查看Python和NumPy版本以及路径的几条命令 # 查看Python版本及路径 python -V python -c "import sys;print(sys.executabl ...

  7. Python的安装和配置(windowns 双版本)

    1.去官网上下载python,注意版本. 官网地址:https://www.python.org/downloads/windows 2.下载安装版或者zip包都可以.安装就按向导一步一步完成即可.z ...

  8. windows 如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中

    题目]如何将安装Anaconda之前已经安装的python版本(中已安装的库)移动到 Anaconda中 一.概述 之前安装tensorflow的安装了anaconda并用它进行安装,anaconda ...

  9. thrift安装及常见问题

    一.安装thrift (macOS / Linux) 1. 下载thrift0.10.0源码 https://github.com/apache/thrift/releases/tag/0.10.0 ...

随机推荐

  1. FB商务管理平台(Business Manager) (2)

    Business Manager 商务管理平台(以下简称BM)API 一站式管理广告帐户.主页及相关的工作人员. BM功能结构(其中:账户下的节点属于市场营销API) API / SDK FB提供了多 ...

  2. input type类型和input表单属性

    一.input type类型 1.Input 类型 - email 在提交表单时,会自动验证 email 域的值. E-mail: <input type="email" n ...

  3. random使用方法

    random.random() 没有参数,选择0到1之间的随机浮点数 random.uniform(a, b) 生成指定范围内的随机浮点数如果a.b哪个大那个小都没关系,生成的都是在小的与大的之间的随 ...

  4. C#需要在项目程序生成前后执行相关的事件

    分享4: 需求:需要在项目程序生成前后执行相关的事件,比如:需要将某个文件拷贝到bin\Debug中,或者创建某文件夹等. 分析:我们可利用项目属性(选择项目右键,选择属性)中的“生成事件”预定义相关 ...

  5. 暂时禁止Cnario Player开机自动启动的办法

    如果暂时不需要播放器开机后启动Cnario Player, 有两种办法 从Windows启动菜单禁用Cnario Player 在Windows的任务管理器中, 找到启动标签栏, 从里面找到Cnari ...

  6. cpu_relax

    https://blog.csdn.net/justlinux2010/article/details/8533451

  7. 解决Windows10中Virtualbox安装虚拟机没有64位选项

    今天想在Windows 10系统安装完Virtualbox虚拟机,然后打算装一个CENTOS系统,但是选择安装系统的时候竟然没有64位操作系统的选项,经过一阵Google,终于解决了,在这里盘点一下出 ...

  8. sql 查询字段如果为null 则返回0的写法

    oracle select nvl(字段名,0) from 表名; ----------------------------------- sqlserver select isnull(字段名,0) ...

  9. 消息队列与Kafka

    2019-04-09 关键词: 消息队列.为什么使用消息队列.消息队列的好处.消息队列的意义.Kafka是什么 本篇文章系本人就当前所掌握的知识关于 消息队列 与 kafka 知识点的一些简要介绍,不 ...

  10. 完全理解 Python 迭代对象、迭代器、生成器(转)

    完全理解 Python 迭代对象.迭代器.生成器 本文源自RQ作者的一篇博文,原文是Iterables vs. Iterators vs. Generators » nvie.com,俺写的这篇文章是 ...