etcdcli

package
v1.4.52 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 12 Imported by: 0

README

etcdcli

Connect to the etcd service client.

Example of use

    import "github.com/18721889353/sunshine/pkg/etcdcli"

    endpoints := []string{"192.168.3.37:2379"}
    // 方式1: 通过选项参数设置
    cli, err := etcdcli.NewClient(endpoints,
        etcdcli.WithDialTimeout(time.Second*2),
        etcdcli.WithAuth("root", "password"),
        // etcdcli.WithAutoSyncInterval(0),
    )

    // 方式2: 直接传入 clientv3.Config
    cli, err = etcdcli.NewClient(nil, etcdcli.WithConfig(&clientv3.Config{
        Endpoints:   endpoints,
        DialTimeout: time.Second * 2,
        // Username:    "",
        // Password:    "",
    }))

JWT Token 自动刷新

当 etcd 服务端使用 JWT 认证时,token 有固定的 TTL(如5分钟)。默认情况下,etcd 客户端 SDK 会在 token 过期后被动刷新(服务端会先拒绝请求再让客户端重新认证),导致服务端日志出现 "token is expired" 告警。

启用主动 token 刷新后,客户端会在后台协程中定期续期 token,避免服务端告警。

    // 方式1: 通过 WithAuthConfig 选项启用
    cli, err := etcdcli.NewClient(endpoints,
        etcdcli.WithAuth("root", "password"),
        etcdcli.WithAuthConfig(etcdcli.AuthConfig{
            TokenTTL: 5 * time.Minute, // 服务端 JWT token 有效期
        }),
    )

    // 方式2: 通过 YAML 配置(etcdInfo.etcdClient.authTokenTTL)
    // etcdInfo:
    //   etcdClient:
    //     username: root
    //     password: password
    //     authTokenTTL: 5m  # JWT token 有效期,客户端将自动在过期前刷新

AuthConfig 字段说明:

  • TokenTTL: 服务端签发的 JWT token 有效期(如 5m)
  • RefreshInterval: 客户端刷新间隔,默认为 TokenTTL * 0.6(即过期前40%时间刷新)

Documentation

Overview

Package etcdcli 用于连接到 etcd 服务

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient added in v1.4.40

func NewClient(endpoints []string, opts ...Option) (*clientv3.Client, error)

NewClient 创建一个 etcd 客户端连接。 注意: 如果设置了 WithConfig(*clientv3.Config) 参数,则 endpoints 参数将被忽略!

func StopTokenRefresh added in v1.4.50

func StopTokenRefresh(cli *clientv3.Client)

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 WithAuth

func WithAuth(username string, password string) Option

WithAuth 设置认证信息。

func WithAuthConfig added in v1.4.50

func WithAuthConfig(cfg AuthConfig) Option

WithAuthConfig 设置 JWT token 自动刷新策略。 启用后,客户端会在后台协程中定期主动刷新 token, 避免服务端出现 "token is expired" 告警。

func WithAutoSyncInterval

func WithAutoSyncInterval(duration time.Duration) Option

WithAutoSyncInterval 设置成员列表自动同步的时间间隔。

func WithConfig

func WithConfig(c *clientv3.Config) Option

WithConfig 设置 etcd 客户端的配置。

func WithDialKeepAliveTime added in v1.4.40

func WithDialKeepAliveTime(duration time.Duration) Option

WithDialKeepAliveTime 设置保持连接的时间间隔。

func WithDialKeepAliveTimeout added in v1.4.40

func WithDialKeepAliveTimeout(duration time.Duration) Option

WithDialKeepAliveTimeout 设置保持连接的超时时间。

func WithDialTimeout

func WithDialTimeout(duration time.Duration) Option

WithDialTimeout 设置连接超时时间。

func WithSecure

func WithSecure(serverNameOverride string, certFile string, caFile ...string) Option

WithSecure 设置 TLS 安全连接。 caFile 为空时使用单向 TLS,非空时启用双向 TLS(mTLS)。

Jump to

Keyboard shortcuts

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