gRPC Python entry into the production environment

All code https://github.com/xsren/learning_record/tree/master/grpc, welcomed the star.

First, to understand a few concepts

RPC

RPC (Remote Procedure Call) - remote procedure call, which is a service request from a remote computer through a network, without having to understand the underlying network protocol technology.

gRPC

gRPC is a high-performance, general-purpose open-source RPC framework, mainly by developers and based on HTTP / 2 standard protocol designed by Google, based on ProtoBuf (Protocol Buffers) serialization protocol development, and support many development languages.

 

Provides better application of a strong performance-based HTTP / 2 protocol (bandwidth savings, reduce TCP requests connection) based ProtoBuf defined service-oriented interface to services top-level design support mainstream programming language, C ++, Java, Python, Go, Ruby, Node.js, PHP, etc., to form the corresponding ProtoBuf based server and client code. Is accomplished using Restful compared between each access service, GRPC provides better performance, lower latency, and adapted born with a distributed system. Interface design at the same time to generate between server-side and client-side code standards-based IDL (ProtoBuf), ProtoBuf service is defined as service contract, so you can better support team and team development, testing, collaboration, and so on.

protobuf

protocol buffers (referred Protobuf) google is a format of data exchange, which is independent of language, platform-independent.

 

protobuf is a data transfer format google developed, similar json protobuf is binary, structured, it is smaller than the amount of data the json, more objectification protobuf not directly plaintext like json, this is the definition of the object structure, and then by the protbuf go to the library object is automatically converted into a binary, with the time and then automatically inverse solution over. Transmission is transparent to us! We just transport objects on it

Second, re-learning protobuf

1, the installation protobuf

1) Installation Protocol Compiler

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

There are two ways, one is to compile a download and then protoc in / usr / bin can be. I chose the latter, the local command is as follows:

# Unzip download unzip protoc-3.5.1-osx-x86_64.zip -d protoc # copy sudo cp protoc in https://github.com/google/protobuf/releases download protoc-3.5.1-osx-x86_64.zip # / bin / protoc / usr / bin / # increase the executable permissions sudo chmod + x / usr / bin / protoc # include copies of documents pcp -rf include / google / usr / local / include

2) Installation python package

sudo pip3 install protobuf

2, run protobuf demo

reference:

 

Buffer Basics Protocol: Python https://developers.google.com/protocol-buffers/docs/pythontutorial proto3 and proto2 difference https://superlc320.gitbooks.io/protocol-buffers-3-study-notes/content/proto3he_proto2_de_qu_bie .html

The official demo implements a simple address book, contacts can be written to a file, and you can read contacts from the file.

python version 3.6.0 Protoc version 3.5.1cd protobuf_demo # compiler generates addressbook_pb2.pyprotoc --python_out =. addressbook.proto # add contacts python3 add_person.py address.txt # read contact python3 list_people.py address.txt

operation result

Three, gRPC learning

1, the installation

Reference: https://grpc.io/docs/quickstart/python.html

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

2, run

1) hello world

and generating helloworld_pb2.py cd grpc_helloworld # helloworld_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path =. --python_out =. --grpc_python_out =. helloworld.proto # serverpython3 greeter_server.py # Run Run clientpython3 greeter_client.py

2) route guide

And a related streaming demo, support:

 

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

The streaming scenario where data transmission is mainly more than.

and generating route_guide_pb2.py cd grpc_helloworld # route_guide_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path =. --python_out =. --grpc_python_out =. route_guide.proto # serverpython3 route_guide_server.py # Run Run clientpython3 route_guide_client.py

Fourth, the production environment

reference:

 

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

Production of environmental requirements:

 

The performance using multiple threads to improve concurrency. Use of load balancing for expansion. Security SSL / TLS Token-based authentication with Google expand the use of other authentication error handling timeout error interceptor (python version of the interceptor is not stable) cd grpc_product # production private openssl genrsa -out server.key 2048 # openssl public key production req -new -x509 -sha256 -key server.key -out server.crt -days 3650 # helloworld_pb2.py and generating helloworld_pb2_grpc.pypython3 -m grpc_tools.protoc --proto_path =. --python_out = . --grpc_python_out =. helloworld .proto # run serverpython3 greeter_server.py # run clientpython3 greeter_client.py

Fifth, let gRPC support Restful

reference:

 

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 can be used to generate using a reverse proxy, received RESTful JSON API into gRPC.

grpc_gateway.png

# Generated python file uses google.api, So now, I find that actually included in the google-cloud-translate inside sudo pip3 install google-cloud-translate # dependent packages installed go go get -u github.com/ Ecosystem-GRPC / GRPC-Gateway / Protoc-Gen-GRPC-Gateway Go GET -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger Go GET -u github.com/golang/protobuf/protoc- gen-go # # modified proto file generated gRPC golang stub class sh gen_grpc_stub_go.sh # need to comment helloworld.pb.go line 19: import _ "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google / api "# generated gRPC python stub class sh gen_grpc_stub_python.sh # generate the gateway code sh gen_grpc_gw.sh # sh gen_grpc_gw_swagger.sh # code that generates swagger run serverpython3 server.py # run clientpython3 client.py # running gateway services go run proxy.go # command-line test curl -X POST -k http: // localhost : 8080 / v1 / hello -d '{ "name": "world"}' # test swagger open http: // localhost: 8080 / swagger -ui /

Six, TODO:

 

1, in-depth study of advanced options grpc-gateway.

Seven questions to answer:

Question1: gRPC client can use a proxy it?

Answer:

The official client API does not provide the option to set the proxy feasible several options:

 

1, by setting the system's proxy $ $ echo the http_proxy HTTP: //httpproxy.mydomain: 8080 2, through a reverse proxy nginx, haproxy other settings

Question2: protobuf default value is?

Answer: The following proto3 only for reference 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 how authentication and authorization?

 

SSL / TLS Token-based authentication with Google (includes Oauth2 and JWT way) own expansion, the official API can be extended to provide their own authentication method (go find the implementation)

Question4: gRPC concurrency problems?

 

Multithreading (does not support multi-process) asynchronous, there is a enthusiastic users achieve load balancing

Guess you like

Origin www.cnblogs.com/ExMan/p/12112640.html