main.go 637 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "git.nspix.com/golang/micro/gateway/rpc"
  6. "time"
  7. )
  8. type (
  9. mathRequest struct {
  10. NumA int `json:"num_a"`
  11. NumB int `json:"num_b"`
  12. }
  13. mathResponse struct {
  14. Value int `json:"value"`
  15. }
  16. )
  17. func main() {
  18. c := rpc.NewClient()
  19. if err := c.Dialer("tcp", "127.0.0.1:52743"); err != nil {
  20. panic(err)
  21. }
  22. defer c.Close()
  23. ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
  24. if res, err := c.Do(ctx, rpc.NewRequest("math.add", &mathRequest{NumA: 100, NumB: 12})); err == nil {
  25. var i mathResponse
  26. fmt.Println(res.Decode(&i))
  27. fmt.Println(i)
  28. } else {
  29. fmt.Println(err)
  30. }
  31. }