Thrift安装部署

下载源码包

wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz

安装g++

centos:yum install gcc gcc-c++

如果没有安装g++,无法编译

解压Thrift安装包

tar -xvf thrift-0.9.3.tar.gz

安装boost开发工具

  1. 进入thrift-0.9.3目录

  2. 运行命令:yum install boost-devel.x86_64

  3. 运行命令:yum install boost-devel-static

  4. 运行命令:./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go

  5. 编译,命令:make

  6. 编译完成之后安装,命令:make install

  7. 出现thrift命令提示表示Thrift安装成功

测试(python版)

  1. 创建RecSys.thrift文件
service RecSys {
string rec_data(1:string data)
}
  1. 创建server.py文件
#! /usr/bin/env python
# -*- coding: utf-8 -*- import sys
sys.path.append('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 "ok" if __name__ == "__main__": # 实例化handler
handler = RecSysHandler() # 设置processor
processor = RecSys.Processor(handler) # 设置端口
transport = TSocket.TServerSocket('localhost', port=9900) # 设置传输层
tfactory = TTransport.TBufferedTransportFactory() # 设置传输协议
pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print 'Starting the server...'
server.serve()
print 'done'
  1. 创建client.py文件
#! /usr/bin/env python
# -*- coding: utf-8 -*- import sys
sys.path.append("gen-py") // 将第4步骤产生的目录加载进来 from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol from RecSys import RecSys
# from demo.ttypes import * try:
# Make Socket
# 建立socket, IP 和port要写对
transport = TSocket.TSocket('localhost', 9900) # Buffering is critical. Raw sockets are very slow
# 选择传输层,这块要和服务器的设置一样
transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol
# 选择传输协议,这个也要和服务器保持一致,负责无法通信
protocol = TBinaryProtocol.TBinaryProtocol(transport) client = RecSys.Client(protocol) # Connect!
transport.open() # Call server services
rst = client.rec_data("are you ok!")
print rst # close transport
transport.close()
except Thrift.TException, ex:
print "%s" % (ex.message)
  1. 运行命令:thrift --gen py RecSys.thrift

    • 运行完之后会在当前目录生成一个gen-py的文件夹,里面有模块的名字之类的东西

    • 运行命令:python server.py,启动服务端

    • 运行命令:python client.py,启动客户端,并能收到服务端发出的信息

最新文章

  1. c++局域网多播
  2. C# Winform防止一个程序重复运行
  3. 用遗传算法GA改进CloudSim自带的资源调度策略
  4. 用CMake屏蔽Release运行时的控制台窗口
  5. js三级省市区选择
  6. struts的hello world小试
  7. 使用Ganglia监控hadoop、hbase
  8. win8图片默认不显示
  9. 彻底搞清js中闭包(Closure)的概念
  10. JQuery中根据属性或属性值获得元素
  11. Cocos2d粒子系统二
  12. Cocos2d-x3.0 lua捆绑C++分类
  13. ural1471 Distance in the Tree
  14. 米扑科技的开源项目:sitemap-php 自动生成网站地图
  15. 传统业务上云:跨AZ容灾架构解析
  16. 2018-4-12 数学建模MATLAB常用的一些函数
  17. 多线程 ThreadLocal
  18. C++获取文件夹下所有文件名
  19. Oracle学习笔记之四sp1,Oracle 11g的常用函数
  20. Pytorch之Variable求导机制

热门文章

  1. matlab练习程序(多线段交点)
  2. 成功解决:FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is
  3. 长大Tips的第一步
  4. SQA冲刺
  5. MovieReview—Ghost in the shell(攻壳机动队95版)
  6. CentOS 6.4安装谷歌浏览器(chromium)不是chrome
  7. css relative
  8. HDU 1083 Courses 【二分图完备匹配】
  9. activeMQ消息队列的使用
  10. fastJson简单实用