RPC应用

发布于:2022-12-06 ⋅ 阅读:(880) ⋅ 点赞:(0)

linux安装thrift工具包:

step 1 -> 安装依赖:

sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config

step 2 -> 下载这几个包:

libevent-master.zip、boost_1_80_0.tar.gz、thrift-master.zip;

step 3 -> 安装libevent:

./autogen.sh

./configure

make -j4

make install

step 4 -> 安装boost:

cd boost_1_80_0

./bootstrap.sh

./b2 install

step 5 -> 安装boost:

./bootstrap.sh

./configure --with-qt5=no --with-cl=no --with-java=no --with-php=no --with-python=no --disable-tests

# 参数说明

# --with-qt5=no 不编译thrift的qt5模块

# --disable-tests 不编译单元测试模块(会报错,但是影响也不大)

make -j4

make install

thrift --version

默认安装在/usr/local/lib /usr/local/bin /usr/local/include

linux安装grpc工具包:

pip install grpcio-tools

thrift和grpc封装:

thrift封装:

Step1:写好thrift文件;

举例:

struct Status {

    1: i32 code, //状态码

    2: string msg, //返回信息

    3: string version //版本信息

}

struct AlgRsp {

    1: string result,

    2: Status status,

}

struct AlgReq {

    1: string filename,

}

service PdfOCRServer{

    AlgRsp handler(1:AlgReq req)

}

Step2:thrift --gen py pdf_ocr.thrift

grpc封装:

Step1:写好proto;

举例:

syntax = "proto3";

package pdf_ocr;

import 'data.proto';

service PdfOCR {

    rpc pdf_ocr (pdf_ocr.Request) returns (pdf_ocr.Reply) {}

}

message Request {

    string filename = 1;

}

message Reply {

    string result = 1;

    common.Status status = 2;

}

Step2:python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. pdf_ocr.proto

安装thrift和grpc运行时工具包:

pip install thrift

pip install grpcio