建筑公司网站首页,网站通栏尺寸,万商惠网站建设系统开发,创意网站建设策划方案什么是RPC RPC#xff08;Remote Procedure Call Protocol#xff09;-- 远程过程调用协议#xff0c;它是一种通过网络从远程计算机程序上请求服务#xff0c;而不需要了解底层网络协议的协议。RPC协议假定某些传输协议的存在#xff0c;如TCP或UDP#xff0c;为通信程序…什么是RPC
RPCRemote Procedure Call Protocol-- 远程过程调用协议它是一种通过网络从远程计算机程序上请求服务而不需要了解底层网络协议的协议。RPC协议假定某些传输协议的存在如TCP或UDP为通信程序之间携带信息数据。在OSI网络通信模型中RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。什么是gRPC
gRPC 是Google开源的一款高性能的 RPC 框架它基于 ProtoBuf 序列化协议进行开发支持多种开发语言Golang、Python、Java、C/C等。gRPC 提供了一种简单的方法来定义服务同时客户端可以充分利用 HTTP/2 stream 的特性从而有助于节省带宽、降低 TCP 的连接次数、节省CPU的使用等。本文参考官方文档[grpc.html]以及gRPC的GitHub开源项目grpc/grpc。并通过一个小demo展示框架用法。
安装gRPC及gRPC工具
pip install grpcio
pip install grpcio-toolsgrpcio-tools包含了protobuf的编辑工具 protoc用来根据 .proto 服务定义生成服务器端和客户端代码。 自定义 gRPC 接口
假定这里我们需要定义一个数据接收服务Receiver用来接收客户端传递给服务器端的数据。
syntax proto3;
import google/protobuf/struct.proto;// 服务定义
service Receiver {rpc receive (Event) returns (Reply) {}
}// 接收消息定义
message Event {string appid 1;int32 xwhen 2;string xwho 3;string xwhat 4;google.protobuf.Struct xcontext 5;
}// 返回消息定义
message Reply {int32 status 1;string message 2;
}编译 proto 文件生成服务接口
python -m grpc_tools.protoc -I. --python_out. --grpc_python_out. ./receiver.proto这里会生成两个python文件
receiver_pb2.py
receiver_pb2_grpc.py编写Server端代码
# _*_ coding: utf-8 _*_import grpc
import receiver_pb2
import receiver_pb2_grpcimport time
from concurrent import futures_ONE_DAY_IN_SECONDS 60 * 60 * 24class Receiver(receiver_pb2_grpc.ReceiverServicer):# 重写父类方法返回消息def receive(self, request, context):print(request:, request)return receiver_pb2.Reply(messageHello, %s! % request.xwho)if __name__ __main__:server grpc.server(futures.ThreadPoolExecutor(max_workers10))receiver_pb2_grpc.add_ReceiverServicer_to_server(Receiver(), server)server.add_insecure_port([::]:50051)server.start()print(server start...)try:while True:time.sleep(_ONE_DAY_IN_SECONDS)except KeyboardInterrupt:server.stop(0)编写client端代码
# _*_ coding: utf-8 _*_import grpc
import receiver_pb2
import receiver_pb2_grpc
from google.protobuf import struct_pb2def run():channel grpc.insecure_channel(localhost:50051)stub receiver_pb2_grpc.ReceiverStub(channel)# 自定义struct结构struct struct_pb2.Struct()struct[idfa] idfa1struct[amount] 123response stub.receive(receiver_pb2.Event(xwhatinstall, appidfuckgod, xwhen123, xwhojerry, xcontextstruct))print(client status: %s received: %s % (response.status, response.message))if __name__ __main__:run()测试流程
1启动serverpython server.py
2运行client.py发送消息
server输出
server start...
request:
appid: fuckgod
xwhen: 123
xwho: jerry
xwhat: install
xcontext {fields {key: amountvalue {number_value: 123.0}}fields {key: idfavalue {string_value: idfa1}}
}client输出
client status: 0 received: Hello, jerry!表示测试成功。
转载自https://zhuanlan.zhihu.com/p/37158888