Files
kratos-bootstrap/remoteconfig/etcd/client_creator.go
2025-06-02 14:38:13 +08:00

49 lines
967 B
Go

package etcd
import (
"strings"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/log"
"google.golang.org/grpc"
etcdClient "go.etcd.io/etcd/client/v3"
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
)
// getConfigKey 获取合法的配置名
func getConfigKey(configKey string, useBackslash bool) string {
if useBackslash {
return strings.Replace(configKey, `.`, `/`, -1)
} else {
return configKey
}
}
// NewConfigSource 创建一个远程配置源 - Etcd
func NewConfigSource(c *conf.RemoteConfig) config.Source {
if c == nil || c.Etcd == nil {
return nil
}
cfg := etcdClient.Config{
Endpoints: c.Etcd.Endpoints,
DialTimeout: c.Etcd.Timeout.AsDuration(),
DialOptions: []grpc.DialOption{grpc.WithBlock()},
}
cli, err := etcdClient.New(cfg)
if err != nil {
panic(err)
}
source, err := New(cli, WithPath(getConfigKey(c.Etcd.Key, true)))
if err != nil {
log.Fatal(err)
}
return source
}