etcd

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 5 Imported by: 0

README

go-etcd

基于 etcd clientv3 的轻量封装,提供:

  • etcd client 初始化(Endpoints、可选用户名密码、可选 TLS 双向证书)
  • 基于 etcd 的轻量服务调用实现(invocation 子包)

当前状态

  • registry 子包已废弃并移除
  • 若仍有旧项目依赖 etcd registry 语义,应迁移到 go-micro/registry/agent 或直接迁往 invocation
  • go-etcd 当前只保留 etcd 客户端能力与 invocation 调用能力

安装

go get github.com/fireflycore/go-etcd

快速开始

基础用法

package main

import (
	"log"

	"github.com/fireflycore/go-etcd"
)

func main() {
	cli, err := etcd.New(&etcd.Conf{
		Endpoint: []string{"127.0.0.1:2379"},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer cli.Close()
}

用户名密码

当同时配置了 Username 与 Password 时,会启用用户名密码认证:

cli, err := etcd.New(&etcd.Conf{
	Endpoint: []string{"127.0.0.1:2379"},
	Username: "root",
	Password: "root",
})

TLS(双向证书)

当 Conf.Tls 同时配置了 CaCert / ClientCert / ClientCertKey 三个文件路径时启用 TLS,否则视为不启用:

cli, err := etcd.New(&etcd.Conf{
	Endpoint: []string{"127.0.0.1:2379"},
	Tls: &etcd.TLS{
		CaCert:        "/path/to/ca.pem",
		ClientCert:    "/path/to/client.pem",
		ClientCertKey: "/path/to/client.key",
	},
})

配置说明

初始化配置为 etcd.Conf。

常用字段:

  • Endpoint:etcd 节点列表(如 127.0.0.1:2379)
  • Username/Password:用户名密码(同时配置时启用)
  • Tls:TLS 双向证书配置(同时配置三个文件路径时启用)

附件指引

  • 服务调用(invocation)invocation/README.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(c *Conf) (*clientv3.Client, error)

New 根据配置创建 etcd v3 客户端。

Types

type Conf

type Conf struct {
	// Username 是 etcd 访问用户名。
	Username string `json:"account"`
	// Password 是 etcd 访问密码。
	Password string `json:"password"`
	// Endpoint 是 etcd 节点地址列表。
	Endpoint []string `json:"endpoint"`

	// Tls 是可选 TLS 连接配置。
	Tls *tlsx.TLS `json:"tls"`
}

Conf 定义 etcd 客户端初始化配置。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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