Prerequisites
Go version
gRPC works with Go 1.5 or higher.
|
|
For installation instructions, follow this guide: Getting Started - The Go Programming Language
Install gRPC
Use the following command to install gRPC.
|
|
Install Protocol Buffers v3
Install the protoc compiler that is used to generate gRPC service code. The simplest way to do this is to download pre-compiled binaries for your platform(protoc-<version>-<platform>.zip
) from here: https://github.com/google/protobuf/releases
- Unzip this file.
- Update the environment variable
PATH
to include the path to the protoc binary file.
Next, install the protoc plugin for Go
|
|
The compiler plugin, protoc-gen-go, will be installed in $GOBIN, defaulting to $GOPATH/bin. It must be in your $PATH for the protocol compiler, protoc, to find it.
|
|
Download the example
The grpc code that was 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
|
|
gRPC services are defined in a proto file, which is used to generate a corresponding .pb.go. This file is already generated for the helloworld example code and can be 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
andHelloReply
message types.
Try it!
To compile and run the server and client code, the go run
command can be used. In the examples directory:
|
|
From a different terminal:
|
|
If things go smoothly, you will see the Greeting: Hello world
in the 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 an extra method on the server for the client to call. Our gRPC service is defined using protocol buffers; you can find out lots more about how to define a service in a .proto
file in What is gRPC? and gRPC Basics: Go. For now all you need to know is that both the server and the client “stub” have a SayHello
RPC method that takes a HelloRequest
parameter from the client and returns a HelloReply
from the server, and that this method is defined like this:
|
|
Let’s update this so that the Greeter
service has two methods. Make sure you are 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:
|
|
Generate gRPC code
Next we need to update the gRPC code used by our application to use the new service definition. From the same examples dir as above ($GOPATH/src/google.golang.org/grpc/examples/helloworld
)
|
|
This regenerates the helloworld.pb.go with our new changes.
Update and run the application
We now have new generated server and client code, but we still need to implement and call the new method in the human-written parts of our example application.
Update the server
Edit greeter_server/main.go
and add the following function to it:
|
|
Update the client
Edit greeter_client/main.go
to add the following code to the main function.
|
|
Run!
Run the server
|
|
On a different terminal, run the client
|
|
You should see the updated output:
|
|