preface

What is a ProtoBuf?

ProtoBuf is a set of interface description language (IDL), commonly known as a data representation format, also known as a data interchange format.

Our common data formats are JSON and XML. Why use ProtoBuf? Because it’s fast. Why is it fast? You can look it up. A.proto file is used to describe the data structure to be serialized, and then written. Proto files can be easily compiled into interface code for many computer languages using protoc.

What is gRPC?

GRPC is an open source RPC framework that supports mainstream computer languages. ProtoBuf can be used to define interfaces and transmit data.

Although the two are a family, but to solve different problems, can be used together, can also be separated.

How is the proto file of gRPC HelloWorld 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; }Copy the code

The file defines a Service Greeter and RPC SayHello methods.

Input parameter: string name

Output parameter: string message

That’s too simplistic. Can you describe anything else?

Small thinking

  1. Can you define RPC methods that also support HTTP calls? For example, the SayHello method supports both gRPC and HTTP calls, and generates Swagger interface documents when protoC code is generated.

  2. Does the defined input parameter support parameter validation? For example, name contains a maximum of 20 characters.

  3. Can the defined Service Greeter service support interceptors? For example, all methods under the service require login token authentication.

  4. Does the defined RPC SayHello method support interceptors? For example, the current method supports whether to enable or disable logging.

summary

The above problems have not been completely solved, learning gRPC feel some difficulty…

Do you have any learning resources you can recommend? Currently looking at GRPC-Gateway.

Recommended reading

  • Go – Use sync.waitGroup for concurrent operations
  • Go – Use sync.Map to solve the problem of Map concurrency security
  • Go – Improves application performance based on escape analysis
  • Go – Use sync.Pool to reduce GC stress
  • Go – Uses options design mode