service

package
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 5 Imported by: 0

README

Service

service 包定义服务内统一主上下文模型,以及业务服务级别的标准 DNS 模型。

它只负责:

  • 定义 service.Context
  • 定义 service.DNS
  • 提供 WithContext(...) / FromContext(...) / MustFromContext(...)
  • 提供 BuildContext(...) 把入站 metadata 与当前 OTel span 结构化为服务内主上下文

其中:

  • service.DNS 只表达远程业务服务的标准 DNS 结构
  • service.DNSinvocation 侧的 DNSManagerRemoteServiceCallerRemoteServiceManaged 统一消费

它不负责:

  • gRPC interceptor 装配
  • 出站 metadata 拼装
  • 下游服务调用

当前推荐分工:

  • middleware/grpc:在 gRPC 服务端入口创建并注入 service.Context
  • service:供 service / biz / data / log 统一读取服务内主上下文
  • invocation:负责纯出站调用语义

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, value *Context) context.Context

WithContext 将服务主上下文注入到 ctx。

Types

type BuildContextOptions

type BuildContextOptions struct {
	ServiceAppId      string
	ServiceInstanceId string
}

BuildContextOptions 定义构建服务主上下文时需要补齐的服务自身身份。

type Context

type Context struct {
	UserId   string
	AppId    string
	TenantId string
	OrgIds   []string
	RoleIds  []string
	// TraceId 表示从当前 OTel span 提取的 trace 标识快照,不对应自定义 header。
	TraceId           string
	RouteMethod       string
	AccessMethod      string
	ServiceAppId      string
	ServiceInstanceId string
}

Context 表示当前请求在服务内部流转时的统一主上下文。

func BuildContext

func BuildContext(ctx context.Context, options BuildContextOptions) *Context

BuildContext 从入站 metadata 与运行时信息构造服务主上下文。

它只负责把服务端入口已经拿到的 metadata 与 OTel span 信息结构化, 不负责缓存 transport 原文,也不参与出站调用语义。

func FromContext

func FromContext(ctx context.Context) (*Context, bool)

FromContext 从 ctx 读取服务主上下文。

func MustFromContext

func MustFromContext(ctx context.Context) *Context

MustFromContext 从 ctx 读取服务主上下文,不存在时 panic。

type DNS added in v1.4.1

type DNS struct {
	// Service 表示业务服务名,例如 auth。
	Service string `json:"service"`
	// Namespace 表示命名空间,例如 default。
	Namespace string `json:"namespace"`
	// ServiceType 表示服务类型片段,默认值通常为 svc。
	ServiceType string `json:"service_type"`
	// ClusterDomain 表示集群域,默认值通常为 cluster.local。
	ClusterDomain string `json:"cluster_domain"`
	// Port 表示业务服务监听端口,默认值通常为 9090。
	Port uint16 `json:"port"`
}

DNS 表示一个业务服务的标准 DNS 配置。

这里表达的是“这个业务服务在网络上的稳定入口”, 而不是“这个服务当前有哪些实例”。

func (DNS) WithPort added in v1.4.1

func (d DNS) WithPort(port uint16) DNS

WithPort 返回带显式端口的新 DNS。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL