etcd

package
v1.1.21 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetClient

func SetClient(client Client)

SetClient 设置全局etcd客户端

Types

type Client

type Client interface {
	// Put 存储键值对
	Put(ctx context.Context, key, value string, opts ...interface{}) error
	// Get 获取键值
	Get(ctx context.Context, key string, opts ...interface{}) (string, error)
	// Delete 删除键
	Delete(ctx context.Context, key string, opts ...interface{}) error
	// List 列出指定前缀的所有键值对
	List(ctx context.Context, prefix string, opts ...interface{}) (map[string]string, error)
	// Watch 监听键变化
	Watch(ctx context.Context, key string, opts ...interface{}) (<-chan WatchEvent, error)
	// Register 服务注册
	Register(ctx context.Context, serviceKey, serviceValue string, ttl int64) error
	// Unregister 服务注销
	Unregister(ctx context.Context, serviceKey string) error
	// Discover 服务发现
	Discover(ctx context.Context, servicePrefix string) ([]ServiceEndpoint, error)
	// Close 关闭客户端
	Close() error
}

Client etcd客户端接口

func GetClient

func GetClient() Client

GetClient 获取全局etcd客户端

func NewClientFromConfig

func NewClientFromConfig(cfg *config.ETCDConfig) (Client, error)

NewClientFromConfig 从配置创建etcd客户端

func NewEtcdV3Client

func NewEtcdV3Client(config *ClientConfig) (Client, error)

NewEtcdV3Client 创建etcd v3客户端

type ClientConfig

type ClientConfig struct {
	Endpoints   []string
	Username    string
	Password    string
	Namespace   string
	DialTimeout time.Duration
	Timeout     time.Duration
	TLS         *tls.Config
}

ClientConfig etcd客户端配置

type ServiceEndpoint

type ServiceEndpoint struct {
	Address string            `json:"address"`
	Port    int               `json:"port"`
	Meta    map[string]string `json:"meta"`
}

ServiceEndpoint 服务端点

type WatchEvent

type WatchEvent struct {
	Type  WatchEventType
	Key   string
	Value string
}

WatchEvent 监听事件

type WatchEventType

type WatchEventType int

WatchEventType 监听事件类型

const (
	WatchEventTypePut WatchEventType = iota
	WatchEventTypeDelete
)

Jump to

Keyboard shortcuts

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