微服务架构里面,每一个服务都会有不少节点,若是流量分配不均匀,会形成资源的浪费,甚至将一些机器压垮,这个时候就须要负载均衡,最简单的一种策略就是轮询,顺序依次选择不一样的节点访问git

grpc 在客户端提供了负载均衡的实现,并提供了服务地址解析和更新的接口(默认提供了 DNS 域名解析的支持),方便不一样服务的集成github

使用示例

conn, err := grpc.Dial(
    "",
    grpc.WithInsecure(),
    // 负载均衡,使用 consul 做服务发现
    grpc.WithBalancer(grpc.RoundRobin(grpclb.NewConsulResolver(
        "127.0.0.1:8500", "grpc.health.v1.add",
    ))),
)
WithBalancer

与 consul 的集成

naming.Resolvernaming.Watcher
naming.Resolvernaming.Watcher
func NewConsulResolver(address string, service string) naming.Resolver {
    return &consulResolver{
        address: address,
        service: service,
    }
}

type consulResolver struct {
    address string
    service string
}

func (r *consulResolver) Resolve(target string) (naming.Watcher, error) {
    config := api.DefaultConfig()
    config.Address = r.address
    client, err := api.NewClient(config)
    if err != nil {
        return nil, err
    }

    return &consulWatcher{
        client:  client,
        service: r.service,
        addrs:   map[string]struct{}{},
    }, nil
}

type consulWatcher struct {
    client    *api.Client
    service   string
    addrs     map[string]struct{}
    lastIndex uint64
}

func (w *consulWatcher) Next() ([]*naming.Update, error) {
    for {
        services, metainfo, err := w.client.Health().Service(w.service, "", true, &api.QueryOptions{
            WaitIndex: w.lastIndex, // 同步点,这个调用将一直阻塞,直到有新的更新
        })
        if err != nil {
            logrus.Warn("error retrieving instances from Consul: %v", err)
        }
        w.lastIndex = metainfo.LastIndex

        addrs := map[string]struct{}{}
        for _, service := range services {
            addrs[net.JoinHostPort(service.Service.Address, strconv.Itoa(service.Service.Port))] = struct{}{}
        }

        var updates []*naming.Update
        for addr := range w.addrs {
            if _, ok := addrs[addr]; !ok {
                updates = append(updates, &naming.Update{Op: naming.Delete, Addr: addr})
            }
        }

        for addr := range addrs {
            if _, ok := w.addrs[addr]; !ok {
                updates = append(updates, &naming.Update{Op: naming.Add, Addr: addr})
            }
        }

        if len(updates) != 0 {
            w.addrs = addrs
            return updates, nil
        }
    }
}

func (w *consulWatcher) Close() {
    // nothing to do
}

参考连接