registry

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 6 Imported by: 0

README

Registry

registry 包定义了服务注册与发现的核心接口与通用模型,供各类注册中心实现复用。

核心接口

  • Register:定义服务注册行为(InstallUninstall)。
  • Discovery:定义服务发现行为(GetServiceWatchUnwatch),仅网关使用。

设计约束

  • 业务服务只依赖 Register,不依赖 Discovery
  • registry 只保留接口与模型,不承载具体适配实现
  • etcd、consul、k8s/istio 的实现完全独立维护,互不耦合

分层建议

  • go-micro/registry 保留接口和跨实现共享模型
  • go-etcd/registrygo-consul/registrygo-k8s/registry 各自维护实现细节与实现专属模型
  • 不把某个注册中心专属字段上提到 go-micro/registry
  • ServiceConfGatewayConf 等实现配置模型下沉到各实现包维护

推荐按下面边界维护:

位置 放什么
go-micro/registry Register/Discovery 接口,Meta/Network/Kernel/ServiceNode 等通用模型
go-etcd/registry lease、revision、watch 重连、etcd key 组织等
go-consul/registry check、service meta 编码、blocking query、consul 事件模型等
go-k8s/registry EndpointSlice/Service 查询、K8s 资源监听、Istio 路由映射等

不建议把 go-micro/registry 完全下沉到实现包,原因如下:

  • 会丢失统一契约,业务层会被迫直接依赖某个实现包
  • 会出现重复模型,ServiceNode 等对象在多个包中无法直接复用
  • 会提升迁移成本,从 etcd 切到 consul/k8s 时业务代码改动面会扩大

建议维持“核心契约 + 实现扩展”:

  • 核心契约放 go-micro/registry
  • 实现专属字段放 go-etcd/go-consul/go-k8s
  • 通过实现包内部转换,避免把实现细节泄漏给业务层

实现包

注册中心的具体实现位于独立仓库中。

  • 基于 etcd v3 的注册与发现实现: github.com/fireflycore/go-etcd/registry;
  • 基于 Consul 的注册与网关发现实现: github.com/fireflycore/go-consul/registry;
  • 基于 K8s/Istio 的注册与网关发现实现: github.com/fireflycore/go-k8s/registry;

迁移设计文档

  • Consul 迁移说明:consul/README.md
  • K8s + Istio 迁移说明:k8s/istio/README.md

Documentation

Overview

Package registry 定义服务注册与发现的核心接口与通用模型。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClientIsNil 表示客户端为空
	ErrClientIsNilFormat = "%s client is nil"

	// ErrRegisterIsNil 表示注册器为空。
	ErrRegisterIsNil = errors.New("注册器为空")
	// ErrServiceNodeNotExists 表示服务节点不存在。
	ErrServiceNodeNotExists = errors.New("服务节点不存在")
	// ErrServiceMethodNotExists 表示服务方法不存在。
	ErrServiceMethodNotExists = errors.New("服务方法不存在")
	// ErrServiceNodeMethodNotExists 表示服务节点不包含指定方法。
	ErrServiceNodeMethodNotExists = errors.New("服务节点不包含该方法")

	// ErrServiceConfIsNil 表示服务配置为空。
	ErrServiceConfIsNil = errors.New("service conf is nil")
	// ErrServiceMetaIsNil 标识服务元数据为空。
	ErrServiceMetaIsNil = errors.New("service meta is nil")
	// ErrServiceNodeIsNil 表示服务节点对象为空。
	ErrServiceNodeIsNil = errors.New("service node is nil")
)

Functions

func NewRegisterService

func NewRegisterService(raw []*grpc.ServiceDesc, reg Register) []error

NewRegisterService 将 gRPC ServiceDesc 解析为节点方法集合并执行注册。

Types

type Discovery

type Discovery interface {
	// GetService 根据 rpc method (如 /user.UserService/Login) 返回可用节点列表和对应的 AppId。
	// 网关在接收到请求时,只有 method 信息,需要通过此方法路由到具体的微服务实例。
	GetService(method string) ([]*ServiceNode, string, error)

	// Watch 启动全量监听,返回变动事件的通道,通过 ctx 控制生命周期。
	// 监听实现需要维护内部的 Method -> AppId 以及 AppId -> ServiceNodes 映射。
	Watch(ctx context.Context) (<-chan ServiceEvent, error)
	// Unwatch 停止监听并释放相关资源。
	Unwatch()
}

Discovery 定义服务发现实现的最小能力集合 (主要供网关使用)。

type EventType added in v1.2.4

type EventType int

EventType 服务变动事件类型

const (
	EventAdd EventType = iota
	EventUpdate
	EventDelete
)

type GatewayConf

type GatewayConf struct {
	// 网卡
	Network *Network `json:"network"`
}

GatewayConf 定义网关相关配置。

func (*GatewayConf) Bootstrap

func (gc *GatewayConf) Bootstrap()

type Kernel

type Kernel struct {
	// 所使用的开发语言
	Language string `json:"language"`
	// 内核版本
	Version string `json:"version"`
}

Kernel 定义服务实例运行时元信息。

func (*Kernel) Bootstrap

func (k *Kernel) Bootstrap()

type Meta

type Meta struct {
	// 环境,不同环境的服务不互通
	Env string `json:"env"`
	// 应用id,泛指服务实例,不同版本的服务实例可以共享appId
	AppId string `json:"app_id"`
	// 服务实例版本
	Version string `json:"version"`
}

Meta 服务元信息。

type Network

type Network struct {
	// 网卡唯一标识,用于grpc-gateway流量控制,同sn流量优先
	SN string `json:"sn"`
	// 内网地址
	Internal string `json:"internal"`
	// 外网地址
	External string `json:"external"`
}

Network 定义服务节点上报的网络信息。

func (*Network) Bootstrap

func (n *Network) Bootstrap()

type Register

type Register interface {
	// Install 安装并注册一个服务节点,完成必要的元信息填充与持久化。
	Install(service *ServiceNode) error
	// Uninstall 注销当前注册的服务节点并释放相关资源。
	Uninstall() error
}

Register 定义服务注册器的最小能力集合。

type ServiceConf

type ServiceConf struct {
	// 实例Id
	InstanceId string `json:"instance_id"`
	// 命名空间
	Namespace string `json:"namespace"`
	// 网卡
	Network *Network `json:"network"`
	// 内核
	Kernel *Kernel `json:"kernel"`

	// 最大重试次数, 间隔时间是TTL*5
	MaxRetry uint32 `json:"max_retry"`
	// 心跳/租约 TTL(秒), 最少是10s
	TTL uint32 `json:"ttl"`
	// 权重
	Weight int `json:"weight"`
}

ServiceConf 服务注册/服务发现配置。

func (*ServiceConf) Bootstrap

func (sc *ServiceConf) Bootstrap()

Bootstrap 补齐 namespace/ttl/maxRetry/network/kernel 等默认值,避免下游逻辑出现零值陷阱

type ServiceDiscover

type ServiceDiscover map[string][]*ServiceNode

ServiceDiscover 服务发现数据结构(appId -> nodes)本地缓存。

func (ServiceDiscover) GetNodes

func (s ServiceDiscover) GetNodes(appId string) ([]*ServiceNode, error)

GetNodes 获取指定 appId 下的所有服务节点。

type ServiceEvent added in v1.2.4

type ServiceEvent struct {
	Type    EventType
	Service *ServiceNode
}

ServiceEvent 服务变动事件

type ServiceMethod

type ServiceMethod map[string]string

ServiceMethod 服务方法映射(method -> appId)。

func (ServiceMethod) GetAppId

func (s ServiceMethod) GetAppId(sm string) (string, error)

GetAppId 根据 gRPC 方法名返回归属的 appId。

type ServiceNode

type ServiceNode struct {
	ProtoCount int             `json:"proto_count"`
	Weight     int             `json:"weight"` // 权重,默认100
	RunDate    string          `json:"run_date"`
	Methods    map[string]bool `json:"methods"`

	Network *Network `json:"network"`
	Kernel  *Kernel  `json:"kernel"`
	Meta    *Meta    `json:"meta"`
}

ServiceNode 适用于服务注册/发现的节点描述。

func (*ServiceNode) CheckMethod

func (ist *ServiceNode) CheckMethod(sm string) error

CheckMethod 检查节点是否包含指定方法。

func (*ServiceNode) ParseMethod

func (ist *ServiceNode) ParseMethod(s ServiceMethod)

ParseMethod 将节点方法映射写入方法表(method -> appId)。

Jump to

Keyboard shortcuts

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