client.go 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "time"
  7. "github.com/tal-tech/go-zero/core/discov"
  8. "github.com/tal-tech/go-zero/example/rpc/remote/unary"
  9. "github.com/tal-tech/go-zero/zrpc"
  10. )
  11. const timeFormat = "15:04:05"
  12. func main() {
  13. flag.Parse()
  14. client := zrpc.MustNewClient(zrpc.RpcClientConf{
  15. Etcd: discov.EtcdConf{
  16. Hosts: []string{"localhost:2379"},
  17. Key: "zrpc",
  18. },
  19. })
  20. ticker := time.NewTicker(time.Second)
  21. defer ticker.Stop()
  22. for {
  23. select {
  24. case <-ticker.C:
  25. conn := client.Conn()
  26. greet := unary.NewGreeterClient(conn)
  27. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  28. resp, err := greet.Greet(ctx, &unary.Request{
  29. Name: "kevin",
  30. })
  31. if err != nil {
  32. fmt.Printf("%s X %s\n", time.Now().Format(timeFormat), err.Error())
  33. } else {
  34. fmt.Printf("%s => %s\n", time.Now().Format(timeFormat), resp.Greet)
  35. }
  36. cancel()
  37. }
  38. }
  39. }