1. 程式人生 > >thrift安裝及python和c++版本調試

thrift安裝及python和c++版本調試

最好 查看 IE 分享 -- 通過 error: rom tool

一、安裝過程

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文件

]# thrift --gen python RecSys.thrift
//產生gen-py目錄

技術分享圖片

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安裝及python和c++版本調試