grpc
In gRPC, a client application can directly call a method on [a server application on a different machine] as if it were a local object, making it easier for you to create distributed applications and services
gRPC is based around the idea of
- defining a service
- specifying the methods that can be called remotely with their parameters and return types
- implement the methods in the server side and call them in the client side
server side and client side
once you’ve specified your data structures in protocol buffers(.proto file), you use the protocol buffer compiler protoc
to generate data access classes and APIs in your preferred language(s) from your proto definition. These provide simple accessors for each field, like name() and set_name(), as well as methods to serialize/parse the whole structure to/from raw bytes.
On the server side, the server must implement the interface of service defined in the proto buffers file, and runs a gRPC server to handle client calls.
On the client side, the client has a local object known as stub (referred to as just a client in some languages) that provides the same methods as the server. stub is for client side only
gRPC users typically call these APIs on the client side and implement the corresponding API on the server side.
example
// example.proto
syntax = "proto3";
service Hello {
...
}
message HelloRequest {
string msg = 1;
}
This will generate the example.pb.go
and example_grpc.pb.go
files, which contain:
-
example.pb.go
: Code for populating, serializing, and retrievingHelloRequest
message types -
example_grpc.pb.go
: Generated client and server code - An interface type (or stub) for clients to call with the methods defined in the Hello service.
- An interface type for servers to implement, also with the methods defined in the Hello service.
You have generated/regenerated server and client code by protoc
, but you still need to implement the interface in the server side, and call the new method in the client side. Both are the human-written works
protocol buffer compiler
The protocol buffer compiler requires a plugin to generate Go code. Install it using Go 1.16 or higher by running:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
This will install a protoc-gen-go
binary in $GOBIN
we can move the binary to /usr/local/bin/protoc-gen-go
protoc
creates a single source file for each .proto
file input. The name of the output file is created by replacing the .proto
extension with .pb.go
.
package in .proto
file
You can add an optional package
specifier to a .proto
file to prevent name clashes between protocol message types.
// 这个package不是 Go 语言中的package,是proto buffers 的package
package foo.bar;
message Open { ... }
You can then use the package specifier when defining fields of your message type:
message Foo {
...
foo.bar.Open open = 1;
...
}
In Go, the package
specifier is used as the Go package name, unless you explicitly provide an option go_package
in your .proto
file.
导入其他proto文件
要导入其他 proto 文件,只需要在当前.proto
file 使用import
键字,如下:
import "protobuf/import/component.proto";
网友评论