lb/

directory
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2023 License: MIT

README

gRPC Load Balancing in 5 minutes

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).

See the examples directory for a working gRPC client/server implementation.

Directories

Path Synopsis
example/proto/echo
Package echo is a generated protocol buffer package.
Package echo is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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