GRPC学习(An RPC library and framework)

汇总:
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值