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 方法名返回可用节点和所属 appId。
// 例如方法 /user.UserService/Login 会映射到一个 appId 及其节点列表。
GetService(method string) ([]*ServiceNode, string, error)
// Watcher 启动发现监听并持续维护本地索引。
// 该方法通常阻塞运行,内部负责同步 Method -> AppId 与 AppId -> Nodes 的映射关系。
Watcher()
// Unwatch 停止监听并释放内部资源。
Unwatch()
// WatchEvent 注册变更回调,供外部订阅服务增删改事件。
// 回调触发时机由具体实现决定,调用方不应阻塞回调执行。
WatchEvent(callback WatchEventFunc)
}
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 定义服务实例运行时元信息。
type Meta ¶
type Meta struct {
// 环境,不同环境的服务不互通
Env string `json:"env"`
// 服务实例版本
Version string `json:"version"`
// 实例id, 实例id和应用id的区别就是,一个服务主体可以有多个实例
InstanceId string `json:"instance_id"`
// 应用id,泛指服务主体,不同版本的服务实例可以共享appId
AppId string `json:"app_id"`
}
Meta 服务元信息。
type Network ¶
type Network struct {
// 网卡唯一标识,用于grpc-gateway流量控制,同sn流量优先
SN string `json:"sn"`
// 内网地址
Internal string `json:"internal"`
// 外网地址
External string `json:"external"`
}
Network 定义服务节点上报的网络信息。
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 ¶
ServiceMethod 服务方法映射(method -> 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)。
type WatchEventFunc ¶ added in v1.2.7
type WatchEventFunc = func(event *ServiceEvent)
Click to show internal directories.
Click to hide internal directories.