lb

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2020 License: MIT Imports: 11 Imported by: 0

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsulResolver

type ConsulResolver struct {
	// contains filtered or unexported fields
}

ConsulResolver implements the gRPC Resolver interface using a Consul backend.

See the gRPC load balancing documentation for details about Balancer and Resolver: https://github.com/grpc/grpc/blob/master/doc/load-balancing.md.

func NewConsulResolver

func NewConsulResolver(client *api.Client, service, tag string) (*ConsulResolver, error)

NewConsulResolver initializes and returns a new ConsulResolver.

It resolves addresses for gRPC connections to the given service and tag. If the tag is irrelevant, use an empty string.

func (*ConsulResolver) Close

func (r *ConsulResolver) Close()

Close closes the watcher.

func (*ConsulResolver) Next

func (r *ConsulResolver) Next() ([]*naming.Update, error)

Next blocks until an update or error happens. It may return one or more updates. The first call will return the full set of instances available as NewConsulResolver will look those up. Subsequent calls to Next() will block until the resolver finds any new or removed instance.

An error is returned if and only if the watcher cannot recover.

func (*ConsulResolver) Resolve

func (r *ConsulResolver) Resolve(target string) (naming.Watcher, error)

Resolve creates a watcher for target. The watcher interface is implemented by ConsulResolver as well, see Next and Close.

type HealthzEndpoint

type HealthzEndpoint struct {
	Addr     string // e.g. 127.0.0.1:10000
	CheckURL string // e.g. http://127.0.0.1:10000/healthz
	// contains filtered or unexported fields
}

HealthzEndpoint is an endpoint that serves gRPC and responds to health checks on the CheckURL.

type HealthzResolver

type HealthzResolver struct {
	// contains filtered or unexported fields
}

HealthzResolver implements the gRPC Resolver interface using a simple health endpoint check on a list of clients initially passed to the resolver.

See the gRPC load balancing documentation for details about Balancer and Resolver: https://github.com/grpc/grpc/blob/master/doc/load-balancing.md.

func NewHealthzResolver

func NewHealthzResolver(endpoints ...HealthzEndpoint) (*HealthzResolver, error)

NewHealthzResolver initializes and returns a new HealthzResolver.

It resolves addresses for gRPC connections to the given list of host:port endpoints. It does

func (*HealthzResolver) Close

func (r *HealthzResolver) Close()

Close closes the watcher.

func (*HealthzResolver) Next

func (r *HealthzResolver) Next() ([]*naming.Update, error)

Next blocks until an update or error happens. It may return one or more updates. The first call will return the full set of instances available as NewHealthzResolver will look those up. Subsequent calls to Next() will block until the resolver finds any new or removed instance.

An error is returned if and only if the watcher cannot recover.

func (*HealthzResolver) Resolve

func (r *HealthzResolver) Resolve(target string) (naming.Watcher, error)

Resolve creates a watcher for target. The watcher interface is implemented by HealthzResolver as well, see Next and Close.

type StaticResolver

type StaticResolver struct {
	// contains filtered or unexported fields
}

StaticResolver implements a gRPC resolver/watcher that simply returns a list of addresses, then blocks.

func NewStaticResolver

func NewStaticResolver(addr ...string) *StaticResolver

NewStaticResolver initializes and returns a new StaticResolver.

func (*StaticResolver) Close

func (r *StaticResolver) Close()

Close is a no-op for a StaticResolver.

func (*StaticResolver) Next

func (r *StaticResolver) Next() ([]*naming.Update, error)

Next returns the list of addresses once, then blocks on consecutive calls.

func (*StaticResolver) Resolve

func (r *StaticResolver) Resolve(target string) (naming.Watcher, error)

Resolve creates a watcher for target. The watcher interface is implemented by StaticResolver as well, see Next and Close.

Jump to

Keyboard shortcuts

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