package main import ( "context" "fmt" "time" log "github.com/sirupsen/logrus" "google.golang.org/grpc" "myschools.me/suguo/consul-demo/consul" pb "myschools.me/suguo/consul-demo/proto" ) func main() { v := []byte("asdfsdfdsf") consul.KVPut("hello", &v, 0) vv, _ := consul.KVGet("hello", 0) fmt.Println(string(*vv)) for { time.Sleep(10 * time.Millisecond) node, err := consul.FindNode("demo") if err != nil { log.WithFields(log.Fields{ "func": "main", }).Errorf("%v", err) fmt.Printf("%v\n", err) continue } if node == nil { fmt.Println("node is empty") continue } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) // conn, err := grpc.Dial("127.0.0.1:9001", grpc.WithBlock(), grpc.WithInsecure()) conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", node.Address, node.Port), grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin")) if err != nil { fmt.Printf("%v\n", err) cancel() continue } c := pb.NewHelloClient(conn) resp, err := c.Say(ctx, &pb.SayRequest{Name: "jiale", Day: time.Now().Format("2006-01-02 15:04:05")}) if err != nil { log.WithFields(log.Fields{ "func": "main", }).Errorf("%v", err) fmt.Printf("%v\n", err) continue } log.WithFields(log.Fields{ "func": "main", }).Infof("%s", resp.Reply) conn.Close() } }