GRPC Basic Concept Learning and Installation Tutorial

tags: go  golang  grpc

First, basically

1, GRPC definition

A High-Performance, Open-Source Universal RPC Framework: High Performance, Open Source Universal RPC Framework

The so-called RPC (Remote Procedure Call Remote Process Call) framework is actually provided a mechanism that can be communicated between the applications, and also follow the Server / Client model. When using it, the client calls the server-connected interface is like calling a local function.

 

2、grpc VS restful api

Both GRPC and RESTFUL API provide a set of communication mechanisms for server / client model communication, and they all use HTTP as the underlying transport protocol (strictly said that the HTTP2.0 used by GRPC, and the Restful API is not necessarily) .

  • GRPC can define the interface through Protobuf, so that there can beMore stringent interface constraints

  • In addition, data sequence can be serialized through ProtobufBinary codeThis will greatly reduce the amount of data that needs to be transmitted, thereby greatly improves performance.

  • GRPC can easily supportStreamCommunication (theoretically via HTTP2.0 can use Streaming mode, but usually the RESTful API of the web service seems to be so useful, usually flow data applications such as video streams, generally use specializes such as HLS, RTMP, etc. It's not us usually web service, but there is a dedicated server application.)

 

3, GRPC usage scene

  • The case where the interface is strictly constrained, especially the factors considering safety
  • For performance, there is a higher demand. Need to pass a large number, asynchronous requests pass binary data

 

4、protobuf

4.1 definition

Protobuf is actually a data transfer format and specification similar to JSON or XML, which is used when communicating between different applications or processes. The information transmitted during communication is packed by the Message data structure defined by Protobuf, and then compiled into binary code streams and then transmits or stores.

 

4.2 Advantages

In contrast, Protobuf has the following advantages:

  • Simple enough
  • After serialization, the volume is small: the message size only needs 1/10 ~ 1/3 of XML
  • Resolution speed: Analytical speed is 20 ~ 100 times faster than XML
  • Multilingual support
  • Better compatibility, a principle of protobuf design is to be able to support down or upwardly

 

5. Relationship between protoc, protoc-gen-go and grpc

5.1 protoc

The protoc command comes from https://github.com/golang/protobuf, which can generate serialization and reverse selecente code, without Go-related code.

Protoc is a compiler of the protobuf file (.proto), with it to translate the .proto file into a source code corresponding to a variety of programming languages.

Compilation method
 protoc --go_out=./go1/ ./proto/my.proto

Protoc's version can be viewed through Protoc --Version

 

5.2 protoc-gen-go

Protoc-gen-GO plug-in, from https://github.com/golang/protobuf/protoc-gen-go, you can generate GO-related code, in addition to the above serialization and reverse sequence code, add some communication public Library. It is a Go version in the Protobuf compilation plugin series

Compilation method
  protoc --go_out=plugins=grpc:./go2/  ./proto/my.proto 

 

5.3 grpc

GRPC comes from https://google.golang.org/grpc or https://github.com/grpc/grpc-go ,Protoc and protoc-gen-go These two tools are not there, The protoc and protoc-gen-g are needed to be placed in the system PATH.

 

Second, install

1, download Protobuf, install protoc

Method 1: Download binary directly

Download address: https://github.com/protocolbuffers/protobuf/releases

Download the corresponding OS compression package to decompress,binYou can see the compiled Protoc, you can use it in the system PATH.

Method Two:

Download the source code slowly compile, but I won't hahahahaha

 

2, install GRPC

Project address: https://github.com/grpc/grpc
 

2.1 Using the official installation command

go get google.golang.org/grpc

But it will report an error: (I will not go online.

package google.golang.org/grpc: unrecognized import path “google.golang.org/grpc”(https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

 

2.2 Installing using gitclone

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text

cd $GOPATH/src/
go install google.golang.org/grpc

go installThe deficiencies are gitclone

 

3, install protoc-gen-go

3.1 download installation

go get -u github.com/golang/protobuf/protoc-gen-go

exist$GOPATH/binGo down to generate corresponding binary

 

3.2 Use the command description

protoc --go_out=output_directory input_directory/file.proto

Where "-go_out =" means generating a Go file, Protoc automatically looks for the Protoc-Gen-GO execution file in the PATH (system execution path).

 

3.3 Install the specified version

If you need to download the specified version of Protoc-Gen-Go, then: (Version in version can passGit Checkout [Tab Key]Add it to see what

cd "$(go env GOPATH)"/src/github.com/golang/protobuf
VERSION=v1.5.0
git checkout $VERSION
cd protoc-gen-go
go install

The generated binary will also be placed$GOPATH/binMiddle (remember$GOPATH/binJoin the system path, otherwise you can find it when using protoc

 
 

Reference link:
https://www.jianshu.com/p/dba4c7a6d608
https://blog.csdn.net/stpeace/article/details/82713647
https://studygolang.com/articles/12673?fr=sidebar
 
If there is something wrong, please point out, thank you ~

Intelligent Recommendation

GRPC basic tutorial service creation

Interface implementation: Simple RPC: The client initiates the Point request, the server returns the Feature Message The entire interface request response process: 1. Execute the response process: onn...

GRPC learning notes | detailed tutorial

Original address:GRPC learning notes | detailed tutorial(Permanent address, save the URL is not lost) Problem Description We believe that after the prevailing from DevOps, the operation and maintenanc...

gRPC concept

This paper introduces some key points of the gRPC concept through the structural overview and life cycle of gRPC. Overview Service definition Like many RPC systems, gRPC is based on the idea of ​​defi...

Latex basic installation and learning tutorial

Latex download and install and use tutorial 1. Introduction (1) About LaTeX and CTeX Two TexLive2018 + WinEdt10.3 installation (1) Basic installation (B) Introduction (3) Complete entry resources: 1. ...

Docker installation basic learning tutorial

Table of contents 1. What is the docker? Second, CentOS Docker installation (CentOS7 and above) 1.Docker installation 2. Docker start and stop 2. Docker uninstall (RM -RF carefully) 3. Docker mirror o...

More Recommendation

Node.js zero-based detailed tutorial (1): installation + basic concept

  The first chapter recommends 2 hours of study. The course has 10 chapters. Learning style: read in detail and manually implement the relevant code learning targetThis tutorial will teach you to...

Docker Operation Tutorial (1) Basic Concept and Installation of Docker

First, what is docker 1, Docker's role and principle Docker container technology can imagine a software industry container, which can quickly pack different applications or a different version of the ...

Git Tutorial - Basic Concept

reference:Liao Xuefeng Tutorial 1 Introduction Git is a distributed version control system. Key isdistributedwithversion control。 Distributed and centralized contrast Each time you need to connect to ...

Redux Tutorial --- Basic Concept

1. Redux contains the following parts containing applications state: The real data source of the driver application view: UI declaration description based on the current state actions: Depending on th...

Section 3 GRPC-JAVA basic tutorial

Table of contents I. Overview 2. Route_guide.proto file 3. Generate code (slightly) Fourth, code detailed explanation 4-1 service side code 4-2 tool class 4-3 client and running results display 4-3-1 ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top