gRPC Python 入门到生产环境

所有的代码在 https://github.com/xsren/learning_record/tree/master/grpc,欢迎star。

一、先了解几个概念

RPC

RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。

gRPC

gRPC是一个高性能、通用的开源RPC框架,其由Google主要由开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。

 

基于HTTP/2协议提供了更好的强的应用性能(节省带宽,减少TCP请求连接数)基于ProtoBuf定义服务,面向接口对服务进行顶层设计支持主流的编程语言,C++,Java,Python,Go,Ruby,Node.js,PHP等, 基于ProtoBuf生成相应的服务端和客户端代码。相比在使用Restful方式完成服务之间的相互访问,GRPC能提供更好的性能,更低的延迟,并且生来适合与分布式系统。同时基于标准化的IDL(ProtoBuf)来生成服务器端和客户端代码, ProtoBuf服务定义可以作为服务契约,因此可以更好的支持团队与团队之间的接口设计,开发,测试,协作等等。

protobuf

protocol buffers(简称protobuf)是google 的一种数据交换的格式,它独立于语言,独立于平台。

 

protobuf是google开发的一个数据传输格式,类似jsonprotobuf是二进制的、结构化的,所以比json的数据量更小,也更对象化protobuf不是像json直接明文的,这个是定义对象结构,然后由protbuf库去把对象自动转换成二进制,用的时候再自动反解过来的。传输对我们是透明的!我们只管传输的对象就可以了

二、再学习protobuf

1、安装protobuf

1)安装 Protocol Compiler

参考:Protocol Compiler Installation https://github.com/google/protobuf

有两种方式,一种是自己编译,一种是下载然后把protoc放在/usr/bin即可。我选的后者,本地命令如下:

# 下载在 https://github.com/google/protobuf/releases 下载 protoc-3.5.1-osx-x86_64.zip# 解压unzip protoc-3.5.1-osx-x86_64.zip -d protoc# 拷贝sudo cp protoc/bin/protoc /usr/bin/# 增加可执行的权限sudo chmod +x /usr/bin/protoc# 拷贝 include文件pcp -rf include/google /usr/local/include

2)安装 python package

sudo pip3 install protobuf

2、运行protobuf demo

参考:

 

Protocol Buffer Basics: Python https://developers.google.com/protocol-buffers/docs/pythontutorialproto3和proto2的区别 https://superlc320.gitbooks.io/protocol-buffers-3-study-notes/content/proto3he_proto2_de_qu_bie.html

官方的demo实现了一个简易通讯录,可以将联系人写入文件,并可以从文件中读取联系人。

python版本3.6.0protoc版本3.5.1cd protobuf_demo# 编译生成addressbook_pb2.pyprotoc --python_out=. addressbook.proto# 添加联系人python3 add_person.py address.txt# 读取联系人python3 list_people.py address.txt

运行结果

三、gRPC学习

1、安装

参考:https://grpc.io/docs/quickstart/python.html

# Install gRPCsudo pip3 install grpcio# Install gRPC tools sudo pip3 install grpcio-tools

2、运行

1) hello world

cd grpc_helloworld# 生成 helloworld_pb2.py 和 helloworld_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. helloworld.proto# 运行serverpython3 greeter_server.py# 运行clientpython3 greeter_client.py

2) route guide

一个和streaming相关的demo,支持:

 

A server-to-client streaming RPC.A client-to-server streaming RPC.A Bidirectional streaming RPC.

streaming 的应用场景主要是传输数据量比较多的情况。

cd grpc_helloworld# 生成 route_guide_pb2.py 和 route_guide_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. route_guide.proto# 运行serverpython3 route_guide_server.py# 运行clientpython3 route_guide_client.py

四、生产环境

参考:

 

python API doc https://grpc.io/grpc/python/index.htmlExploring Security, Metrics, and Error-handling with gRPC in Python https://blog.codeship.com/exploring-security-metrics-and-error-handling-with-grpc-in-python/gRPC Authentication https://grpc.io/docs/guides/auth.html#credential-types

生产环境的要求:

 

性能使用多线程提高并发。使用负载均衡的方式进行扩展。安全SSL/TLSToken-based authentication with Google扩展使用其他认证方式错误处理超时错误拦截器(python版本的拦截器还不稳定)cd grpc_product# 生产私钥openssl genrsa -out server.key 2048# 生产公钥openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650# 生成 helloworld_pb2.py 和 helloworld_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. helloworld.proto# 运行serverpython3 greeter_server.py# 运行clientpython3 greeter_client.py

五、让gRPC支持Restful

参考:

 

gRPC with REST and Open APIs https://grpc.io/blog/coreosgrpc-gateway https://github.com/grpc-ecosystem/grpc-gatewaygRPC helloworld service, RESTful JSON API gateway and swagger UI http://www.cnblogs.com/lienhua34/p/6285829.html

可以使用使用grpc-gateway生成一个反向代理,将接收的RESTful JSON API 转化为 gRPC。

grpc_gateway.png

# 生成的python文件用到了google.api,搞了半天,我发现居然是包含在google-cloud-translate里面的sudo pip3 install google-cloud-translate# 安装go依赖的包go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gatewaygo get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swaggergo get -u github.com/golang/protobuf/protoc-gen-go# 修改proto文件# 生成 gRPC golang stub 类sh gen_grpc_stub_go.sh# 需要注释掉helloworld.pb.go第19行: import _ "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"# 生成 gRPC python stub 类sh gen_grpc_stub_python.sh# 生成网关代码sh gen_grpc_gw.sh# 生成swagger代码sh gen_grpc_gw_swagger.sh# 运行 serverpython3 server.py# 运行 clientpython3 client.py# 运行网关服务go run proxy.go# 命令行测试curl -X POST -k http://localhost:8080/v1/hello -d '{"name": "world"}'# 打开swagger测试http://localhost:8080/swagger-ui/

六、TODO:

 

1、深入研究grpc-gateway的高级选项。

七、疑问解答:

Question1: gRPC client能使用代理吗?

Answer:

官方的client API并没有提供设置proxy的选项,可行的几种方案:

 

1、通过设置系统的代理$ echo $http_proxyhttp://httpproxy.mydomain:80802、通过nginx、haproxy等设置一个反向代理

Question2: protobuf的默认值是?

Answer:以下只针对proto3参考https://developers.google.com/protocol-buffers/docs/proto3

For strings, the default value is the empty string. For bytes, the default value is empty bytes.For bools, the default value is false.For numeric types, the default value is zero.For enums, the default value is the first defined enum value, which must be 0.For message fields, the field is not set. Its exact value is language-dependent. See the generated code guide for details.

Question3: gRPC如何认证和授权?

 

SSL/TLSToken-based authentication with Google(包含了Oauth2和JWT的方式)自己扩展,官方提供API可以自行扩展认证方式(找到了go的实现方式)

Question4: gRPC的并发问题?

 

多线程(不支持多进程)异步,有一个热心网友的实现负载均衡

posted on 2019-12-28 18:35  ExplorerMan  阅读(705)  评论(0编辑  收藏  举报

导航