github.com/jtzjtz/kit@v1.0.2/balance/example/client/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/jtzjtz/kit/balance"
     7  	pb "github.com/jtzjtz/kit/balance/example/pb"
     8  	"golang.org/x/net/context"
     9  	"google.golang.org/grpc"
    10  	"google.golang.org/grpc/resolver"
    11  	"time"
    12  )
    13  
    14  func main() {
    15  
    16  	//客户端发现服务开始
    17  	r := balance.NewResolver(balance.NacosConfig{NacosIp: "localhost", NacosPort: 8848, NamespaceId: "962222fe-63a2-42aa-a456-b880587931e2", GroupName: "test-app"})
    18  	resolver.Register(r)
    19  	serviceName := "testservice"
    20  	//pick_first/round_robin
    21  	conn, err := grpc.Dial(r.Scheme()+":///"+serviceName, grpc.WithDefaultServiceConfig(`{"LoadBalancingPolicy": "round_robin"}`), grpc.WithInsecure())
    22  	//conn, err := grpc.Dial(r.Scheme()+"://author/"+serviceName, grpc.WithBalancerName("round_robin"), grpc.WithInsecure())
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  	//客户端发现服务结束
    27  
    28  	client := pb.NewGreeterClient(conn)
    29  
    30  	for {
    31  		resp, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "hello"}, grpc.FailFast(true))
    32  		if err != nil {
    33  			fmt.Println(err)
    34  		} else {
    35  			fmt.Println(resp)
    36  		}
    37  
    38  		<-time.After(time.Second)
    39  	}
    40  }