This commit is contained in:
huangsimin@fusen.cn 2024-01-19 16:23:47 +08:00
parent ce53f9caa3
commit e9a16ee7a2

View File

@ -54,18 +54,23 @@ func Auto{{.ClientName}}Client(ctx context.Context) {{.ClientName}}Client {
GroupName: groupName,
}
insService, err := namingClient.SelectOneHealthyInstance(sel)
if err != nil {
log.Println(err)
return nil
}
for i := 0; i < 10; i++ {
insService, err := namingClient.SelectOneHealthyInstance(sel)
if err != nil {
log.Println(err)
return nil
}
if insService.Enable && insService.Healthy {
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", insService.Ip, insService.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Println(err)
return nil
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", insService.Ip, insService.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Println(err)
return nil
}
return New{{.ClientName}}Client(conn)
}
}
return New{{.ClientName}}Client(conn)
return nil
}
@ -80,16 +85,21 @@ func Auto{{.ClientName}}ClientEx(ctx context.Context, opts ...grpc.DialOption) (
GroupName: groupName,
}
insService, err := namingClient.SelectOneHealthyInstance(sel)
if err != nil {
return nil, err
for i := 0; i < 10; i++ {
insService, err := namingClient.SelectOneHealthyInstance(sel)
if err != nil {
return nil, err
}
if insService.Enable && insService.Healthy {
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", insService.Ip, insService.Port), opts...)
if err != nil {
return nil, err
}
return New{{.ClientName}}Client(conn), nil
}
}
conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", insService.Ip, insService.Port), opts...)
if err != nil {
return nil, err
}
return New{{.ClientName}}Client(conn), nil
return nil, fmt.Errorf("no healthy with times 10")
}
{{end}}