Documentation
¶
Overview ¶
Package configcenter 提供配置中心客户端(Nacos 风格 HTTP API): 拉取配置内容 + 轮询监听变更(回调)。 场景:配置热更新(灰度开关/业务参数不改代码不重启)。 说明:轻量实现(HTTP 拉取 + 定时轮询);长轮询与鉴权签名可在此基础上扩展。
Index ¶
- type CachedClient
- func (c *CachedClient) Close()
- func (c *CachedClient) Content() string
- func (c *CachedClient) Get(ctx context.Context) (string, error)
- func (c *CachedClient) Loaded() bool
- func (c *CachedClient) Refresh(ctx context.Context) error
- func (c *CachedClient) Start(ctx context.Context, interval time.Duration) error
- func (c *CachedClient) Subscribe() <-chan string
- func (c *CachedClient) UpdatedAt() time.Time
- type Client
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedClient ¶ added in v1.74.0
type CachedClient struct {
// contains filtered or unexported fields
}
CachedClient 是带本地缓存的配置中心客户端: 启动/定时刷新拉取,配置中心不可用时回退最后成功值(进程内缓存),变更时推送订阅者。 解决 Watch 场景下配置中心短暂不可用导致配置丢失的问题,对标 Nacos 客户端体验。
func NewCachedClient ¶ added in v1.74.0
func NewCachedClient(client *Client) *CachedClient
NewCachedClient 创建缓存客户端;client 为底层拉取客户端(不可为 nil)。
func (*CachedClient) Content ¶ added in v1.74.0
func (c *CachedClient) Content() string
Content 返回当前缓存内容(无锁读取;可能为空字符串)。
func (*CachedClient) Get ¶ added in v1.74.0
func (c *CachedClient) Get(ctx context.Context) (string, error)
Get 返回配置内容:未加载时拉取并缓存;已加载直接返回缓存(配置中心不可用也可用旧值)。
func (*CachedClient) Loaded ¶ added in v1.74.0
func (c *CachedClient) Loaded() bool
Loaded 返回是否成功加载过。
func (*CachedClient) Refresh ¶ added in v1.74.0
func (c *CachedClient) Refresh(ctx context.Context) error
Refresh 强制拉取并更新缓存;失败时保留旧值并返回错误。 内容变化时向订阅者广播(非阻塞,订阅通道缓冲 1)。
func (*CachedClient) Start ¶ added in v1.74.0
Start 后台轮询刷新(阻塞):interval 内拉取配置,变化即广播; ctx 取消时退出并关闭所有订阅通道。
func (*CachedClient) Subscribe ¶ added in v1.74.0
func (c *CachedClient) Subscribe() <-chan string
Subscribe 订阅配置变更:返回的通道在内容变化时收到新内容, 首次订阅立即收到当前缓存值(若有);Close 后通道关闭。
func (*CachedClient) UpdatedAt ¶ added in v1.74.0
func (c *CachedClient) UpdatedAt() time.Time
UpdatedAt 返回最近成功更新时间(零值表示从未成功)。
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 配置中心客户端。
func (*Client) Watch ¶
func (c *Client) Watch(ctx context.Context, interval time.Duration, onChange func(content string)) error
Watch 轮询监听配置变更:interval 为轮询间隔;onChange 收到新内容回调。 首次拉取即回调(初始化配置);之后内容变化才回调。 阻塞运行;ctx 取消后退出。
func (*Client) WithNamespace ¶
WithNamespace 设置命名空间(tenant)。