GO GRPC客户端通过IP:PORT列表实现负载均衡

没有服务发现,只有固定的几台机器,可以用以下方法实现

import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/resolver"
    "google.golang.org/grpc/resolver/manual"
)
func newClient() (conn *grpc.ClientConn, err error) {
    r := manual.NewBuilderWithScheme("whatever")
    conn, err = grpc.Dial(
        r.Scheme()+":///test.server",
        grpc.WithInsecure(),
        grpc.WithResolvers(r),
        grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy":"round_robin"}`))
    r.UpdateState(resolver.State{Addresses: []resolver.Address{
        {Addr: "127.0.0.1:7000"}, {Addr: "127.0.0.1:7001"}}})
    return
}

你可能感兴趣的:(GO GRPC客户端通过IP:PORT列表实现负载均衡)