rpc.go 850 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package go_chat_api_util
  2. import "context"
  3. import "github.com/smallnest/rpcx/client"
  4. var rpcClient client.XClient
  5. func InitRpc(addr string) error {
  6. d, err := client.NewPeer2PeerDiscovery("tcp@"+addr, "")
  7. if err != nil {
  8. return err
  9. }
  10. // 超时设置
  11. option := client.DefaultOption
  12. rpcClient = client.NewXClient("crmRpcService", client.Failtry, client.RandomSelect, d, option)
  13. return nil
  14. }
  15. type RespData struct {
  16. Code int `json:"code"`
  17. Data interface{} `json:"data"`
  18. Msg string `json:"msg"`
  19. }
  20. func RpcCall(funcName string, body interface{}) (*RespData, error) {
  21. resp := new(RespData)
  22. err := rpcClient.Call(context.Background(), funcName, body, resp)
  23. if err != nil {
  24. log.Error("funcName:"+funcName, " err:"+err.Error(), " body:", body)
  25. return resp, err
  26. }
  27. return resp, nil
  28. }
  29. func CloseRpc() {
  30. rpcClient.Close()
  31. }