1,安装所需库:
$ pip install grpcio
$ pip install grpcio-tools
$ pip install protobuf
2,编写proto文件
syntax = "proto3"; //定义使用proto3的语法协议
package weather_test; //定义包名
message WeatherRequest {
//定义客户端请求参数,其中"1","2"是指参数的序列号,新加参数需要递增
string city = 1;
}
message WeatherReply {
//定义服务端响应,其中"1"是指参数的序列号
string response = 1;
}
service weather {
// rpc服务名,服务端为weatherServicer客户端为weatherStub
rpc get_weather(WeatherRequest) returns (WeatherReply) {
}
// get_weather中输入WeatherRequest的参数,返回WeatherReply
}
3,生成 gRPC 代码