This is a creation in Article, where the information may have evolved or changed.
GRPC Introduction:
GRPC is a high-performance, open-source RPC framework that is developed from Google, based on the PROTOBUF serialization protocol and supports multiple languages (Golang, Python, Java, etc.). Because GRPC's support for the HTTP/2 protocol makes it a good prospect for the development of client backend services such as Android and IOS. GRPC provides an easy way to define services, while clients can take advantage of the HTTP2 stream features to help conserve bandwidth, reduce TCP connections, save CPU usage, and more.
Installation:
Installation of GRPC
$ go get-u google.golang.org/grpc
Because GRPC is an interface serialization based on the PROTOBUF implementation, you also install the PROTOBUF: Installation and introduction tutorial.
Practice:
Below we use GRPC to define an interface that implements the uppercase formatting of the incoming data.
Create Project Golang Demo Project:
Paste_image.png
- The Main.go in the client directory implements the data that clients use to send data and print after receiving server-side processing
- The main.go in the server directory implements the service side to receive the data sent by the client, and then returns the data to the client when it is processed in uppercase.
- Example Package user writes Proto file and generates data interface
Define the GRPC interface:
syntax = "proto3";package example;service FormatData { rpc DoFormat(Data) returns (Data){}}message Data { string text = 1;}
-
Implement server-side
package mainimport ("demo/example" "Net" "Google.golang.org/grpc" " Google.golang.org/grpc/reflection "" Golang.org/x/net/context "" Strings "" Log "//define the Listener address const (HOST string =" localhost ") "Port string =" 8080 ")//Defines the interface type Formatdata struct{}func (fd *formatdata) Doformat (CTX context. Context, in *example. Data) (Out *example. Data, err Error) {str: = in. Text out = &example. Data{text:strings. ToUpper (str)} return out, nil}//directly registers the interface Func main () {listener, err: = Net in the Main method. Listen ("TCP", host+ ":" +port) if err! = Nil {log. Fatalln ("Faile Listen at:" + HOST + ":" + PORT)} else {log. PRINTLN ("Demo server is listening at:" + HOST + ":" + PORT)} rpcserver: = Grpc. NewServer () example. Registerformatdataserver (Rpcserver, &formatdata{}) reflection. Register (rpcserver) If Err = Rpcserver.serve (listener); Err! = Nil {log. Fatalln ("Faile serve at:" + HOST + ":" + PORT)}}
- Implement Client side:
package mainimport ( "google.golang.org/grpc" "log" "Demo/example" "golang.org/x/net/context")// 定义请求地址const ( ADDRESS string = "localhost:8080")// main 方法实现对 gRPC 接口的请求func main() { conn, err := grpc.Dial(ADDRESS, grpc.WithInsecure()) if err != nil { log.Fatalln("Can't connect: " + ADDRESS) } defer conn.Close() client := example.NewFormatDataClient(conn) resp,err := client.DoFormat(context.Background(), &example.Data{Text:"hello,world!"}) if err != nil { log.Fatalln("Do Format error:" + err.Error()) } log.Println(resp.Text)}
- Perform validation results:
- Start the server before executing the client
- Client Side Console If you print the result: hello,world! , proving that the Grpc interface definition was successful