registry

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 6 Imported by: 0

README

Registry

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

核心接口

  • Register:定义服务注册行为(InstallUninstall)。
  • Discovery:定义服务发现行为(GetServiceWatcher)。

实现包

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

  • 基于 etcd v3 的注册与发现实现: github.com/fireflycore/go-etcd/registry;

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClientIsNil 表示客户端为空
	ErrClientIsNil = "%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 根据方法名返回可用节点列表和AppId。
	GetService(name string) ([]*ServiceNode, string, error)
	// Watcher 启动监听并持续刷新本地缓存。
	Watcher()
	// Unwatch 停止监听并释放相关资源。
	Unwatch()
}

Discovery 定义服务发现实现的最小能力集合。

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()
	// SustainLease 维持租约/心跳,发生异常时可结合重试策略恢复。
	SustainLease()
	// WithRetryBefore 设置重试前回调,用于指标/告警等场景。
	WithRetryBefore(func())
	// WithRetryAfter 设置重试成功后回调,用于恢复通知等场景。
	WithRetryAfter(func())
	// WithLog 设置内部日志实例,统一输出实现内部状态。
	WithLog(*zap.Logger)
}

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

type ServiceConf

type ServiceConf struct {
	// 命名空间
	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 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"`
	LeaseId    int             `json:"lease_id"`
	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