# gRPC
介绍“gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.”
On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.
# 环境搭建创建一个用于 gRPC
的虚拟环境
conda create -n grcp_py python = 3.10
激活虚拟环境,并进入到项目目录下方
conda activate grpc_py mkdir grpccd grpc
安装 gRPC
第三方库及其依赖
pip install grpcio pip install grpcio-tools
# 示例一: HelloWorld# 功能# protobuf 协议结构syntax = "proto3" ; package helloworld; service Greeter { rpc SayHello ( HelloRequest ) returns ( HelloReply ) { } } message HelloRequest { string name = 1 ; } message HelloReply { string message = 1 ; }
# 简单的客户端、服务端from concurrent import futures import grpcimport helloworld_pb2import helloworld_pb2_grpcclass Greeter ( helloworld_pb2_grpc. GreeterServicer) : def SayHello ( self, request, context) : return helloworld_pb2. HelloReply( message= "Hello, %s!" % request. name) def serve ( ) : port = "50051" server = grpc. server( futures. ThreadPoolExecutor( max_workers= 10 ) ) helloworld_pb2_grpc. add_GreeterServicer_to_server( Greeter( ) , server) server. add_insecure_port( "[::]:" + port) server. start( ) print ( "Server started, listening on " + port) server. wait_for_termination( ) if __name__ == "__main__" : serve( )
import grpcimport helloworld_pb2import helloworld_pb2_grpcdef run ( ) : print ( "Will try to greet world ..." ) with grpc. insecure_channel( "服务端ip:服务端端口" ) as channel: stub = helloworld_pb2_grpc. GreeterStub( channel) response = stub. SayHello( helloworld_pb2. HelloRequest( name= "you" ) ) print ( "Greeter client received: " + response. message) if __name__ == "__main__" : run( )
# with flectionpip install grpcio-reflection
from concurrent import futuresimport grpcfrom grpc_reflection. v1alpha import reflectionimport helloworld_pb2import helloworld_pb2_grpcclass Greeter ( helloworld_pb2_grpc. GreeterServicer) : def SayHello ( self, request, context) : return helloworld_pb2. HelloReply( message= "Hello, %s!" % request. name) def serve ( ) : server = grpc. server( futures. ThreadPoolExecutor( max_workers= 10 ) ) helloworld_pb2_grpc. add_GreeterServicer_to_server( Greeter( ) , server) SERVICE_NAMES = ( helloworld_pb2. DESCRIPTOR. services_by_name[ "Greeter" ] . full_name, reflection. SERVICE_NAME, ) reflection. enable_server_reflection( SERVICE_NAMES, server) server. add_insecure_port( "[::]:50051" ) server. start( ) server. wait_for_termination( ) if __name__ == "__main__" : serve( )
# 异步客户端、服务端import asyncioimport grpcimport helloworld_pb2import helloworld_pb2_grpcclass Greeter ( helloworld_pb2_grpc. GreeterServicer) : async def SayHello ( self, request: helloworld_pb2. HelloRequest, context: grpc. aio. ServicerContext, ) - > helloworld_pb2. HelloReply: return helloworld_pb2. HelloReply( message= "Hello, %s!" % request. name) async def serve ( ) - > None : server = grpc. aio. server( ) helloworld_pb2_grpc. add_GreeterServicer_to_server( Greeter( ) , server) listen_addr = "[::]:50051" server. add_insecure_port( listen_addr) logging. info( "Starting server on %s" , listen_addr) await server. start( ) await server. wait_for_termination( ) if __name__ == "__main__" : asyncio. run( serve( ) )
# 参考[1] gRPC [2] [Github for gRPC][https://github.com/grpc/grpc ]