main.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "time"
  8. "github.com/tal-tech/go-zero/core/discov"
  9. "github.com/tal-tech/go-zero/zrpc"
  10. "github.com/zeromicro/zero-examples/rpc/remote/unary"
  11. )
  12. var lb = flag.String("t", "direct", "the load balancer type")
  13. func main() {
  14. flag.Parse()
  15. var cli zrpc.Client
  16. switch *lb {
  17. case "direct":
  18. cli = zrpc.MustNewClient(zrpc.RpcClientConf{
  19. Endpoints: []string{
  20. "localhost:3456",
  21. "localhost:3457",
  22. },
  23. })
  24. case "discov":
  25. cli = zrpc.MustNewClient(zrpc.RpcClientConf{
  26. Etcd: discov.EtcdConf{
  27. Hosts: []string{"localhost:2379"},
  28. Key: "zrpc",
  29. },
  30. })
  31. default:
  32. log.Fatal("bad load balancing type")
  33. }
  34. greet := unary.NewGreeterClient(cli.Conn())
  35. ticker := time.NewTicker(time.Second)
  36. defer ticker.Stop()
  37. for {
  38. select {
  39. case <-ticker.C:
  40. resp, err := greet.Greet(context.Background(), &unary.Request{
  41. Name: "kevin",
  42. })
  43. if err != nil {
  44. fmt.Println("X", err.Error())
  45. } else {
  46. fmt.Println("=>", resp.Greet)
  47. }
  48. }
  49. }
  50. }