Documentation
¶
Overview ¶
Package rpc 提供标准化的 RPC 调用封装与响应处理工具。
Index ¶
- Constants
- Variables
- func NewRemoteInvokeServiceContext(bootstrapConf conf.BootstrapConf) context.Context
- func NewRemoteServiceGrpcClient(bootstrapConf conf.BootstrapConf) (*grpc.ClientConn, error)
- func ParseMetaKey(md metadata.MD, key string) (string, error)
- func SetRemoteInvokeServiceAfterContext(ctx context.Context, appId, endpoint string) context.Context
- func SetRemoteInvokeServiceBeforeContext(ctx context.Context, bootstrapConf conf.BootstrapConf) context.Context
- func WithRemoteInvoke[T any, R RemoteResponse[T]](callFunc func() (R, error)) (T, error)
- type ClientContextMeta
- type RemoteResponse
- type UserContextMeta
Constants ¶
View Source
const MetaKeyParseErrorFormat = "%s 解析失败"
MetaKeyParseErrorFormat 用于构造元信息缺失/解析失败的错误文本。
Variables ¶
View Source
var ( // ErrRemoteResponseIsNil 表示远程调用返回的响应对象为空。 ErrRemoteResponseIsNil = errors.New("远程响应为空") // ErrRemoteCallFailed 表示远程调用失败但未返回可读错误信息。 ErrRemoteCallFailed = errors.New("远程调用失败") )
Functions ¶
func NewRemoteInvokeServiceContext ¶ added in v0.9.7
func NewRemoteInvokeServiceContext(bootstrapConf conf.BootstrapConf) context.Context
NewRemoteInvokeServiceContext 初始化远程调用服务上下文, 适用于服务自身(无其他用户特征)调用其他服务时,可基于此上下文进行设置上下文
func NewRemoteServiceGrpcClient ¶ added in v0.9.8
func NewRemoteServiceGrpcClient(bootstrapConf conf.BootstrapConf) (*grpc.ClientConn, error)
func ParseMetaKey ¶
ParseMetaKey 解析元信息 key。
func SetRemoteInvokeServiceAfterContext ¶ added in v0.7.9
func SetRemoteInvokeServiceAfterContext(ctx context.Context, appId, endpoint string) context.Context
SetRemoteInvokeServiceAfterContext 设置远程调用后置上下文,一般是grpc-gateway使用的
func SetRemoteInvokeServiceBeforeContext ¶ added in v0.7.9
func SetRemoteInvokeServiceBeforeContext(ctx context.Context, bootstrapConf conf.BootstrapConf) context.Context
SetRemoteInvokeServiceBeforeContext 设置远程调用前置上下文,基于当前上下文进行设置服务自身的信息,并设置链路追踪,一般是将其封装成中间件使用
func WithRemoteInvoke ¶
func WithRemoteInvoke[T any, R RemoteResponse[T]](callFunc func() (R, error)) (T, error)
WithRemoteInvoke 执行远程调用并处理标准化响应。 T: 业务数据类型 R: 响应类型,必须实现 RemoteResponse[T] 接口
Types ¶
type ClientContextMeta ¶
type ClientContextMeta struct {
ClientIp string `json:"client_ip"`
AppVersion string `json:"app_version"`
AppLanguage string `json:"app_language"`
}
ClientContextMeta 表示客户端上下文元信息。
func ParseClientContextMeta ¶
func ParseClientContextMeta(md metadata.MD) (raw *ClientContextMeta, err error)
ParseClientContextMeta 解析客户端上下文元信息。
type RemoteResponse ¶
type RemoteResponse[T any] interface { GetCode() uint32 // 获取状态码 GetMessage() string // 获取消息文本 GetData() T // 获取业务数据 }
RemoteResponse 定义远程调用响应的标准接口。
type UserContextMeta ¶
type UserContextMeta struct {
Session string `json:"session"`
ClientIp string `json:"client_ip"`
UserId string `json:"user_id"`
AppId string `json:"app_id"`
TenantId string `json:"tenant_id"`
RoleIds []string `json:"role_ids"`
OrgIds []string `json:"org_ids"`
}
UserContextMeta 表示用户上下文元信息。
func ParseUserContextMeta ¶
func ParseUserContextMeta(md metadata.MD) (raw *UserContextMeta, err error)
ParseUserContextMeta 解析用户上下文元信息。
Click to show internal directories.
Click to hide internal directories.