grpc

package module
v0.0.0-...-4940c1b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

Micro gRPC License GoDoc Travis CI Go Report Card

This is a micro gRPC framework. A simplified experience for building gRPC services.

Go-grpc makes use of go-micro plugins to create a better framework for gRPC development. It interoperates with standard gRPC services seamlessly, including the grpc-gateway.

Find an example greeter service in examples/greeter.

Getting Started

Install Protobuf

Protobuf is required for code generation

You'll need to install:

Service Discovery

Service discovery is used to resolve service names to addresses.

Consul

Consul is used as the default service discovery system. See the install guide.

Discovery is pluggable. Find plugins for etcd, kubernetes, zookeeper and more in the micro/go-plugins repo.

mDNS

Multicast DNS is a built in alternative for zero dependencies.

Pass --registry=mdns to any command or the enviroment variable MICRO_REGISTRY=mdns

MICRO_REGISTRY=mdns go run main.go

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.

package main

import (
	"context"
	"time"

	"github.com/micro/go-grpc"
	"github.com/micro/go-micro"
	hello "github.com/micro/go-grpc/examples/greeter/server/proto/hello"
)

type Say struct{}

func (s *Say) Hello(ctx context.Context, req *hello.Request, rsp *hello.Response) error {
	rsp.Msg = "Hello " + req.Name
	return nil
}

func main() {
	service := grpc.NewService(
		micro.Name("greeter"),
	)

	service.Init()

	hello.RegisterSayHandler(service.Server(), new(Say))

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

Use with Micro

You may want to use the micro toolkit with grpc services. To do this either use the prebuilt toolkit or simply include the grpc client plugin and rebuild the toolkit.

Go Get
go get github.com/micro/grpc/cmd/micro
Build Yourself
go get github.com/micro/micro

Create a plugins.go file

package main

import _ "github.com/micro/go-plugins/client/grpc"
import _ "github.com/micro/go-plugins/server/grpc"

Build binary

// For local use
go build -i -o micro ./main.go ./plugins.go

Flag usage of plugins

micro --client=grpc --server=grpc

Use with gRPC Gateway

Go-grpc 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFunction

func NewFunction(opts ...micro.Option) micro.Function

NewFunction returns a grpc service compatible with go-micro.Function

func NewService

func NewService(opts ...micro.Option) micro.Service

NewService returns a grpc service compatible with go-micro.Service

func WithTLS

func WithTLS(t *tls.Config) micro.Option

WithTLS sets the TLS config for the service

Types

This section is empty.

Directories

Path Synopsis
examples
greeter/function/proto/hello
Package go_micro_srv_greeter is a generated protocol buffer package.
Package go_micro_srv_greeter is a generated protocol buffer package.
greeter/gateway/proto/hello
Package greeter is a generated protocol buffer package.
Package greeter is a generated protocol buffer package.
greeter/server/proto/hello
Package go_micro_srv_greeter is a generated protocol buffer package.
Package go_micro_srv_greeter is a generated protocol buffer package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL