Tuesday, 24 May 2016

gRPC for Golang

As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub that provides exactly the same methods as the server.

By default, gRPC uses protocol buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages.
1. Install the gRPC runtime

go get google.golang.org/grpc

2. Install the protocol buffer compiler and runtime with gRPC

$ go get -a github.com/golang/protobuf/protoc-gen-go
Or in windows, you can install protoc-3.0.0-beta-3-win32.zip

3. Define a service and generate gRPC code

Edit a helloworld.proto file under ./helloworld folder.
The generated code will be in the same folder as .proto file
The first parameter proto_path(or -I) must be an exact prefix of the .proto file names
The second parameter refers to the .proto file
The third parameter refers to the language of the generated code

$ protoc -I helloworld helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
This generates helloworld.pb.go, which contains generated client and server code, as well as code for populating, serializing, and retrieving message types.


No comments:

Post a Comment