github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/library/go-grpc.md (about) 1 --- 2 title: gRPC Service 3 keywords: grpc 4 tags: [go-grpc] 5 sidebar: home_sidebar 6 permalink: /go-grpc 7 summary: gRPC service integration with go-micro 8 --- 9 10 # Overview 11 12 Our gRPC service makes use of [go-micro](https://github.com/micro/go-micro) plugins to create a simpler framework for gRPC development. It interoperates with 13 standard gRPC services seamlessly, including the [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway). The grpc service uses 14 the go-micro client and server plugins. Because gRPC is a tightly coupled protocol and framework we ignore 15 the go-micro codec and transport plugins. 16 17 Internally we make use of the gRPC framework but hide the complexity. 18 19 ## Examples 20 21 Find an example greeter service in [examples/grpc](https://github.com/micro/examples/tree/master/grpc) 22 23 ## Getting Started 24 25 - [Install Protobuf](#install-protobuf) 26 - [Service Discovery](#service-discovery) 27 - [Writing a Service](#writing-a-service) 28 - [Using with Micro](#use-with-micro) 29 - [Using with gRPC Gateway](#use-with-grpc-gateway) 30 31 32 ## Install Protobuf 33 34 Protobuf is required for code generation 35 36 You'll need to install: 37 38 - [protoc-gen-micro](https://github.com/tickoalcantara12/micro/tree/master/cmd/protoc-gen-micro) 39 40 ## Writing a Service 41 42 Go-grpc service is identical to a go-micro service. Which means you can swap out `micro.NewService` for `grpc.NewService` 43 with zero other code changes. 44 45 ```go 46 package main 47 48 import ( 49 "context" 50 "time" 51 52 "github.com/micro/go-micro/v2" 53 "github.com/micro/go-micro/v2/service/grpc" 54 hello "github.com/micro/go-micro/v2/service/grpc/examples/greeter/server/proto/hello" 55 ) 56 57 type Say struct{} 58 59 func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error { 60 rsp.Msg = "Hello " + req.Name 61 return nil 62 } 63 64 func main() { 65 service := grpc.NewService( 66 micro.Name("greeter"), 67 ) 68 69 service.Init() 70 71 hello.RegisterSayHandler(service.Server(), new(Say)) 72 73 if err := service.Run(); err != nil { 74 log.Fatal(err) 75 } 76 } 77 ``` 78 79 ## Use with Micro 80 81 You may want to use the micro toolkit with grpc services. Simply flag flip or use env vars to set the 82 grpc client and server like below. 83 84 ### Using env vars 85 86 ``` 87 MICRO_CLIENT=grpc MICRO_SERVER=grpc micro api 88 ``` 89 90 ### Using flags 91 92 ```shell 93 micro --client=grpc --server=grpc api 94 ``` 95 96 ## Use with gRPC Gateway 97 98 The micro gRPC plugins seamlessly integrates with the gRPC ecosystem. This means the grpc-gateway can be used as per usual. 99 100 Find an example greeter api at [examples/grpc/gateway](https://github.com/micro/examples/tree/master/grpc/gateway). 101