client.go 760 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "time"
  7. "github.com/tal-tech/go-zero/core/conf"
  8. "github.com/tal-tech/go-zero/zrpc"
  9. "github.com/zeromicro/zero-examples/rpc/remote/unary"
  10. )
  11. var configFile = flag.String("f", "config.json", "the config file")
  12. func main() {
  13. flag.Parse()
  14. var c zrpc.RpcClientConf
  15. conf.MustLoad(*configFile, &c)
  16. client := zrpc.MustNewClient(c)
  17. ticker := time.NewTicker(time.Millisecond * 500)
  18. defer ticker.Stop()
  19. for {
  20. select {
  21. case <-ticker.C:
  22. conn := client.Conn()
  23. greet := unary.NewGreeterClient(conn)
  24. resp, err := greet.Greet(context.Background(), &unary.Request{
  25. Name: "kevin",
  26. })
  27. if err != nil {
  28. fmt.Println("X", err.Error())
  29. } else {
  30. fmt.Println("=>", resp.Greet)
  31. }
  32. }
  33. }
  34. }