一、安装过程

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. vue项目接口域名动态获取

    需求: 接口域名是从外部 .json 文件里获取的. 思路: 在开始加载项目前 进行接口域名获取,然后重置 接口域名的配置项. 实现: 1.config/index.js 文件 进行基础配置 impo ...

  2. Photoshop入门教程图解版

  3. Photoshop给人像加上个性裂纹肌肤

    1.打开人物及纹理素材图片,把素材图片拖到人物图片里面,适当降低图层不透明度. 2.选择菜单:编辑 > 变形 > 自由变形,使纹理位置合适,然后确定. 3.用橡皮工具擦除多余的地方. 4. ...

  4. 使用Docker安装Oracle数据库

    在很多时候,我们需要在本地安装Oracle数据库,但是整个安装的过程时间非常长而且安装文件大,那么有不有更好的办法来安装Oracle数据库既能减少安装的时间而且还能够快速进行部署呢?答案就是使用Doc ...

  5. Java 学习(1)----- java 学习的总体感觉

    好久没有更新博客了,是因为最近在集中精力学习java, Java 的基础知识确实是比 js 多太多了. 学习java 断断续续的差不多有一年左右的时间, 这一年来,感觉懂了一点,过一段时间又忘记了,总 ...

  6. anaconda相关使用方法

    本文不涉及anaconda的安装,如果需要请自行搜索,cnblogs和CSDN都挺多的. conda安装完,大部分人都jupyter notebook的使用需求,jupyter的开启命令是什么呢? j ...

  7. django rest framework serializers

    django rest framework serializers序列化   serializers是将复杂的数据结构变成json或者xml这个格式的 serializers有以下几个作用:- 将qu ...

  8. ZuulFilter 执行顺序

    说明: 创建了两个Filter,分别是 PreFilter public class PreFilter extends ZuulFilter { public PreFilter() { super ...

  9. Python 目录指引

    1.0 Python 基础整合 1.1 变量 1.2 数据类型 1.3 基础语法 1.4 文件操作 1.5 函数 1.6 生成器 1.7 迭代器 1.8 装饰器 1.9 字符集 2.0 Python ...

  10. Keras 获取中间某一层输出

    1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要的那一层的输出,然后重新进行predict. #coding=utf-8 import seaborn as ...