Skip to main content

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/p2c"
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 := p2c.New(p2c.WithFilter(filter))

hConn, err := http.NewClient(
context.Background(),
http.WithEndpoint("discovery:///helloworld"),
http.WithDiscovery(r),
// Inject Selector into HTTP client through http.WithSelector
http.WithSelector(
p2c.New(p2c.WithFilter(filter.Version("2.0.0"))),
)
)

gRPC Client

import "github.com/go-kratos/kratos/v2/selector/p2c"
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")

conn, err := grpc.DialInsecure(
context.Background(),
grpc.WithEndpoint("discovery:///helloworld"),
grpc.WithDiscovery(r),
// Due to the limitations of the gRPC framework, only the global balancer name can be used to inject the selector
grpc.WithBalancerName(wrr.Name),
// Inject routing Filter through grpc.WithSelectFilter
grpc.WithSelectFilter(filter),
)