Preface

ProtoBuf is 061a321ff7f500?

ProtoBuf is a set of interface description language (IDL), in layman's terms, it is a way of data expression, and it can also be called a data exchange format.

Our commonly used data formats are JSON and XML , why use ProtoBuf ? It is because of its fast transmission, why is it fast? You can find the information. Use .proto file to describe the data structure to be serialized, and then write the .proto file and use protoc to easily compile into many computer language interface codes.

gRPC is 061a321ff7f599?

gRPC is the open source RPC framework, which already supports mainstream computer languages. The interface can be defined ProtoBuf , and data transmission ProtoBuf

Although the two are one family, they solve different problems separately and can be used together or separately.

Look at the gRPC the HelloWorld of proto how files are defined?

helloworld.proto

syntax = "proto3";

package helloworld;

option go_package = "./;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 {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

service Greeter and rpc SayHello methods are defined in the file.

Entry: string name

Reference: string message

These are too simple, can you describe other information?

Little thinking

  1. Can the defined rpc also support the call of HTTP For example, the SayHello method supports both gRPC calls and HTTP calls. At the same time, it also supports protoc generate code and also generates Swagger interface documents.
  2. Can the defined input parameters support parameter verification? For example: the length of name cannot be greater than 20 characters.
  3. Can the defined service Greeter service support interceptors? For example: all methods under this service require login token verification.
  4. rpc defined 061a321ff7f824 SayHello method support interceptors? For example: The current method supports whether to enable and disable logging.

summary

The above problems have not been completely solved, and learning gRPC feels a bit difficult...

Do you have any learning resources that you can recommend? Currently looking at grpc-gateway .

Recommended reading


程序员新亮
2.9k 声望1.2k 粉丝

GitHub 9K+ Star,其中适合 Go 新手的开箱即用项目 go-gin-api 5.2K Star:[链接],联系我:wx-xinliang