Routing and Load Balancing

Routing and Load Balancing

Interface Implementation

The main interface for routing and load balancing is Selector, and a default Selector implementation is also provided in the same directory. This implementation can implement node weight calculation algorithm, service routing filtering strategy, and load balancing algorithm by replacing NodeBuilder , Filter , Balancer , and Pluggable.

type Selector interface {
    // The list of service nodes maintained internally by the Selector is updated through the Rebalancer interface.
    Rebalancer

    // Select nodes
    // if err == nil, selected and done must not be empty.
    Select(ctx context.Context, opts ...SelectOption) (selected Node, done DoneFunc, err error)
}

// Realize service node change awareness through Rebalancer.
type Rebalancer interface {
    Apply(nodes []Node)
}

Supported implementations:

  • wrr : Weighted round robin (Kratos Client built-in default algorithm)
  • p2c : Power of two choices
  • random : Random

How to use

HTTP Client

import "github.com/go-kratos/kratos/v2/selector/wrr"
import "github.com/go-kratos/kratos/v2/selector/filter"

// Create a route Filter: filter instances with version number "2.0.0".
filter := filter.Version("2.0.0")
// Create P2C load balancing algorithm Selector, and inject routing Filter.
selector.SetGlobalSelector(wrr.NewBuilder())

hConn, err := http.NewClient(
  http.WithEndpoint("discovery:///helloworld"),
  http.WithDiscovery(r),
  http.WithNodeFilter(filter)
)

gRPC Client

import "github.com/go-kratos/kratos/v2/selector/wrr"
import "github.com/go-kratos/kratos/v2/selector/filter"

// Create a route Filter: filter instances with version number "2.0.0".
filter := filter.Version("2.0.0")
// Due to the limitations of the gRPC framework, only the global balancer name can be used to inject Selector.
selector.SetGlobalSelector(wrr.NewBuilder())

conn, err := grpc.DialInsecure(
  context.Background(),
  grpc.WithEndpoint("discovery:///helloworld"),
  grpc.WithDiscovery(r),

  // Inject routing Filter through grpc.WithFilter.
  grpc.WithNodeFilter(filter),
)