-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
572 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
package application | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
routing "github.com/gly-hub/fasthttp-routing" | ||
"github.com/gly-hub/toolbox/ip" | ||
Check failure on line 6 in application/rpcserver.go GitHub Actions / Windows
|
||
"github.com/gly-hub/toolbox/stringx" | ||
jsoniter "github.com/json-iterator/go" | ||
"github.com/smallnest/rpcx/server" | ||
"github.com/team-dandelion/go-dandelion/config" | ||
error_support "github.com/team-dandelion/go-dandelion/error-support" | ||
"github.com/team-dandelion/go-dandelion/logger" | ||
"github.com/team-dandelion/go-dandelion/server/http" | ||
"github.com/team-dandelion/go-dandelion/server/rpcx" | ||
"github.com/team-dandelion/go-dandelion/telemetry" | ||
"reflect" | ||
) | ||
|
||
var ( | ||
|
@@ -25,8 +18,8 @@ var ( | |
|
||
type RpcClient struct { | ||
ClientName string | ||
clientPool *rpcx.ClientPool | ||
headerFunc func(ctx *routing.Context, header map[string]string) map[string]string | ||
ClientPool *rpcx.ClientPool | ||
HeaderFunc func(ctx *routing.Context, header map[string]string) map[string]string | ||
} | ||
|
||
func initRpcClient() { | ||
|
@@ -47,97 +40,12 @@ func initRpcClient() { | |
} | ||
rpcClient = &RpcClient{ | ||
ClientName: config.Conf.RpcClient.ClientName, | ||
clientPool: client, | ||
ClientPool: client, | ||
} | ||
} | ||
|
||
func RegisterHeaderFunc(f func(ctx *routing.Context, header map[string]string) map[string]string) { | ||
rpcClient.headerFunc = f | ||
} | ||
|
||
// RpcCall rpc请求 | ||
func RpcCall(ctx *routing.Context, serverName, funcName string, args interface{}, reply interface{}) error { | ||
if rpcClient.clientPool == nil { | ||
panic("请配置rpcx参数") | ||
} | ||
content, _ := jsoniter.MarshalToString(args) | ||
var traceId string | ||
if telemetry.GetSpanTraceId() != nil { | ||
traceId = telemetry.GetSpanTraceId().(string) | ||
} | ||
requestHeader := map[string]string{ | ||
"request_id": stringx.Strval(logger.GetRequestId()), | ||
"span_trace_id": traceId, | ||
"client_name": rpcClient.ClientName, | ||
"content": content, | ||
} | ||
|
||
requestHeader = rpcClient.headerFunc(ctx, requestHeader) | ||
c := rpcx.Header().Set(context.Background(), requestHeader) | ||
err := rpcClient.clientPool.Client().Call(c, serverName, funcName, args, reply) | ||
if err != nil { | ||
logger.Error("ServerName: ", serverName, ", FuncName: ", funcName, ", Err: ", err) | ||
return &error_support.Error{Code: 5001, Msg: "服务器异常"} | ||
} | ||
|
||
rv := reflect.ValueOf(reply) | ||
if rv.Kind() == reflect.Ptr { | ||
rv = rv.Elem() | ||
} | ||
if rv.FieldByName("Code").Int() != int64(0) { | ||
return &error_support.Error{Code: int(rv.FieldByName("Code").Int()), Msg: rv.FieldByName("Msg").String()} | ||
} | ||
return nil | ||
} | ||
|
||
// SRpcCall rpc请求拓展 | ||
func SRpcCall(ctx *routing.Context, serverName, funcName string, args interface{}, reply interface{}) error { | ||
if rpcClient.clientPool == nil { | ||
panic("请配置rpcx参数") | ||
} | ||
var hc http.HttpController | ||
if err := hc.ReadJson(ctx, args); err != nil { | ||
return hc.Fail(ctx, &error_support.Error{Code: 5000, Msg: "数据解析失败"}) | ||
} | ||
|
||
content, _ := jsoniter.MarshalToString(args) | ||
var traceId string | ||
if telemetry.GetSpanTraceId() != nil { | ||
traceId = telemetry.GetSpanTraceId().(string) | ||
} | ||
requestHeader := map[string]string{ | ||
"request_id": stringx.Strval(logger.GetRequestId()), | ||
"span_trace_id": traceId, | ||
"client_name": rpcClient.ClientName, | ||
"content": content, | ||
} | ||
requestHeader = rpcClient.headerFunc(ctx, requestHeader) | ||
c := rpcx.Header().Set(context.Background(), requestHeader) | ||
err := rpcClient.clientPool.Client().Call(c, serverName, funcName, args, reply) | ||
if err != nil { | ||
logger.Error("ServerName: ", serverName, ", FuncName: ", funcName, ", Err: ", err) | ||
return hc.Fail(ctx, &error_support.Error{Code: 5001, Msg: "服务器异常"}) | ||
} | ||
|
||
rt := reflect.TypeOf(reply) | ||
if rt.Kind() == reflect.Ptr { | ||
rt = rt.Elem() | ||
} | ||
_, cOk := rt.FieldByName("Code") | ||
_, mOk := rt.FieldByName("Msg") | ||
if mOk && cOk { | ||
rv := reflect.ValueOf(reply) | ||
if rv.Kind() == reflect.Ptr { | ||
rv = rv.Elem() | ||
} | ||
|
||
if rv.FieldByName("Code").Int() != int64(0) { | ||
return hc.Fail(ctx, &error_support.Error{Code: int(rv.FieldByName("Code").Int()), Msg: rv.FieldByName("Msg").String()}) | ||
} | ||
return hc.Success(ctx, reply, rv.FieldByName("Msg").String()) | ||
} | ||
|
||
return hc.Success(ctx, reply, "") | ||
rpcClient.HeaderFunc = f | ||
} | ||
|
||
func RegisterRpcPlugin(plugins ...server.Plugin) { | ||
|
@@ -174,3 +82,7 @@ func RpcServer(handler interface{}, auth ...rpcx.AuthFunc) { | |
} | ||
rpcServer.Start() | ||
} | ||
|
||
func GetRpcClient() *RpcClient { | ||
return rpcClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
proto: | ||
protoc -I. -I${GOPATH}/src -I ../../ -I ./ \ | ||
--gofast_out=. --gofast_opt=paths=source_relative \ | ||
--rpcx_out=. --rpcx_opt=paths=source_relative *.proto |
Oops, something went wrong.