client.go 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "log"
  7. "sync"
  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/stream"
  11. )
  12. const name = "kevin"
  13. var key = flag.String("key", "zrpc", "the key on etcd")
  14. func main() {
  15. flag.Parse()
  16. client, err := zrpc.NewClientNoAuth(discov.EtcdConf{
  17. Hosts: []string{"localhost:2379"},
  18. Key: *key,
  19. })
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. conn := client.Conn()
  24. greet := stream.NewStreamGreeterClient(conn)
  25. stm, err := greet.Greet(context.Background())
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. var wg sync.WaitGroup
  30. go func() {
  31. for {
  32. resp, err := stm.Recv()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("=>", resp.Greet)
  37. wg.Done()
  38. }
  39. }()
  40. for i := 0; i < 3; i++ {
  41. wg.Add(1)
  42. fmt.Println("<=", name)
  43. if err = stm.Send(&stream.StreamReq{
  44. Name: name,
  45. }); err != nil {
  46. log.Fatal(err)
  47. }
  48. }
  49. wg.Wait()
  50. }