This library support new grpc-go/balancer interface.
You can pass self-implemented endpoint providers. For this you can realize service interface and pass implementation to the .NewBalancerBuilder
constructor. This library support consul by default.
package api // which contains code generated from the proto-files
import (
"github.com/mbobakov/grpc-dynamic-balancer"
"github.com/mbobakov/grpc-dynamic-balancer/provider/consul"
"google.golang.org/grpc"
grpcbalancer "google.golang.org/grpc/balancer"
)
func NewLoadBalancedClient(consulAddr, serviceName string, opts ...grpc.DialOption) (<Client>, error) {
bb := balancer.NewBalancerBuilder(serviceName, &consul.Consul{})
grpcbalancer.Register(bb)
opts = append(opts, grpc.WithBalancerName(serviceName))
conn, err := grpc.Dial(consulAddr, opts...)
if err != nil { <handle error> }
return &grpcGeneratedClient{cc: conn}, nil
}
After some test load-balancing on the client-side is the most efficient way to HA. Tests(envoy,nginx) TBD
Feel free to open issues, PRs and refer to the implementation of endpoints providers