汇总:
https://grpc.io/docs/
github:
https://github.com/grpc/grpc
介绍:
https://grpc.io/docs/guides/index.html
快速:
https://grpc.io/docs/quickstart/
gRPC Basics-Python:
https://grpc.io/docs/tutorials/basic/python.html
python 快速:
https://grpc.io/docs/quickstart/python.html
#Ensure you have pip version 9.0.1 or higher:
python -m pip install --upgrade pip
#If you cannot upgrade pip due to a system-owned installation,
#you can run the example in a virtualenv:
python -m pip install virtualenv
virtualenv venv
source venv/bin/activate
python -m pip install --upgrade pip
#Install gRPC:
python -m pip install grpcio
#Install gRPC tools
python -m pip install grpcio-tools
#Download the example
# Clone the repository to get the example code:
git clone -b v1.7.x https://github.com/grpc/grpc
# Navigate to the "hello, world" Python example:
cd grpc/examples/python/helloworld
#Run a gRPC application
#From the examples/python/helloworld directory:
#1.Run the server
python greeter_server.py
#2.In another terminal, run the client
python greeter_client.py
#Congratulations! You’ve just run a client-server application with gRPC.
Update a gRPC service
#Edit examples/protos/helloworld.proto
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
#Generate gRPC code
#From the examples/python/helloworld directory, run:
python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto
#This regenerates helloworld_pb2.py which contains our generated request
#and response classes and helloworld_pb2_grpc.py
#which contains our generated client and server classes.
#Update and run the application
#Update the server
#In the same directory, open greeter_server.py
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def SayHelloAgain(self, request, context):
return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)
...
#Update the client
#In the same directory, open greeter_client.py. Call the new method like this:
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
#Run!
#Just like we did before, from the examples/python/helloworld directory:
#1.Run the server
python greeter_server.py
#2.In another terminal, run the client
python greeter_client.py
proto buffers:
https://developers.google.com/protocol-buffers/docs/overview