rpc server and client
define a proto
syntax = "proto3";
package helloworld;
option go_package="github.com/go-eagle/eagle/examples/helloworld/helloworld";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
// 字段定义,如果使用 form 表单传输,则只支持
// int32, int64, uint32, unint64, double, float, bool, string,
// 以及对应的 repeated 类型,不支持 map 和 message 类型!
// 框架会自动解析并转换参数类型
// 如果用 json 或 protobuf 传输则没有限制
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
// 出参定义, 理论上可以输出任意消息
// 但我们约定只能包含 code, msg, data 三个字段,
// 其中 data 需要定义成 message
// 开源版本可以怱略这一约定
message HelloResponse {
// 业务错误码[机读],必须大于零
// 小于零的主站框架在用,注意避让。
int32 code = 1;
// 业务错误信息[人读]
string msg = 2;
// 业务数据对象
HelloReply data = 3;
}
generated helper code
the protocol buffer compiler generates codes that has
- message serialization code(*.pb.go)
- remote interface stub for Client to call with the methods(*_grpc.pb.go)
- abstract interface for Server code to implement(*_grpc.pb.go)
try it out.
enter the project root directory
cd {root_path}
go get -v google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
go get -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
gen proto
old-way
使用:github.com/golang/protobuf
$ protoc -I . --go_out=plugins=grpc,paths=source_relative:. examples/helloworld/protos/greeter.proto
生成的 *.pb.go
包含消息序列化代码和 gRPC
代码.
new-way
使用:google.golang.org/protobuf
$ protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
examples/helloworld/protos/greeter.proto
会生成两个文件 *.pb.go
和 *._grpc.pb.go
, 分别是消息序列化代码和 gRPC
代码.
官方说明:https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.20.0#v1.20-grpc-support
https://grpc.io/docs/languages/go/quickstart/#regenerate-grpc-code
Run
- run the server
cd examples/helloworld/server
go run main.go
- run the client from another terminal
cd examples/helloworld/client
go run main.go
- You’ll see the following output:
Greeting : "Hello eagle"
Reference