gRPC Service

gRPC service integration with go-micro

Overview

Our gRPC service makes use of go-micro plugins to create a simpler framework for gRPC development. It interoperates with standard gRPC services seamlessly, including the grpc-gateway. The grpc service uses the go-micro client and server plugins. Because gRPC is a tightly coupled protocol and framework we ignore the go-micro codec and transport plugins.

Internally we make use of the gRPC framework but hide the complexity.

Examples

Find an example greeter service in examples/grpc

Getting Started

Install Protobuf

Protobuf is required for code generation

You’ll need to install:

Writing a Service

Go-grpc service is identical to a go-micro service. Which means you can swap out micro.NewService for grpc.NewService with zero other code changes.

  1. package main
  2. import (
  3. "context"
  4. "time"
  5. "github.com/micro/go-micro/v2"
  6. "github.com/micro/go-micro/v2/service/grpc"
  7. hello "github.com/micro/go-micro/v2/service/grpc/examples/greeter/server/proto/hello"
  8. )
  9. type Say struct{}
  10. func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
  11. rsp.Msg = "Hello " + req.Name
  12. return nil
  13. }
  14. func main() {
  15. service := grpc.NewService(
  16. micro.Name("greeter"),
  17. )
  18. service.Init()
  19. hello.RegisterSayHandler(service.Server(), new(Say))
  20. if err := service.Run(); err != nil {
  21. log.Fatal(err)
  22. }
  23. }

Use with Micro

You may want to use the micro toolkit with grpc services. Simply flag flip or use env vars to set the grpc client and server like below.

Using env vars

  1. MICRO_CLIENT=grpc MICRO_SERVER=grpc micro api

Using flags

  1. micro --client=grpc --server=grpc api

Use with gRPC Gateway

The micro gRPC plugins seamlessly integrates with the gRPC ecosystem. This means the grpc-gateway can be used as per usual.

Find an example greeter api at examples/grpc/gateway.