Golang gRPC basic introduction 1: introduction and installation

What you are wasting today is tomorrow for those who died yesterday; what you hate now is the future you can not go back.

What you waste today is the tomorrow that those who died yesterday wished; what you hate now is the future you can't go back to.

Introduction to gRPC:

gRPC is a high-performance, open-source RPC framework produced by Google, developed based on the ProtoBuf serialization protocol, and supports multiple languages ​​(Golang, Python, Java, etc.). This article only introduces the use of gRPC in Golang. Because gRPC supports the HTTP/2 protocol, it has a good prospect in the development of client backend services such as Android and IOS. gRPC provides an easy way to define services, while clients can take full advantage of the features of HTTP/2 stream, which helps to save bandwidth, reduce the number of TCP connections, save CPU usage, etc.

Install:

    1. Installation of gRPC:

go get -u google.golang.org/grpc

This link requires fq. If you don't know how to FQ, you can directly download the third-party package and install it. Provide an address where you can download without FQ: https://www.golangtc.com/download/package

  • Specific installation method:

    Put the downloaded third-party package under $GOPATH/src, and then go install XXX.com/XX (package name). You may need other third-party packages, just go get everything you need

    2. Installation of protobuf

Because gRPC is based on the interface serialization implemented by protobuf, protobuf is also installed.

  1. Install protoc: Protoc download address , you can download the corresponding protoc according to your own system, and Windows users can download the win32 version uniformly.
  2. Configure protoc to the environment variable of the system, and execute the following command to check whether the installation is successful: protoc 

    3. Install ProtoBuf-related golang dependencies

go get -u github.com/golang/protobuf/{protoc-gen-go,proto}

// 可以分步执行
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go

If you can't download it, you can download and install it using the method mentioned above. After the installation is successful, check whether there is protoc-gen-go.exe (under windows) in $GOPATH/bin.

Even if the installation is successful at this point, the following can be like the testing phase immediately.

Summarize:

    1. Q: Why is there an HTTP request, and why is it still called by rpc?

        First, http is the hypertext transfer protocol.

        rpc is a remote procedure call, and its call protocol usually includes a transmission protocol and an encoding protocol. The http2 protocol used, and the tcp protocol for custom packets such as dubbo

        Reference article:

            https://www.zhihu.com/question/41609070?sort=created

     2. The survival of the fittest between HTTP services and RPC services?

        Because RPC is more efficient, and HTTP service development iteration will be faster and relatively simple, RPC is mostly used in large enterprises

 

 

 

 

 

 

 

 

 

RPC call flow chart

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324455824&siteId=291194637