mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-22 10:15:24 +02:00
docs of etcd config (#1622)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# Etcd Config
|
||||
|
||||
```go
|
||||
import (
|
||||
"log"
|
||||
|
||||
cfg "github.com/go-kratos/kratos/contrib/config/etcd/v2"
|
||||
"github.com/go-kratos/kratos/v2/config"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// create a 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()
|
||||
|
||||
// acquire config value
|
||||
foo, err := c.Value("/app-config").String()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
println(foo)
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user