Help us shape the future of Go! Take the 2026 Go Developer Survey

registry

package
v2.0.14 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedRegistry added in v2.0.4

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

CachedRegistry 是 RegistryClient 的装饰器(decorator)。

解决的核心问题:原实现每次微服务调用(client.CallCtx → selectHostByLB → GetServices) 都会同步打一次注册中心(Nacos /list、etcd Get、Consul Health、Polaris GET), 在高 QPS 下既拖慢单次调用、又给注册中心造成巨大压力,且注册中心抖动会直接击穿到业务调用。

本装饰器在内存中缓存 GetServices 结果:

  • 读取命中缓存(未过期)直接返回,零网络开销;
  • 缓存过期后删除,仅在下一次请求到来时按需回源;
  • 并发冷命中通过 inFlight 去重,只回源一次;

该层对调用方与注册中心后端完全透明,不改变 RegistryClient 接口语义。

func NewCachedRegistry added in v2.0.4

func NewCachedRegistry(inner RegistryClient) *CachedRegistry

NewCachedRegistry 用缓存层包裹一个真实的 RegistryClient。 inner 为 nil 时直接返回 nil(与 NewRegistry 的兜底逻辑保持一致)。

func (*CachedRegistry) DeRegister added in v2.0.4

func (c *CachedRegistry) DeRegister()

DeRegister 透传注销。

func (*CachedRegistry) GetServiceURL added in v2.0.4

func (c *CachedRegistry) GetServiceURL(servicename string, groupName ...string) (string, string)

GetServiceURL v1 单实例入口:复用缓存的实例列表,随机选 1 个(保持原语义)。 缓存未命中或回源失败时返回 ("", ""),与原 v1 行为一致。

func (*CachedRegistry) GetServices added in v2.0.4

func (c *CachedRegistry) GetServices(servicename string, groupName ...string) ([]string, error)

GetServices v2 多实例入口:优先返回未过期的缓存;过期后删除并按需回源。

func (*CachedRegistry) Register added in v2.0.4

func (c *CachedRegistry) Register(data []byte)

Register / DeRegister 直接透传到内层客户端。

type ConfiguredRegistrar added in v2.0.13

type ConfiguredRegistrar interface{ RegisterConfigured() }

type NamedRegistryResolver added in v2.0.13

type NamedRegistryResolver interface {
	GetServiceURLFrom(registryName, service string, group ...string) (string, string)
	GetServicesFrom(registryName, service string, group ...string) ([]string, error)
}

type RegistryClient

type RegistryClient interface {
	// Register 把本服务注册到配置中心。
	Register(registryConfigData []byte)
	// GetServiceURL v1 单实例返回,保留向后兼容。
	// 内部实现通常从多实例中随机选 1 个返回,与 GetServices 等价。
	GetServiceURL(servicename string, groupName ...string) (string, string)
	// GetServices v2 新增:返回该服务的所有可用实例 URL 列表。
	// 用于客户端负载均衡(pkg/loadbalancer)。
	// 找不到实例时返 (nil, nil),错误时返 (nil, err)。
	GetServices(servicename string, groupName ...string) ([]string, error)
	// DeRegister 注销本服务。
	DeRegister()
}

RegistryClient 是 mgin v2 扩展后的统一注册中心客户端接口。

v1 接口:Register / GetServiceURL / DeRegister(保持不变,存量项目无需改动)。 v2 新增:GetServices 多实例列表,供 client.CallCtx 的负载均衡使用。

var Registry RegistryClient

func NewRegistry

func NewRegistry() RegistryClient

type RegistryCollection added in v2.0.13

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

func (*RegistryCollection) DeRegister added in v2.0.13

func (c *RegistryCollection) DeRegister()

func (*RegistryCollection) GetServiceURL added in v2.0.13

func (c *RegistryCollection) GetServiceURL(service string, group ...string) (string, string)

func (*RegistryCollection) GetServiceURLFrom added in v2.0.13

func (c *RegistryCollection) GetServiceURLFrom(name, service string, group ...string) (string, string)

func (*RegistryCollection) GetServices added in v2.0.13

func (c *RegistryCollection) GetServices(service string, group ...string) ([]string, error)

func (*RegistryCollection) GetServicesFrom added in v2.0.13

func (c *RegistryCollection) GetServicesFrom(name, service string, group ...string) ([]string, error)

func (*RegistryCollection) Register added in v2.0.13

func (c *RegistryCollection) Register(data []byte)

func (*RegistryCollection) RegisterConfigured added in v2.0.13

func (c *RegistryCollection) RegisterConfigured()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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