你好,请问个grpc负载均衡的问题
Closed this issue · 1 comments
jwrookie commented
看了你这边的连接池给了我很大的帮助,但有一个问题我不是很明白
如下:是通过域名解析实现的一个简单的负载均衡,但我不是很明白grpc内部是怎么对多个连接进行管理的
func main() {
var svc = flag.String("service", "hello_service", "service name")
var reg = flag.String("reg", "http://192.168.127.135:2379", "register etcd address")
flag.Parse()
r := grpclb.NewResolver(*reg, *svc)
resolver.Register(r)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
DefaultServiceConfig := `{"loadBalancingPolicy":"round_robin"}`
// authority是自己随便起的,不是必须的,但是r.Scheme()+"://authority/"+*svc这种格式是必须的
conn, err := grpc.DialContext(ctx, r.Scheme()+"://authority/"+*svc, grpc.WithInsecure(), grpc.WithDefaultServiceConfig(DefaultServiceConfig), grpc.WithBlock())
if err != nil {
panic(err)
}
ticker := time.NewTicker(1000 * time.Millisecond)
client := pb.NewGreeterClient(conn)
for t := range ticker.C {
resp, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "world " + strconv.Itoa(t.Second())})
if err == nil {
logrus.Infof("%v: Reply is %s\n", t, resp.Message)
}
}
}`
rfyiamcool commented
grpc 有个 balancer 可以对域名负载均衡