invocation

package
v0.0.5 Latest Latest
Warning

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

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

README

Invocation

go-consul/invocation 提供基于 Consul 的裸机服务调用实现。

它的目标是对齐 go-micro/invocation 的统一调用模型,而不是继续把业务调用暴露成节点发现问题。

包定位

go-consul/invocation 适合:

  • 使用 Consul 维护服务健康信息的 IDC 环境
  • 需要与 go-consul/agent -> sidecar-agent -> consul / envoy 主链配合的裸机场景
  • 但对外希望统一成 service -> service 调用模型的场景

当前提供的能力

Config

Config 用于描述 Consul invocation 的公共配置,例如:

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

Locator 负责把 ServiceRef 解析成 Target

支持两类模式:

1. Service DNS 模式

若已经有稳定服务名,则优先直接返回 service 级 DNS target。

2. Endpoint 模式

从 Consul 健康实例列表读取实例信息,在本地做轻量 endpoint 选择。

这样可以把:

  • Consul 内部的 health check
  • service meta
  • blocking query / watch

继续保留在实现层,同时对外维持统一的调用体验。

NewConnectionManager

该辅助函数负责把:

  • Consul Locator
  • go-micro/invocation.ConnectionManager

拼装成统一连接管理入口。

设计约束

  • Consul 只作为实现后端
  • 裸机注册主链由 go-consul/agent 承接,go-consul/invocation 只负责调用侧
  • 不向业务侧泄漏实例列表与节点模型
  • 业务侧始终面向 ServiceRef
  • OTel 观测链路默认继续依赖 go-micro/invocation.ConnectionManager

当前进度

当前已经完成:

  • Config
  • Locator
  • NewConnectionManager
  • 单元测试

当前尚未做的内容:

  • 更复杂的健康权重策略
  • 更复杂的 endpoint 选择策略
  • 更深的 Authz 接入封装

测试

当前包已通过:

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

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func NewConnectionManager

func NewConnectionManager(client *api.Client, conf *Config, options microInvocation.ConnectionManagerOptions) (*microInvocation.ConnectionManager, error)

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

Types

type Config added in v0.0.5

type Config struct {
	// Namespace 是默认命名空间。
	Namespace string `json:"namespace"`
	// TargetOptions 表示通用 target 构造选项。
	TargetOptions microInvocation.TargetOptions `json:"target_options"`
	// PreferServiceDNS 控制是否优先走 service 级 DNS 目标。
	PreferServiceDNS bool `json:"prefer_service_dns"`
	// CacheTTL 控制健康实例列表缓存的有效期。
	CacheTTL time.Duration `json:"cache_ttl"`
}

Config 定义 Consul invocation 轻量实现配置。

func (*Config) Bootstrap added in v0.0.5

func (c *Config) Bootstrap()

Bootstrap 补齐默认值。

type Locator

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

Locator 是基于 Consul 的 invocation 定位器。

它与 etcd 轻量实现保持一致: - 若已具备稳定 service DNS,则优先返回 DNS target; - 否则读取 Consul 健康实例列表,在本地做轻量 endpoint 选择。

func NewLocator

func NewLocator(client *api.Client, config *Config) (*Locator, error)

NewLocator 创建 Consul 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