没有服务发现,只有固定的几台机器,可以用以下方法实现
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
}
网友评论