Communication between microservices--Protobuf

Release: 2023-07-24 15:26:10
forward
735 people have browsed it

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;
}
Copy after login

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 TypeNotesJava TypeGo Type
double
doublefloat64
float
floatfloat32
int32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.intint32
int64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.longint64
uint32Uses variable-length encoding.intuint32
uint64Uses variable-length encoding.longuint64
sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.intint32
sint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.longint64
fixed32Always four bytes. More efficient than uint32 if values are often greater than 228.intuint32
fixed64Always eight bytes. More efficient than uint64 if values are often greater than 256.longuint64
sfixed32Always four bytes.intint32
sfixed64Always eight bytes.longint64
bool
booleanbool
stringA string must always contain UTF-8 encoded or 7-bit ASCII text, and cannot be longer than 232.Stringstring
bytesMay 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
    Copy after login
  3. 生成最终的代码

    SRC_DIR: 源目录

    DST_DIR: 生成的代码目录

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

    最终生成 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)
}
}
Copy after login

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

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template