Grpc+protocol buffer use learning

An introduction to gRPC and protocol buffers.

GRPC and protocol buffer:

gRPC can use protocol buffer as its interface definition language (IDL) and its underlying message exchange format.
Personal understanding: An interactive protocol that mainly realizes data transmission. It is small in size, more convenient and efficient. The proto protocol buffer developed by Google supports the development and generation of multiple languages.

1. GRPC overview
In gRPC, a client application can directly call a method on a server application on another machine as if it were a local object, which makes it easier for you to create distributed applications and services.
Just like in many RPC systems (such as the rpc interaction that comes with springcloud), gRPC is based on the idea of ​​defining services, specifying methods that can be called remotely through its parameters and return types.

On the server side, the server implements this interface and runs the gRPC server to handle client calls.
On the client side, the client side has a stub (only called the client side in some languages), which provides the same method.

The gRPC client and server can run in a variety of environments and communicate with each other-from the server in Google to your own desktop-and can be written in any language supported by gRPC. So, for example, you can easily create a gRPC server in Java and a client in Go, Python or Ruby. In addition, the latest Google API will have a gRPC version of its interface, allowing you to easily build Google features in your application.

Second, the realization of GRPC effect
By default, gRPC uses protocol buffer, which is Google's mature open source mechanism for serializing structured data (although it can also be used for other data formats such as JSON). Below is a quick introduction to how it works. If you are already familiar with the protocol buffer, you can ignore it.

The first step in using the protocol buffer is to define the structure of the data to be serialized in the prototype file:
use a plain text file with a .proto extension. (General request protocol requires interface, request body, and file used here) The
protocol buffer data structure is a message, where each message is a small information logical record, which contains a series of name-value pairs called fields. Here is a simple example:

message Person {
  string name = 1;
  int32 id = 2;
  bool has_ponycopter = 3;
}

Then, once the data structure is specified, the protocol buffer compiler protoc can be used to generate the data access class in the preferred language from the original definition. They provide simple accessors for each field, such as name() and set_name(), and methods to serialize/parse the entire structure into raw bytes. So, for example, if your language of choice is c++, running the compiler on the above example will generate a class named Person. You can then use this class in your application to fill, serialize, and retrieve Person protocol buffer messages.
You define the gRPC service in an ordinary proto file, and the RPC method parameters and return type are specified as protocol buffer messages:

// The greeter 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;
}
gRPC使用带有特殊gRPC插件的protoc从你的原型文件中生成代码:                     你会得到生成的gRPC客户端和服务器代码,以及用于填充、序列化和检索你的消息类型的常规协议缓冲区代码。

Third, the buffer protocol (protocol buffer)
understanding: Protocol buffer is a language-independent, platform-independent extensible mechanism for serializing structured data.
Protocol buffer is Google's language-independent, platform-independent, and extensible mechanism for serializing structured data-think about XML, but it is smaller, faster, and simpler. Once the data structure is defined, the specially generated source code can be used to easily write and read structured data from various data streams and in various languages.
This section explains the prot document used by the above grpcs: this document is used to define the buffer protocol, and roughly needs to be defined internally

syntax = "proto3";(版本)
option java_multiple_files = true;()
package  ***;(包名)
service方法名{rpc 方法名2(enity即message)returns(enity)}
message ***{String **}

Fourth, Proto (binary) use the installation steps
Download the protocol buffer compiler. The compiler (process the prot document and generate the required language protocol) The
current proto version recommends proto3, you can use the following command to install the protocol using the package manager under Linux or macOS The compiler protoc. (Please check the version before installation)

Linux:
$ apt install -y protobuf-compiler
$ protoc --version  # Ensure compiler version is 3+

MAX:
$ brew install protobuf
$ protoc --version  # Ensure compiler version is 3+
 	从预编译的二进制文件安装最新版本的协议编译器:
  1. Get the zip file (protoc--.zip) corresponding to your operating system and computer architecture from github.com/google/protobuf/release, or use the following command to get the file:
$ PB_REL="https://github.com/protocolbuffers/protobuf/releases"
$ curl -LO $PB_REL/download/v3.13.0/protoc-3.13.0-linux-x86_64.zip
  1. Unzip the file to $HOME/. Local or directory of your choice:
$ unzip protoc-3.13.0-linux-x86_64.zip -d $HOME/.local
  1. Update the path variable of the environment to include the path of the original executable file:
$ export PATH="$PATH:$HOME/.local/bin"

Five, Proto installation supplement (four) operation
*Go language is used as a demonstration here. This protocol mode supports C#, C++, Dart, Go, JAVA, Node, Ruby and other languages. The specific operations can be seen in the official

https://grpc.io/docs/languages/go/quickstart/ *

How to use Grpc in Go:
1. Prerequisites:
① Install Go (check the official website https://golang.org/doc/install)
② Install the corresponding compiler protoc
(There are four ways to install this compiler. , The first two are windows systems, the third is for Linux, and the fourth is for MacOs)
2.1 You can directly obtain the corresponding version of the protoc file through the git official website and modify the corresponding environment variables.
Recommended link: https://github.com/protocolbuffers/protobuf/releases/tag/v3.14.0
(protoc --version View installed version)

	2.2 通过包管理器chotolate安装编译器protoc
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
然后通过命令choco install protoc安装protoc编译器

2.3 install compiler under Linux (apt or apt-get-Debian or Ubuntu system package manager, available for download from the official website)

$ apt install -y protobuf-compiler
$ protoc --version # Ensure compiler version is 3+

2.4 MacOs (Use the built-in Homebrew, which can be downloaded from the official website

(https://brew.sh/))
		$ brew install protobuf
		$ protoc --version  # Ensure compiler version is 3+

③ Go protocol compiler plug-in (the export here belongs to Linux commands, windows can use $env or other shell commands, this article recommends Linux)
3.1 Install go compile plug-in

$ export GO111MODULE=on  # Enable module mode
$ go get google.golang.org/protobuf/cmd/protoc-gen-go \
            google.golang.org/grpc/cmd/protoc-gen-go-grpc

3.2 Update your path so that the protoc compiler can find the plugin

  $ export PATH="$PATH:$(go env GOPATH)/bin"

*Note: This step is mainly to install and compile the protoc-gen-go plug-in, the sixth step can be automatically configured (some differences, unknown version is not controllable, you can also download the plug-in on git https://github.com/golang/protobuf/releases)

6. Grpc compiles and prints helloword test
1, download the test source code:

$ git clone -b v1.34.0 https://github.com/grpc/grpc-go

2. Enter the directory:

$ cd grpc-go/examples/helloworld

3. Compile and execute the service code in the same directory: (some plug-ins required for step 5 will be downloaded here)

$ go run greeter_server/main.go

4. From another terminal, compile and execute the client code, and view the client output:

$ go run greeter_client/main.go
Greeting: Hello world

So far, congratulations! You have just run a client-server application using gRPC.

Seven, modify the original proto file and go method
1. Try to modify the proto definition file in the original directory (this article is modified on the helloworld/helloworld.proto directory file)
. The proto here is the definition file, which can be changed by modifying the definition Its attribute changes and methods
(recommended to learn the definition link https://grpc.io/docs/languages/go/basics/) all
you need to know is that both the server and the client stub have a SayHello() RPC method, which accepts a HelloRequest parameter from the client and returns a HelloReply from the server. This method is like this Defined:

// 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;
}

Try to add a SayHelloAgain() method to it, using the same request and response, modified as follows:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (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;
}
		*记得保存文件!!!

2. Regenerate the grpc code
(before using the new service method, you need to recompile the updated .proto file.)
Execute the following code in the previous helloword directory (where "\" is Bash line break style, others)

$ protoc --go_out=. --go_opt=paths=source_relative \
	--go-grpc_out=. --go-grpc_opt=paths=source_relative \
 helloworld/helloworld.proto

This will regenerate helloworld/helloworld.pb and helloworld/helloworld_grpc.pb. Go file, which contains:
the code for filling, serializing and retrieving HelloRequest and HelloReply message types; the
generated client and server code;
*This part is most likely to fail to update due to the appealed go plug-in, causing recompilation to fail. So check the go plugin configuration (Windows will have certain problems when I test this step)

3. Update the main.go file.
You have regenerated the server and client code, but you still need to implement and call new methods in the manual writing part of the sample application. The code is as follows:
2.1 Open greeter_server /main.go, add the following function code:

func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
        return &pb.HelloReply{Message: "Hello again " + in.GetName()}, nil
}

2.2 Open greeter_client/main.go and add the function code:

	     r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})
if err != nil {
        log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())

5. Run
1, run the server main.go method

go run greeter_server/main.go

2. Run the client and pass parameters

go run greeter_client/main.go Alice

got the answer:

Greeting: Hello Alice
Greeting: Hello again Alice

Congratulations, you successfully completed the GRPC simple test.
The above is some personal experience about Grpc and protocol buffer. In view of the different commands used by each system, Linux system is recommended for appeal. When testing on windows, the go plug-in will have some problems, but just install the appropriate version. The Macs system has not been tested yet, if you are willing, you can try it. The appeal test code comes from git, the contents can be changed according to actual needs, and the proto file is for reference only.

Thank you all for your advice and help. If there is something wrong, I hope you can give me more advice. (●'◡'●)

Guess you like

Origin blog.csdn.net/MatChen/article/details/111225977