Skip to content
/ grpclb Public
forked from olivere/grpc

grpc Resolver

License

Notifications You must be signed in to change notification settings

oogway/grpclb

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC Dial Resolvers

This package implements gRPC load-balancing as described in this document.

It has these Resolver implementations:

Here's an example of setting up a Consul-based resolver for a gRPC client:

import (
	"github.com/hashicorp/consul/api"
)

func main() {
	// Create Consul client
	cli, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		log.Fatal(err)
	}

  	// Create a resolver for the "echo" service
	r, err := lb.NewConsulResolver(cli, "echo", "")
	if err != nil {
		log.Fatal(err)
	}

	// Setup a gRPC client connection
	var opts []grpc.DialOption
	opts = append(opts, grpc.WithBalancer(grpc.RoundRobin(r)))

  	// Notice you can use a blank address here
	conn, err := grpc.Dial("", opts...)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	// Every call to conn will get load-balanced between the servers
	// found for the "echo" service in Consul, e.g.:
	for i := 0; i < 100; i++ {
		ctx := context.Background()
		res, err := client.Echo(ctx, &pb.EchoRequest{Message: "Hello"})
		if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%v\n", res.Message)
		time.Sleep(1*time.Second)
	}
}

You don't need to do anything for the gRPC server-side (except registering your service in Consul, of course).

Packages

No packages published

Languages

  • Go 98.3%
  • Makefile 1.7%