From 768f1ce0324e72d7fd985ddf2c692423aec6e989 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 10 Mar 2023 20:04:53 +0800 Subject: [PATCH 1/3] test --- internal/api/a2r/api2rpc.go | 35 +++++++++++++++++++++++++++++++++++ internal/api/auth.go | 3 ++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/api/a2r/api2rpc.go b/internal/api/a2r/api2rpc.go index d370b23f6..98d34e8ea 100644 --- a/internal/api/a2r/api2rpc.go +++ b/internal/api/a2r/api2rpc.go @@ -39,3 +39,38 @@ func Call[A, B, C any]( } apiresp.GinSuccess(c, data) // 成功 } +func Call2[A, B, C any]( + _ func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), + client func() (C, error), + c *gin.Context, +) { + var req A + if err := c.BindJSON(&req); err != nil { + apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数错误 + return + } + if check, ok := any(&req).(interface{ Check() error }); ok { + if err := check.Check(); err != nil { + apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数校验失败 + return + } + } + cli, err := client() + if err != nil { + apiresp.GinError(c, errs.ErrInternalServer.Wrap(err.Error())) // 获取RPC连接失败 + log.Error("0", "get rpc client conn err:", err.Error()) + return + } + if a, ok := any(&cli).(interface { + rpc(ctx context.Context, req *A, options ...grpc.CallOption) (*B, error) + }); ok { + data, err := a.rpc(c, &req) + if err != nil { + log.Error("0", "rpc call err:", err.Error()) + apiresp.GinError(c, err) // RPC调用失败 + return + } + apiresp.GinSuccess(c, data) // 成功 + } + +} diff --git a/internal/api/auth.go b/internal/api/auth.go index e997ba6aa..6f61a310b 100644 --- a/internal/api/auth.go +++ b/internal/api/auth.go @@ -32,7 +32,8 @@ func (o *Auth) UserRegister(c *gin.Context) { } func (o *Auth) UserToken(c *gin.Context) { - a2r.Call(auth.AuthClient.UserToken, o.client, c) + + a2r.Call2(auth.AuthClient.UserToken, o.client, c) } func (o *Auth) ParseToken(c *gin.Context) { From ef0b9a9ebe87894a376c2c7d269f04369968de0d Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 10 Mar 2023 20:16:27 +0800 Subject: [PATCH 2/3] test --- internal/api/a2r/api2rpc.go | 35 ----------------------------------- internal/api/auth.go | 3 +-- pkg/common/mw/rpc.go | 3 +++ 3 files changed, 4 insertions(+), 37 deletions(-) diff --git a/internal/api/a2r/api2rpc.go b/internal/api/a2r/api2rpc.go index 98d34e8ea..d370b23f6 100644 --- a/internal/api/a2r/api2rpc.go +++ b/internal/api/a2r/api2rpc.go @@ -39,38 +39,3 @@ func Call[A, B, C any]( } apiresp.GinSuccess(c, data) // 成功 } -func Call2[A, B, C any]( - _ func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), - client func() (C, error), - c *gin.Context, -) { - var req A - if err := c.BindJSON(&req); err != nil { - apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数错误 - return - } - if check, ok := any(&req).(interface{ Check() error }); ok { - if err := check.Check(); err != nil { - apiresp.GinError(c, errs.ErrArgs.Wrap(err.Error())) // 参数校验失败 - return - } - } - cli, err := client() - if err != nil { - apiresp.GinError(c, errs.ErrInternalServer.Wrap(err.Error())) // 获取RPC连接失败 - log.Error("0", "get rpc client conn err:", err.Error()) - return - } - if a, ok := any(&cli).(interface { - rpc(ctx context.Context, req *A, options ...grpc.CallOption) (*B, error) - }); ok { - data, err := a.rpc(c, &req) - if err != nil { - log.Error("0", "rpc call err:", err.Error()) - apiresp.GinError(c, err) // RPC调用失败 - return - } - apiresp.GinSuccess(c, data) // 成功 - } - -} diff --git a/internal/api/auth.go b/internal/api/auth.go index 6f61a310b..e997ba6aa 100644 --- a/internal/api/auth.go +++ b/internal/api/auth.go @@ -32,8 +32,7 @@ func (o *Auth) UserRegister(c *gin.Context) { } func (o *Auth) UserToken(c *gin.Context) { - - a2r.Call2(auth.AuthClient.UserToken, o.client, c) + a2r.Call(auth.AuthClient.UserToken, o.client, c) } func (o *Auth) ParseToken(c *gin.Context) { diff --git a/pkg/common/mw/rpc.go b/pkg/common/mw/rpc.go index f9004b648..a1d75233a 100644 --- a/pkg/common/mw/rpc.go +++ b/pkg/common/mw/rpc.go @@ -64,10 +64,13 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa if ok { md.Append(constant.OpUserID, opUserID) } + log.Info("", "rpc come here") err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...) if err == nil { return nil } + log.Info("", "rpc come here err") + rpcErr, ok := err.(interface{ GRPCStatus() *status.Status }) if !ok { return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap() From b61ab1201ae4aeb649b49b93df0fac9753010377 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 10 Mar 2023 20:33:37 +0800 Subject: [PATCH 3/3] test --- internal/startrpc/start.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/startrpc/start.go b/internal/startrpc/start.go index 18fe7cef4..17afcc7a9 100644 --- a/internal/startrpc/start.go +++ b/internal/startrpc/start.go @@ -59,11 +59,16 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c } } }() + + err = rpcFn(zkClient, srv) + if err != nil { + return utils.Wrap1(err) + } err = srv.Serve(listener) if err != nil { return utils.Wrap1(err) } - return rpcFn(zkClient, srv) + return nil } //func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, options ...grpc.ServerOption) error {