温馨提示×

温馨提示×

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

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

怎么使用Python搭建gRPC服务

发布时间:2021-06-30 13:51:16 来源:亿速云 阅读:120 作者:小新 栏目:开发技术

这篇文章主要介绍了怎么使用Python搭建gRPC服务,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

一、概述

一个gRPC服务的大体结构图为:

怎么使用Python搭建gRPC服务

图一表明,grpc的服务是跨语言的,但需要遵循相同的协议(proto)。相比于REST服务,gPRC 的一个很明显的优势是它使用了二进制编码,所以它比 JSON/HTTP 更快,且有清晰的接口规范以及支持流式传输,但它的实现相比rest服务要稍微要复杂一些,下面简单介绍搭建gRPC服务的步骤。

二、安装python需要的库

pip install grpcio

pip install grpcio-tools  

pip install protobuf

三、定义gRPC的接口

创建 gRPC 服务的第一步是在.proto 文件中定义好接口,proto是一个协议文件,客户端和服务器的通信接口正是通过proto文件协定的,可以根据不同语言生成对应语言的代码文件。这个协议文件主要就是定义好服务(service)接口,以及请求参数和相应结果的数据结构,下面是一个简单的例子。

syntax = "proto3";

option cc_generic_services = true;

//定义服务接口
service GrpcService {
    rpc hello (HelloRequest) returns (HelloResponse) {}  //一个服务中可以定义多个接口,也就是多个函数功能
}

//请求的参数
message HelloRequest {
    string data = 1;   //数字1,2是参数的位置顺序,并不是对参数赋值
    Skill skill = 2;  //支持自定义的数据格式,非常灵活
};

//返回的对象
message HelloResponse {
    string result = 1;
    map<string, int32> map_result = 2; //支持map数据格式,类似dict
};

message Skill {
    string name = 1;
};

四、使用 protoc 和相应的插件编译生成对应语言的代码

python -m grpc_tools.protoc -I ./ --python_out=./ --grpc_python_out=. ./hello.proto

利用编译工具把proto文件转化成py文件,直接在当前文件目录下运行上述代码即可。

1.-I 指定proto所在目录

2.-m 指定通过protoc生成py文件

3.--python_out指定生成py文件的输出路径

4.hello.proto 输入的proto文件

执行上述命令后,生成hello_pb2.py 和hello_pb2_grpc.py这两个文件。

五、编写grpc的服务端代码

#! /usr/bin/env python
# coding=utf8

import time
from concurrent import futures

import grpc

from gRPC_example import hello_pb2_grpc, hello_pb2

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class TestService(hello_pb2_grpc.GrpcServiceServicer):
    '''
    继承GrpcServiceServicer,实现hello方法
    '''
    def __init__(self):
        pass

    def hello(self, request, context):
        '''
        具体实现hello的方法,并按照pb的返回对象构造HelloResponse返回
        :param request:
        :param context:
        :return:
        '''
        result = request.data + request.skill.name + " this is gprc test service"
        list_result = {"12": 1232}
        return hello_pb2.HelloResponse(result=str(result),
                                       map_result=list_result)

def run():
    '''
    模拟服务启动
    :return:
    '''
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    hello_pb2_grpc.add_GrpcServiceServicer_to_server(TestService(),server)
    server.add_insecure_port('[::]:50052')
    server.start()
    print("start service...")
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    run()

在服务端侧,需要实现hello的方法来满足proto文件中GrpcService的接口需求,hello方法的传入参数,是在proto文件中定义的HelloRequest,context是保留字段,不用管,返回参数则是在proto中定义的HelloResponse,服务启动的代码是标准的,可以根据需求修改提供服务的ip地址以及端口号。

六、编写gRPC客户端的代码

#! /usr/bin/env python
# coding=utf8

import grpc

from gRPC_example import #! /usr/bin/env python
# coding=utf8

import grpc

from gRPC_example import hello_pb2_grpc, hello_pb2


def run():
    '''
    模拟请求服务方法信息
    :return:
    '''
    conn=grpc.insecure_channel('localhost:50052')
    client = hello_pb2_grpc.GrpcServiceStub(channel=conn)
    skill = hello_pb2.Skill(name="engineer")
    request = hello_pb2.HelloRequest(data="xiao gang", skill=skill)
    respnse = client.hello(request)
    print("received:",respnse.result)


if __name__ == '__main__':
    run()


def run():
    '''
    模拟请求服务方法信息
    :return:
    '''
    conn=grpc.insecure_channel('localhost:50052')
    client = hello_pb2_grpc.GrpcServiceStub(channel=conn)
    skill = hello_pb2.Skill(name="engineer")
    request = hello_pb2.HelloRequest(data="xiao gang", skill=skill)
    response = client.hello(request)
    print("received:",response.result)


if __name__ == '__main__':
    run()

客户端侧代码的实现比较简单,首先定义好访问ip和端口号,然后定义好HelloRequest数据结构,远程调用hello即可。需要强调的是,客户端和服务端一定要import相同proto文件编译生成的hello_pb2_grpc, hello_pb2模块,即使服务端和客户端使用的语言不一样,这也是grpc接口规范一致的体现。

七、调用测试

先启动运行服务端的代码,再启动运行客户端的代码即可。

感谢你能够认真阅读完这篇文章,希望小编分享的“怎么使用Python搭建gRPC服务”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

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

AI