GRPC---Go Quick Start

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Grpc DOCUMENT

Before you begin

Prerequisites

Go version

GRPC works with Go 1.5 or higher.

$ go version

For installation instructions, follow this guide:getting started-the Go programming Language

Install GRPC

Use the following command to install GRPC.

$ go get google.golang.org/grpc

Install Protocol Buffers v3

Install the Protoc compiler that's used to generate GRPC service code. The simplest-download pre-compiled binaries for your platform ( protoc-<version>-<platform>.zip ) from Here:https://github.com/go Ogle/protobuf/releases

    • Unzip this file.
    • Update the environment variable to include the path to the PATH protoc binary file.

Next, install the Protoc plugin for Go

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

The compiler plugin, protoc-gen-go, would be is installed in $GOBIN, and defaulting to $GOPATH/bin. It must is in your $PATH for the protocol compiler, PROTOC, to find it.

$ export PATH=$PATH:$GOPATH/bin

Download The example

The GRPC code that is fetched with go get google.golang.org/grpc also contains the examples. They can be found under the examples dir: $GOPATH/src/google.golang.org/grpc/examples .

Build The example

Change to the example directory

$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld

GRPC services was defined in a proto file, which was used to generate a corresponding. Pb.go. This file was already generated for the HelloWorld example code and can found under this directory:$GOPATH/src/google.golang.org/grpc/examples/helloworld/helloworld

This helloworld.pb.go file contains:

    • Generated client and server code.
    • Code for populating, serializing, and retrieving our HelloRequest and HelloReply message types.

Try it!

To compile and run the server and client code, the go run command can used. In the examples directory:

$ go run greeter_server/main.go

From a different terminal:

$ go run greeter_client/main.go

If things go smoothly, you'll see the ' in the ' Greeting: Hello world client side output.

congratulations! You ' ve just run a client-server application with GRPC.

Update a GRPC service

Now let's look at how to update the application with a extra method on the server for the client to call. Our GRPC service is defined using protocol buffers; You can find the lots more on how to define a service in a file in what is .proto grpc?and grpc basics:go. For now all need to know are that both the server and the client "stub" has a SayHello RPC method that takes a HelloRequest para Meter from the client and returns a from HelloReply the server, and that this method was defined like this:

// 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 greetingsmessage HelloReply {  string message = 1;}

Let's update this so, the service has both Greeter methods. Make sure is in the same examples dir as above ( $GOPATH/src/google.golang.org/grpc/examples/helloworld )

Edit helloworld/helloworld.proto and update it with a new SayHelloAgain method, with the same request and response types:

// 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 greetingsmessage HelloReply {  string message = 1;}

Generate GRPC Code

Next We need to update the GRPC code used by the new service definition. From the same examples dir as above ( $GOPATH/src/google.golang.org/grpc/examples/helloworld )

$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

This regenerates the helloworld.pb.go with our new changes.

Update and run the application

We are now having new generated server and client code, but we still need to implement and call the new method in the Human-writ Ten parts of our example application.

Update the server

Edit and greeter_server/main.go Add the following function to it:

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

Update the Client

Edit to greeter_client/main.go Add the following code to the main function.

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

run!

Run the server

$ go run greeter_server/main.go

On a different terminal, run the client

$ go run greeter_client/main.go

You should see the updated output:

  $ go run Greeter_client/main.gogreeting:hello Worldgreeting:hello again world  
Related Article

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.