一、安装过程

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. EntityFramework Core是否可以映射私有属性呢?了解一下。

    前言 最近几天身体有点抱恙,说话都需要勇气,痛哭.今天简短的写一点探索性的内容,仅供了解,感谢您的阅读. EF Core映射私有属性 在EF 6.x系列中写过一篇文章可以映射私有属性,说明EF的灵活性 ...

  2. 使用原生 JS 复制文本兼容移动端 iOS & android

    注意事项 使用 JS 实现复制功能并不是很难,但是有几个需要注意的地方. 首先文本只有选中才可以复制,所以简单的做法就是创建一个隐藏的 input,然后绑定需要复制的文本. 另外如果将 input 设 ...

  3. Tree 和ls 的使用

    再次声明:linux下的文件系统采用树的结构实现的 我们 可以安装 Tree 软件 在当前目录下(随便一个当前目录下)输入 tree 命令,我们可以看到整个当前文件目录下的目录以及文件的树状结构,这也 ...

  4. 制作自己cocoapods库

    https://www.cnblogs.com/czc-wjm/p/5958103.html 今天来讲一下cocoapods制作,网上教程很多,就不再讲理论,直接操作: 1.创建仓库: 2.将仓库克隆 ...

  5. c++使用cmake创建dpdk项目

    使用cmake创建dpdk 特别注意的时,链接dpdk库时,一定要使用 -Wl,--whole-archive 和 -Wl,--no-whole-archive 包含所有的静态库,注意,不要链接 li ...

  6. varnish与squid缓存效率对比实例

    前提:安装varnish.squid.webbench(压测工具) 注:varnish和squid机都未安装其他多余服务,服务器绑定域名为www.dannylinux.top  (为同一台服务器,测试 ...

  7. MySQL安装后无法用root用户访问的问题

    今天在换了Ubuntu后装个本地的mysql,安装过程没什么好说的:sudo apt-get install mysql-server 安装好了之后我做了以下一系列常规动作: 1.$sudo mysq ...

  8. vs code 的便捷使用

    鼠标滚动 改变字体大小 打开编辑器设置,搜索 editor.mouseWheelZoom  或者文本设置 自动保存 打开设置 搜索  autosave

  9. 使用ubuntu做为dotnet core开发环境

    一.安装google浏览器 1.下载安装包(传送门:http://www.google.cn/intl/zh-CN/chrome/browser/desktop/index.html) 2.使用sud ...

  10. CentOS 常用Linux命令

    防火墙 开放端口 firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效) 重启 ...