1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Client.go 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
cxy1620541673 提交于 2023-09-12 18:11 . update
package cRpc
import (
"encoding/json"
"fmt"
"gitee.com/csingo/cCommon"
"gitee.com/csingo/cContext"
"gitee.com/csingo/cError"
"gitee.com/csingo/cHTTPClient"
"gitee.com/csingo/cHelper"
"gitee.com/csingo/cLog"
"gitee.com/csingo/cResponse"
"github.com/gin-gonic/gin"
"strings"
"time"
)
func Call(ctx *gin.Context, client RpcClientInterface, method string, req, rsp interface{}) (err error) {
pkg, app, service := client.RpcClientName()
var host, uri string
var timeout, try int64
if clientContainer.HostFunc != nil {
host, uri = clientContainer.HostFunc(ctx, app, service)
}
if host == "" {
host = clientContainer.GetHost(pkg, app, service)
}
if uri == "" {
if selfRpcConf.RequestURI != "" {
uri = selfRpcConf.RequestURI
} else {
uri = "/rpc/call"
}
}
if selfRpcConf.Timeout <= 0 {
timeout = 60
} else {
timeout = selfRpcConf.Timeout
}
if selfRpcConf.Try <= 0 {
try = 3
} else {
try = selfRpcConf.Try
}
data, err := json.Marshal(req)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call:json.Marshal",
"req": req,
"err": err.Error(),
}).Error("rpc 请求参数格式化异常")
return
}
var traceid, sign string
traceid = cContext.GetTraceId(ctx)
if clientContainer.EncryptFunc != nil {
sign = clientContainer.EncryptFunc(ctx)
} else if clientContainer.Signature != "" {
sign = cHelper.Md5([]byte(fmt.Sprintf("%s:%s", clientContainer.Signature, traceid)))
}
response := &cHTTPClient.Response{}
option := cHTTPClient.Option{
Host: host,
Uri: uri,
Method: cHTTPClient.MethodPOST,
Timeout: time.Duration(timeout) * time.Second,
Try: int(try),
Headers: map[string]string{
"Content-Type": "application/json",
cCommon.XIndex_TraceId: traceid,
cCommon.Header_Rpc_App: app,
cCommon.Header_Rpc_Service: service,
cCommon.Header_Rpc_Method: method,
cCommon.Header_Rpc_Sign: sign,
},
Data: string(data),
}
response, err = cHTTPClient.Request(ctx, option)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call:cHTTPClient.Request",
"body": string(response.Body),
"err": err.Error(),
}).Error("rpc 请求结果获取异常")
return
}
responseBody := &cResponse.ApiData{}
err = json.Unmarshal(response.Body, &responseBody)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call: json.Unmarshal",
"err": err.Error(),
}).Error("rpc 请求结果 json 解析异常")
return
}
if responseBody.Code != cError.CError_Csingo_Nil.Code() {
err = &cError.BaseError{
ErrCode: responseBody.Code,
ErrMsg: strings.TrimLeft(responseBody.Msg, fmt.Sprintf("%s: ", cError.CError_Csingo_System.Error())),
}
} else {
var rspBytes []byte
rspBytes, err = json.Marshal(responseBody.Data)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call: json.Marshal",
"err": err.Error(),
}).Error("rpc 请求结果 data 字段 json 格式化异常")
return
}
err = json.Unmarshal(rspBytes, rsp)
if err != nil {
cLog.WithContext(ctx, map[string]interface{}{
"source": "cRpc.Call: json.Unmarshal",
"err": err.Error(),
}).Error("rpc 请求结果 data 字段 json 解析异常")
return
}
}
return
}
func SetHostFunc(hostFunc ParseHostFunc) {
clientContainer.SetHostFunc(hostFunc)
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.2.26

搜索帮助