温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

「Protocol Buffer」之PB在gRPC中的应用

发布时间:2020-07-26 00:13:47 来源:网络 阅读:411 作者:恒河猴 栏目:开发技术

相关库的安装

$ python -m pip ×××tall grpcio
$ python -m pip ×××tall grpcio-tools googleapis-common-protos

Demo程序功能概述

服务器端存在Test_service类中定义了my_function方法,客户端通过gRPC协议进行远程调用;该方法实现的功能是将接受到的字符串内容全部改为大写并返回

PB接口描述文件定义

syntax = "proto3";
package data;

#定义数据结构
message Data{
    string text=1;
}

#定义服务接口,即声明我要调用目标服务器上的哪个函数
service Test_service{
    #定义调用函数名称、参数类型与返回数据类型
    rpc my_function(Data) returns (Data) {}
}

编译

python3 -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. ./data.proto

客户端代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import grpc
import data_pb2, data_pb2_grpc

_HOST = '127.0.0.1'
_PORT = '8080'

def run():
    conn = grpc.×××ecure_channel(_HOST + ':' + _PORT)
    client = data_pb2_grpc.Test_serviceStub(channel=conn)
    response = client.my_function(data_pb2.Data(text='hello,world!'))
    print("received: " + response.text)

if __name__ == '__main__':
    run()

服务器代码

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import grpc
import time
from concurrent import futures
import data_pb2, data_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24
_HOST = '127.0.0.1'
_PORT = '8080'

class Test_service(data_pb2_grpc.Test_serviceServicer):
    def my_function(self, request, context):
        str = request.text
        return data_pb2.Data(text=str.upper())

def serve():
    grpcServer = grpc.server(futures.ThreadPoolExecutor(max_workers=4))
    data_pb2_grpc.add_Test_serviceServicer_to_server(Test_service(), grpcServer)
    grpcServer.add_×××ecure_port(_HOST + ':' + _PORT)
    grpcServer.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        grpcServer.stop(0)

if __name__ == '__main__':
    serve()

客户端运行结果

$ python3 client.py 
received: HELLO,WORLD!

远程方法调用成功!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI