invocation

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 12 Imported by: 0

README

Invocation

go-etcd/invocation 提供基于 etcd 的轻量服务调用实现。

它的目标不是继续暴露旧的节点发现模型,而是对齐 go-micro/invocation 的统一调用语义:

  • 业务侧面向 ServiceRef
  • 调用侧面向 Target
  • 连接复用交给 ConnectionManager
  • 底层如何从 etcd 解析服务地址,由 Locator 内部完成

包定位

go-etcd/invocation 是 etcd 路径下的轻量实现,适合:

  • IDC 环境
  • 尚未上 K8s + Istio 的场景
  • 需要通过 etcd 维护服务实例信息,但对外仍保持 service -> service 调用体验

当前提供的能力

Conf

Conf 用于描述 etcd invocation 的公共配置,例如:

  • Namespace
  • DefaultPort
  • ClusterDomain
  • ResolverScheme
  • PreferServiceDNS
  • CacheTTL

其中:

  • PreferServiceDNS=true 时,更偏向“类 service mesh”模式
  • PreferServiceDNS=false 时,会从 etcd 中读取实例列表,在本地做轻量 endpoint 选择
Locator

Locator 是核心实现,负责把 ServiceRef 解析成最终 Target

支持两种模式:

1. Service DNS 模式

直接返回类似:

dns:///auth.default.svc.cluster.local:9000

适合:

  • 已通过 CoreDNS 或其他机制提供稳定服务名
  • 希望 etcd 路径尽量对齐 K8s + Istio 的调用体验
2. Endpoint 模式

从 etcd 中读取实例注册信息,解析出 ServiceEndpoint 列表,并在本地做轻量选择。

适合:

  • 还没有稳定 service DNS
  • 仍需要 etcd 直接提供底层实例地址
NewConnectionManager

该辅助函数会把:

  • etcd Locator
  • go-micro/invocation.ConnectionManager

组合起来,让上层更容易直接接入统一调用模型。

设计说明

  • etcd 只作为实现后端
  • lease / watch / key 结构等细节不暴露给业务侧
  • 对外语义始终与 go-micro/invocation 对齐
  • OTel 观测链路默认继续依赖 go-micro/invocation.ConnectionManager

当前进度

当前已经完成:

  • Conf
  • Locator
  • NewConnectionManager
  • 单元测试

当前尚未做的内容:

  • 单独的 etcd invocation README 示例代码扩展
  • 更复杂的 endpoint 选择策略
  • 更深的 Authz 接入封装

测试

当前包已通过:

  • go test ./...
  • go vet ./...

Documentation

Index

Constants

View Source
const (
	// DefaultNamespace 是 etcd 轻量实现下的默认命名空间。
	DefaultNamespace = "default"
	// DefaultCacheTTL 是本地 endpoint 缓存的默认有效期。
	DefaultCacheTTL = 5 * time.Second
)

Variables

This section is empty.

Functions

func NewConnectionManager

func NewConnectionManager(client *clientv3.Client, conf *Conf, options microInvocation.ConnectionManagerOptions) (*microInvocation.ConnectionManager, error)

NewConnectionManager 创建基于 etcd Locator 的连接管理器。

该辅助函数的目标是减少调用方样板代码,让业务侧更容易直接接入: - etcd 轻量服务定位 - go-micro/invocation 的统一连接管理

Types

type Conf

type Conf struct {
	// Namespace 是默认命名空间。
	// 当 ServiceRef 未显式提供 namespace 时,可回退到该值。
	Namespace string `json:"namespace"`
	// DefaultPort 是默认 gRPC 端口。
	DefaultPort uint16 `json:"default_port"`
	// ClusterDomain 是 service DNS 所使用的集群域。
	ClusterDomain string `json:"cluster_domain"`
	// ResolverScheme 是最终生成 gRPC target 时使用的 resolver scheme。
	ResolverScheme string `json:"resolver_scheme"`
	// PreferServiceDNS 表示优先直接返回 service 级 DNS 目标。
	//
	// 适用场景:
	// - 已通过 CoreDNS 或其他机制提供稳定服务名;
	// - 期望让 etcd 轻量实现更接近 K8s + Istio 的调用体验。
	//
	// 若为 false,则 Locator 会从 etcd 中读取实例列表,并在本地做轻量 endpoint 选择。
	PreferServiceDNS bool `json:"prefer_service_dns"`
	// CacheTTL 表示 endpoint 列表缓存有效期。
	CacheTTL time.Duration `json:"cache_ttl"`
}

Conf 定义 etcd invocation 轻量实现的配置。

该配置不再表达旧 registry 模型中的节点注册语义, 而是围绕“如何把 ServiceRef 解析成 Target”组织。

func (*Conf) Bootstrap

func (c *Conf) Bootstrap()

Bootstrap 补齐配置默认值。

type Locator

type Locator struct {
	// contains filtered or unexported fields
}

Locator 是基于 etcd 的 invocation 定位器。

它支持两种模式: 1. PreferServiceDNS=true:直接返回 service 级 DNS 目标; 2. PreferServiceDNS=false:读取 etcd 中已注册的实例列表,在本地做轻量 endpoint 选择。

这样既能兼容“类 service mesh”的统一调用语义, 也能在没有稳定 DNS 的场景下直接工作。

func NewLocator

func NewLocator(client *clientv3.Client, conf *Conf) (*Locator, error)

NewLocator 创建 etcd invocation 定位器。

func (*Locator) Resolve

Resolve 根据 ServiceRef 解析最终 Target。

Jump to

Keyboard shortcuts

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