Golang RPC's Grpc

Source: Internet
Author: User
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:

    1. Installation of GRPC

      $ go get-u google.golang.org/grpc

    2. 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.

  1. Create Project Golang Demo Project:


    Paste_image.png
    1. The Main.go in the client directory implements the data that clients use to send data and print after receiving server-side processing
    2. 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.
    3. Example Package user writes Proto file and generates data interface
  2. Define the GRPC interface:

    syntax = "proto3";package example;service FormatData { rpc DoFormat(Data) returns (Data){}}message Data { string text = 1;}
  3. 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)}}  
  4. 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)}
  5. Perform validation results:
    1. Start the server before executing the client
    2. Client Side Console If you print the result: hello,world! , proving that the Grpc interface definition was successful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.