1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Client.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
chenxuyi 提交于 2022-08-27 17:17 . update
package cRpc
import (
"encoding/json"
"gitee.com/csingo/cCommon"
"gitee.com/csingo/cHTTPClient"
"gitee.com/csingo/cLog"
"github.com/gin-gonic/gin"
)
func Call(ctx *gin.Context, client RpcClientInterface, method string, req, rsp interface{}) (err error) {
app, service := client.RpcClientName()
host := clientContainer.GetHost(app, service)
uri := selfRpcConf.RequestURI
if uri == "" {
uri = "/rpc/call"
}
data, err := json.Marshal(req)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call:json.Marshal",
"req": req,
"err": err,
}).Error("rpc 请求参数格式化异常")
return
}
var body []byte
option := cHTTPClient.Option{
Host: host,
Uri: uri,
Method: cHTTPClient.MethodPOST,
Headers: map[string]string{
"Content-Type": "application/json",
"X-Trace-Id": ctx.GetString(cCommon.XIndex_TraceId),
"Rpc-App": app,
"Rpc-Service": service,
"Rpc-Method": method,
},
Query: nil,
Data: string(data),
}
_, body, err = cHTTPClient.Request(option)
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call:cHTTPClient.Request",
"option": option,
}).Debug("发起 rpc 请求")
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call:cHTTPClient.Request",
"body": body,
"err": err,
}).Error("rpc 请求结果获取异常")
return
}
err = json.Unmarshal(body, rsp)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cPrc.Call: json.Unmarshal",
"err": err,
}).Error("rpc 请求结果 json 解析异常")
}
return
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.0.160

搜索帮助