Python RPC 之 Thrift

thrift-0.12.0  python3.4.3

Thrift 簡介:

Thrift 是一款高性能、開源的 RPC 框架,產自 Facebook 後貢獻給了 Apache,Thrift 囊括了整個 RPC 的上下游體系,自帶序列化編譯工具,由於 Thrift 採用的是二進制序列化,而且與 gRPC 同樣使用的都是長鏈接創建 client 與 server 之間的通信,相比於比傳統的使用XML,JSON,SOAP等短鏈接的解決方案性能要快得多。
本篇只介紹 Python 關於 Thrift 的基礎使用。python

安裝

  • 安裝 Thrift 的 python 庫有兩種方案:
  1. 經過 pip 命令安裝,缺點必需要有外網或者內網的 pip 源:$ pip install thrift
  1. 經過源碼安裝:從 github 上下載 thrift 0.10.0 的源碼 ,解壓後進入 thrift-0.10.0/lib/py 目錄執行:$ python setup.py install
  • 安裝 Thrift 的 IDL 編譯工具
    1. windows 平臺下安裝:

    直接下載:thrift complier 下載地址,下載完成後更名爲:thrift.exe 並將其放入到系統環境變量下便可使用git

  1. Linux 平臺下安裝:

從 github 上下載 thrift 0.10.0 的源碼,解壓後進入:thrift-0.10.0/compiler/cpp 目錄執行以下命令完成編譯後,將其放入到系統環境變量下便可使用:
$ mkdir cmake-build
$ cd cmake-build
$ cmake ..
$ makegithub

  • 驗證是否安裝成功:

$ thrift -version,若是打印出來:Thrift version 0.10.0 代表 complier 安裝成功apache

實踐:

下面咱們使用 Thrift 定義一個接口,該接口實現對傳入的數據進行大寫的格式化處理。windows

  • 建立 python 項目 thrift_demo 工程:
 
 
Paste_Image.png
  1. client目錄下的 client.py 實現了客戶端用於發送數據並打印接收到 server 端處理後的數據
  1. server 目錄下的 server.py 實現了服務端用於接收客戶端發送的數據,並對數據進行大寫處理後返回給客戶端
  2. thrift_file 用於存放 thrift 的 IDL 文件: *.thrift
  • 定義 Thrift RPC 接口

example.thrift:app

namespace py example

struct Data {
    1: string text
}

service format_data {
    Data do_format(1:Data data),
}
  • 編譯 thrift 文件

進入 thrift_file 目錄執行:$ thrift -out .. --gen py example.thrift,就會在 thrift_file 的同級目錄下生成 python 的包:example框架

 
 
Paste_Image.png
  • 實現 server 端:

server.py:socket

#! /usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'xieyanke'

from example import format_data
from example import ttypes
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

__HOST = 'localhost'
__PORT = 8080

class FormatDataHandler(object):
    def do_format(self, data):
        return ttypes.Data(data.text.upper())


if __name__ == '__main__':
    handler = FormatDataHandler()

    processor = format_data.Processor(handler)
    transport = TSocket.TServerSocket(__HOST, __PORT)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    rpcServer = TServer.TSimpleServer(processor,transport, tfactory, pfactory)

    print('Starting the rpc server at', __HOST,':', __PORT)
    rpcServer.serve()
  • 實現 client 端:

client.py:工具

#! /usr/bin/env python
# -*- coding: utf-8 -*-

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from example.format_data import Client
from example.format_data import Data

__HOST = 'localhost'
__PORT = 8080

tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Client(protocol)

data = Data('hello,world!')
transport.open()

print(client.do_format(data).text)
  • 執行驗證結果:
  1. 先啓動 server,以後再執行 client
  1. client 側控制檯若是打印的結果爲: HELLO,WORLD! ,證實 Thrift 的 RPC 接口定義成功
注意 import sys sys.path.append(r"X:\xx\thrift_demo") #thrift_demo 目錄的絕對路徑
相關文章
相關標籤/搜索