Home  >  Article  >  Backend Development  >  Communication between microservices--Protobuf

Communication between microservices--Protobuf

Go语言进阶学习
Go语言进阶学习forward
2023-07-24 15:26:10710browse

1. Overview

Communication between microservices--Protobuf

Above picture It is a disassembled SONY camera. The components inside perform their own duties when working, and only do what they are good at. They can come from different manufacturers or even countries. In today's globalized world, most excellent products are evolving at a single point, seeking better services and technologies.

#In fact, it is the same for software technology, and it is more like a microcosm of globalization.

Microservices There is no fixed and single definition. With the passage of time and the continuous evolution of technology, the industry has silently formed some consensus. The characteristics that can be summarized include the following points.

  • Specific microservices transfer information to each other through communication protocols throughout the entire architecture, such as HTTP.

  • #Microservices can be deployed independently.

  • #Microservices organize specific functions around the business.

  • #Microservices are not limited to language, database, hardware and software environments to implement services.

  • Services are small-granular, support messaging, are context-bound, and are built and released through automated processes.

From the above summary, information interaction between microservices is the foundation of the entire MSA (Microservice Architecture), communication The quality of the protocol determines whether the services established based on it are simple, efficient, stable, scalable, and easy to maintain. The ultimate embodiment in the product is the user experience, especially for services that require quick response, such as payment, advertising bidding, etc. And Protocol Buffers (commonly known as Protobuf) is the best of them.

As for why, we can refer to the following article and will not go into details here.

Beating JSON performance with Protobuf LINK

2. Quick use

2.1 Define communication protocol

Let us first look at a very simple example. Suppose we need to define a login request. This login request requires a username , Password, Number of retries. We can define this request in a file with the suffix .proto. The content of the information is as follows: ##

/* LoginRequest represents a login request
* user_name: user name 
* password: password 
* retry_time: retry times, can not over 3 times */

syntax = "proto3";  // proto3 syntax
option go_package = "pb/request";

message LoginRequest {
 string user_name = 1;
 string password = 2;
 int32 retry_times = 3;
}

Protocol version

The first line defines the current Use the syntax version, the latest version is proto3. You can also use proto2

Protocol field type

LoginRequest The message body defines three specific parameters, each parameter has a specific type and name. Each field can be defined as .proto Type in the following table – and the final type used in the specific language (Java Go) can also be found in the table .

.proto Type Notes Java Type Go Type
double
double float64
float
float float32
int32 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. int int32
int64 Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. long int64
uint32 Uses variable-length encoding. int uint32
uint64 Uses variable-length encoding. long uint64
sint32 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. int int32
sint64 Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. long int64
fixed32 Always four bytes. More efficient than uint32 if values are often greater than 228. int uint32
fixed64 Always eight bytes. More efficient than uint64 if values are often greater than 256. long uint64
sfixed32 Always four bytes. int int32
sfixed64 Always eight bytes. long int64
bool
boolean bool
string A string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232. String string
bytes May contain any arbitrary sequence of bytes no longer than 232. ByteString []byte

注释

Protobuf 支持 C/C++ 中的两种注释风格

  1. 斜线加星号 /* ... */

  2. 双斜线  //

2.2 代码生成

Golang 代码生成

  1. 到 Protobuf 官方 Repo 下载对应平台的 protoc 工具

  2. 首先用 go命令安装生成Go代码的工具, 此工具为生成Golang代码的插件

    go install google.golang.org/protobuf/cmd/protoc-gen-go
  3. 生成最终的代码

    SRC_DIR: 源目录

    DST_DIR: 生成的代码目录

    protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/request.proto

    最终生成 request.pb.go文件,该文件不用修改,后期有任何更新可以重新生成。

  4. 将上述代码保存到 request.proto 文件中

Golang 代码使用

生成的代码可以直接在项目中使用

func main() {
 // 创建请求
loginRequest := request.LoginRequest{
UserName: "Gavin.Yang",
Password: "92d273941d98a8e1c1bb13ac163f0d4e40c5aa70",
RetryTimes: 0}

 // 序列化
out, err := proto.Marshal(&loginRequest)
if err == nil{
fmt.Println(out)
}
}

The above is the detailed content of Communication between microservices--Protobuf. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete