To be honest , I really don't approve of using python Write business server code . This chapter uses python A set of language implementation grpc frame , The example is not too complicated , When it is realized, it is be based on grpc Build a quasi production distributed application from scratch ( series ) Here's an example . The author is realizing demo I stepped on a lot of holes , Because there is little information on the Internet .
Program download address : demo-python Source code Extraction code : 26kv
Some versions of the packages in the following table must be the same , Otherwise, it will not be applied protobuf The problem of .
pip3 install grpcio == 1.43.0
pip3 install grpcio - tools == 1.43.0
pip3 install protobuf == 3.20.1
Take a look at... In the source code readme.md file . Enter the project root path and execute ,--python_out Be sure to specify the project root path ( This is a specification for lawsuit implementation , Cannot change directory ).
python3 - m grpc_tools. protoc - - python_out =.. / - - grpc_python_out =.. / - I =. . / *. proto
Then run one by one :
Server side :ApplicationServerStart.py
client :SystemlogTest.py
python You cannot test with the graph line tool , The command line test method is as follows
brew install grpcurl
// see GRPC Service list of all services
grpcurl -- plaintext 127.0 .0 .1: 9898 list
Output is as follows :
/*grpc.health.v1.Health
grpc.reflection.v1alpha.ServerReflection
net.devh.boot.grpc.example.MyService
*/
// View the service interface provided by an interface
grpcurl -- plaintext 127.0 .0 .1: 9898 list net. devh. boot. grpc. example. MyService
Output is as follows :
/*net.devh.boot.grpc.example.MyService.SayHello*/
// Simulated access
grpcurl -- plaintext - d '{"name": "test"}' 127.0 .0 .1: 9898 net. devh. boot. grpc. example. MyService / sayHello