lihf ab873fe393 fix(config): 修复 Etcd客户端初始化和清理逻辑 2 ماه پیش
..
README.md d561a672d2 Initial commit 8 ماه پیش
config.go bec5909909 feat: 修正config的包名,加上多配置源的处理 3 ماه پیش
config_test.go bec5909909 feat: 修正config的包名,加上多配置源的处理 3 ماه پیش
etcd_client.go ab873fe393 fix(config): 修复 Etcd客户端初始化和清理逻辑 2 ماه پیش
source.go d5219534b7 feat(config): 重构配置模块并添加 etcd 客户端 2 ماه پیش
watcher.go bec5909909 feat: 修正config的包名,加上多配置源的处理 3 ماه پیش

README.md

Etcd Config

import (
	"log"

	clientv3 "go.etcd.io/etcd/client/v3"
	"google.golang.org/grpc"

	cfg "github.com/go-kratos/kratos/contrib/config/etcd/v2"
	"github.com/go-kratos/kratos/v2/config"
)

// create an etcd client
client, err := clientv3.New(clientv3.Config{
    Endpoints:   []string{"127.0.0.1:2379"},
    DialTimeout: time.Second,
    DialOptions: []grpc.DialOption{grpc.WithBlock()},
})
if err != nil {
    log.Fatal(err)
}

// configure the source, "path" is required
source, err := cfg.New(client, cfg.WithPath("/app-config"), cfg.WithPrefix(true))
if err != nil {
    log.Fatalln(err)
}

// create a config instance with source
c := config.New(config.WithSource(source))
defer c.Close()

// load sources before get
if err := c.Load(); err != nil {
    log.Fatalln(err)
}

// acquire config value
foo, err := c.Value("/app-config").String()
if err != nil {
    log.Fatalln(err)
}

log.Println(foo)