xrpc_client.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package rpcx
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.wanpinghui.com/WPH/go_common/wph"
  7. "git.wanpinghui.com/WPH/go_common/wph/date"
  8. "github.com/smallnest/rpcx/client"
  9. )
  10. type DemoRQ struct {
  11. ID wph.Long `json:"id"`
  12. }
  13. type DemoRES struct {
  14. ID wph.Long `json:"id"`
  15. StartTime date.Time `json:"startTime"`
  16. }
  17. func main() {
  18. ctx := context.Background()
  19. rpcRQ := &DemoRQ{ID: wph.NextId()}
  20. rpcRES := &DemoRES{}
  21. c := client.NewClient(client.DefaultOption)
  22. err := c.Connect("tcp", "localhost:10000")
  23. //err := c.Connect("tcp", "api.88ba.com:10007")
  24. if err != nil {
  25. fmt.Printf("failed to connect: %v", err)
  26. return
  27. }
  28. defer c.Close()
  29. //token := "rpc bearer cdEger5i9943rFqewg"
  30. //metadata := ctx.Value(share.ReqMetaDataKey)
  31. //if metadata == nil {
  32. // metadata = map[string]string{}
  33. // ctx = context.WithValue(ctx, share.ReqMetaDataKey, metadata)
  34. //}
  35. //m := metadata.(map[string]string)
  36. //m[share.AuthKey] = token
  37. println(1)
  38. err = c.Call(ctx, "DemoService", "TestFn", rpcRQ, rpcRES)
  39. println(2)
  40. //d := client.NewPeer2PeerDiscovery("tcp@wph-micro-auth:10007", "")
  41. //c := client.NewXClient("GovAuth", client.Failtry, client.RandomSelect, d, client.DefaultOption)
  42. //c.Auth(token)
  43. //err := c.Call(context.Background(), "CheckUser", rpcRQ, rpcRES)
  44. if err != nil {
  45. fmt.Printf("rpc client call err=%s\n", err.Error())
  46. err = nil
  47. } else {
  48. b, _ := json.Marshal(rpcRES)
  49. println("result is ", string(b))
  50. }
  51. }