Documentation
¶
Overview ¶
Package etcdcli 用于连接到 etcd 服务
Index ¶
- func NewClient(endpoints []string, opts ...Option) (*clientv3.Client, error)
- func ObserveEtcdConnected(ready bool)
- func ObserveEtcdConnectedAs(endpoint string, ready bool)
- func StartConnMonitor(cli *clientv3.Client, interval time.Duration)
- func StopConnMonitor(cli *clientv3.Client)
- func StopTokenRefresh(cli *clientv3.Client)
- type AuthConfig
- type Option
- func WithAuth(username string, password string) Option
- func WithAuthConfig(cfg AuthConfig) Option
- func WithAutoSyncInterval(duration time.Duration) Option
- func WithConfig(c *clientv3.Config) Option
- func WithDialKeepAliveTime(duration time.Duration) Option
- func WithDialKeepAliveTimeout(duration time.Duration) Option
- func WithDialTimeout(duration time.Duration) Option
- func WithSecure(serverNameOverride string, certFile string, caFile ...string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClient ¶ added in v1.4.40
NewClient 创建一个 etcd 客户端连接。 注意: 如果设置了 WithConfig(*clientv3.Config) 参数,则 endpoints 参数将被忽略!
func ObserveEtcdConnected ¶ added in v1.4.68
func ObserveEtcdConnected(ready bool)
ObserveEtcdConnected 记录 etcd 连通性(grpc 连接状态,单实例默认 label)。
func ObserveEtcdConnectedAs ¶ added in v1.4.68
ObserveEtcdConnectedAs 记录指定 endpoint 的连通性(多客户端实例场景)。
func StartConnMonitor ¶ added in v1.4.68
StartConnMonitor 定期读取 etcd grpc 连接状态(本地状态,无 RPC 开销)。 与 startTokenRefresh 同模式:内部创建 cancel,StopConnMonitor 释放。
func StopConnMonitor ¶ added in v1.4.68
StopConnMonitor 停止指定客户端的连接监控。
func StopTokenRefresh ¶ added in v1.4.50
StopTokenRefresh 停止指定客户端的 JWT token 自动刷新。 通常在客户端 Close() 之前或之后调用,释放后台协程资源。
Types ¶
type AuthConfig ¶ added in v1.4.50
type AuthConfig struct {
// TokenTTL 表示服务端签发的 JWT token 有效期(例如5分钟)。
// 客户端会在 token 过期前 RefreshInterval 时间主动刷新。
TokenTTL time.Duration
// RefreshInterval 表示客户端主动刷新 token 的时间间隔。
// 建议设置为 TokenTTL 的60%~80%,确保在 token 过期前完成刷新。
// 默认值为 TokenTTL * 0.6(即过期前40%时间刷新)。
RefreshInterval time.Duration
}
AuthConfig 用于配置 etcd JWT 认证 token 的自动刷新策略。 当 etcd 服务端使用 JWT 认证时,客户端需要在 token 过期前主动续期, 避免服务端出现 "token is expired" 告警。
type Option ¶
type Option func(*options)
Option 用于设置 etcd 客户端的选项。
func WithAuthConfig ¶ added in v1.4.50
func WithAuthConfig(cfg AuthConfig) Option
WithAuthConfig 设置 JWT token 自动刷新策略。 启用后,客户端会在后台协程中定期主动刷新 token, 避免服务端出现 "token is expired" 告警。
func WithAutoSyncInterval ¶
WithAutoSyncInterval 设置成员列表自动同步的时间间隔。
func WithDialKeepAliveTime ¶ added in v1.4.40
WithDialKeepAliveTime 设置保持连接的时间间隔。
func WithDialKeepAliveTimeout ¶ added in v1.4.40
WithDialKeepAliveTimeout 设置保持连接的超时时间。
func WithDialTimeout ¶
WithDialTimeout 设置连接超时时间。