Next we need to generate the gRPC client and server interfaces from our .proto service definition. We do this using the protocol buffer compiler protoc with a special gRPC Go plugin.
For simplicity, we’ve provided a bash script that runs protoc for you with the appropriate plugin, input, and output (if you want to run this by yourself, make sure you’ve installed protoc and followed the gRPC-Go installation instructions first):
[Read More]
Building a gRPC Client in Go
In this section, we’ll look at creating a Go client for our RouteGuide service. You can see our complete example client code in grpc-go/examples/route_guide/client/client.go.
Creating a stub To call service methods, we first need to create a gRPC channel to communicate with the server. We create this by passing the server address and port number to grpc.Dial() as follows:
conn, err := grpc.Dial(*serverAddr) if err != nil { ... } defer conn.
[Read More]
Building a gRPC Server in Go
Let’s start off by defining a really simple gRPC server in Go. Once we have a simple server up and running we can set about creating a gRPC client that will be able to interact with it.
We will start off by writing the logic within our main function to listen on a port for incoming TCP connections:
main.go
package main import ( "log" "net" ) func main() { lis, err := net.
[Read More]
Cobra - Create CLI in Go
Golang for CLI Go is the perfect language to develop command line applications. Go has a few advantages that really set it apart from other languages:
Single binary Very fast execution time, no interpreter needed Go is awesome! Cross platform support Command line based applications are nearly as old as computing itself but this doesn’t mean that they haven’t evolved. Traditional cli applications used flags to manage the different behaviors an application could perform.
[Read More]
Defining gRPC service
Our first step (as you’ll know from the quick start) is to define the gRPC service and the method request and response types using protocol buffers. You can see the complete .proto file in examples/route_guide/routeguide/route_guide.proto.
To define a service, you specify a named service in your .proto file:
service RouteGuide { ... } Then you define rpc methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the RouteGuide service:
[Read More]
Setting up gRPC
The example code for our tutorial is in grpc/grpc-go/examples/route_guide. To download the example, clone the grpc-go repository by running the following command:
go get google.golang.org/grpc
Then change your current directory to grpc-go/examples/route_guide:
cd $GOPATH/src/google.golang.org/grpc/examples/route_guide
You also should have the relevant tools installed to generate the server and client interface code - if you don’t already, follow the setup instructions in the Go quick start guide.
Protocol buffers
Intro Protobuf (or Protocol Buffers) is a language-agnostic and platform-neutral serialization format invented at Google. Each protocol buffer message is a small logical record of information, containing a series of name-value pairs.
Unlike XML or JSON, here you first define the schema in a .proto file. They are a format like JSON but simpler, smaller, strictly typed, understandable only from the client to the server and faster to Marshall/Unmarshall. For example:
[Read More]
Why use gRPC?
With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.
[Read More]
Challenges with gRPC
Challenges with gRPC You should bear in mind that whilst gRPC does allow you to utilize these newer bits of technology, it is more challenging prototyping a gRPC service due to the fact that tools like the Postman HTTP client cannot be used in order to easily interact with your exposed gRPC service.
You do have options that make this possible, but it’s not something that’s readily available natively. There are options to use tools such as envoy to reverse proxy standard JSON requests and transcode them into the right data format but this is an additional dependency that can be tricky to set up for simple projects.
[Read More]
gRPC vs Rest
Whilst REST and gRPC are somewhat similar, there are some fundamental differences in how they work that you should be aware of.
gRPC utilizes HTTP/2 whereas REST utilizes HTTP 1.1 gRPC utilizes the protocol buffer data format as opposed to the standard JSON data format that is typically used within REST APIs. This provides us with strong schema and backward compatibility. With gRPC you can utilize HTTP/2 capabilities such as server-side streaming, client-side streaming or even bidirectional-streaming should you wish.
[Read More]