Go GRPC
Go-GRPC is a micro based gRPC framework for microservices.
Go-GRPC provides a go-micro.Service leveraging gRPC plugins for the client, server and transport. Go-GRPC shares the go-micro codebase, making it a pluggable gRPC framework for microservices. Everything works
just like a go-micro service, using micro generated protobufs and defaulting to consul for service discovery.
Go-GRPC interoperates with standard gRPC services seamlessly.
Find an example greeter service in examples/greeter.
Writing a Micro Service
Initialisation of a 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.
package main
import (
"log"
"time"
"github.com/micro/go-grpc"
"github.com/micro/go-micro"
hello "github.com/micro/go-grpc/examples/greeter/server/proto/hello"
"golang.org/x/net/context"
)
type Say struct{}
func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
log.Print("Received Say.Hello request")
rsp.Msg = "Hello " + req.Name
return nil
}
func main() {
service := grpc.NewService(
micro.Name("go.micro.srv.greeter"),
micro.RegisterTTL(time.Second*30),
micro.RegisterInterval(time.Second*10),
)
// optionally setup command line usage
service.Init()
// Register Handlers
hello.RegisterSayHandler(service.Server(), new(Say))
// Run server
if err := service.Run(); err != nil {
log.Fatal(err)
}
}