client.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "go-common/library/ecode"
  7. "go-common/library/net/rpc/warden"
  8. pb "go-common/library/net/rpc/warden/proto/testproto"
  9. "github.com/golang/protobuf/ptypes"
  10. "github.com/golang/protobuf/ptypes/any"
  11. "github.com/pkg/errors"
  12. )
  13. // usage: ./client -grpc.target=test.service=127.0.0.1:8080
  14. func main() {
  15. flag.Parse()
  16. conn, err := warden.NewConn("discovery://d/test.service?t=t&cluster=asdasd")
  17. if err != nil {
  18. panic(err)
  19. }
  20. cli := pb.NewGreeterClient(conn)
  21. normalCall(cli)
  22. errDetailCall(cli)
  23. }
  24. func normalCall(cli pb.GreeterClient) {
  25. reply, err := cli.SayHello(context.Background(), &pb.HelloRequest{Name: "tom", Age: 23})
  26. if err != nil {
  27. panic(err)
  28. }
  29. fmt.Println("get reply:", *reply)
  30. }
  31. func errDetailCall(cli pb.GreeterClient) {
  32. _, err := cli.SayHello(context.Background(), &pb.HelloRequest{Name: "err_detail_test", Age: 12})
  33. if err != nil {
  34. any := errors.Cause(err).(ecode.Codes).Details()[0].(*any.Any)
  35. var re pb.HelloReply
  36. err := ptypes.UnmarshalAny(any, &re)
  37. if err == nil {
  38. fmt.Printf("cli.SayHello get error detail!detail:=%v", re)
  39. }
  40. return
  41. }
  42. }