Documentation
¶
Overview ¶
Package consul 提供基于 Consul 的服务注册与服务发现实现。
Index ¶
- func NewDiscover(client *api.Client, meta *micro.Meta, conf *micro.ServiceConf) (micro.Discovery, error)
- func NewRegister(client *api.Client, meta *micro.Meta, conf *micro.ServiceConf) (micro.Register, error)
- type DiscoverInstance
- type RegisterInstance
- func (s *RegisterInstance) Install(service *micro.ServiceNode) error
- func (s *RegisterInstance) SustainLease()
- func (s *RegisterInstance) Uninstall()
- func (s *RegisterInstance) WithLog(handle func(level logger.LogLevel, message string))
- func (s *RegisterInstance) WithRetryAfter(handle func())
- func (s *RegisterInstance) WithRetryBefore(handle func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DiscoverInstance ¶
type DiscoverInstance struct {
// contains filtered or unexported fields
}
DiscoverInstance 基于 Consul 的服务发现实例。
设计要点: - 通过 Consul Health API 获取指定 serviceName(namespace-env)的实例列表; - 将每个实例 Meta 中保存的 ServiceNode(JSON) 反序列化并构建本地缓存:
- service: appId -> 节点列表
- method: method -> appId(派生索引,用于 GetService 快速定位)
- Watcher 使用阻塞查询(WaitIndex)持续拉取变更后的快照,并重建本地缓存。
func (*DiscoverInstance) GetService ¶
func (s *DiscoverInstance) GetService(sm string) ([]*micro.ServiceNode, error)
GetService 根据 gRPC 方法名获取对应的服务节点列表。 参数:
- sm: 服务方法名
返回:
- []*micro.ServiceNode: 服务节点列表
- error: 错误信息,当服务方法不存在时返回错误
func (*DiscoverInstance) Watcher ¶
func (s *DiscoverInstance) Watcher()
Watcher 启动阻塞监听并持续刷新本地缓存。 该方法会阻塞执行,通常在单独的 goroutine 中调用。
type RegisterInstance ¶
type RegisterInstance struct {
// contains filtered or unexported fields
}
RegisterInstance 基于 Consul 的服务注册实例。
设计要点: - 使用 Consul Agent 的 TTL Check 作为“租约/心跳”能力; - Install 时注册一个服务实例并创建 TTL check; - SustainLease 周期性 UpdateTTL,维持健康状态; - 当心跳失败时按 conf.MaxRetry 做重试,并尝试重注册以恢复服务可见性。
func (*RegisterInstance) Install ¶
func (s *RegisterInstance) Install(service *micro.ServiceNode) error
Install 将服务节点注册到 Consul: - 补齐节点运行时信息(Meta/Kernel/Network/RunDate/LeaseId) - 通过 Consul Agent 注册服务并创建 TTL check
func (*RegisterInstance) SustainLease ¶
func (s *RegisterInstance) SustainLease()
SustainLease 维持 TTL 心跳,直到 Uninstall 或调用方主动取消。
func (*RegisterInstance) Uninstall ¶
func (s *RegisterInstance) Uninstall()
Uninstall 注销当前注册的服务实例并停止心跳。
func (*RegisterInstance) WithLog ¶
func (s *RegisterInstance) WithLog(handle func(level logger.LogLevel, message string))
WithLog 设置内部日志输出回调。
func (*RegisterInstance) WithRetryAfter ¶
func (s *RegisterInstance) WithRetryAfter(handle func())
WithRetryAfter 设置重试成功后的回调。
func (*RegisterInstance) WithRetryBefore ¶
func (s *RegisterInstance) WithRetryBefore(handle func())
WithRetryBefore 设置重试前回调。