feat: refactor.

This commit is contained in:
tx7do
2024-05-06 09:52:29 +08:00
parent 7471dbf968
commit 9df72bbad8
21 changed files with 1531 additions and 266 deletions

57
config/nacos/client.go Normal file
View File

@@ -0,0 +1,57 @@
package nacos
import (
"strings"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/log"
nacosKratos "github.com/go-kratos/kratos/contrib/config/nacos/v2"
nacosClients "github.com/nacos-group/nacos-sdk-go/clients"
nacosConstant "github.com/nacos-group/nacos-sdk-go/common/constant"
nacosVo "github.com/nacos-group/nacos-sdk-go/vo"
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 创建一个远程配置源 - Nacos
func NewConfigSource(c *conf.RemoteConfig) config.Source {
srvConf := []nacosConstant.ServerConfig{
*nacosConstant.NewServerConfig(c.Nacos.Address, c.Nacos.Port),
}
cliConf := nacosConstant.ClientConfig{
TimeoutMs: 10 * 1000, // http请求超时时间单位毫秒
BeatInterval: 5 * 1000, // 心跳间隔时间,单位毫秒
UpdateThreadNum: 20, // 更新服务的线程数
LogLevel: "debug",
CacheDir: "../../configs/cache", // 缓存目录
LogDir: "../../configs/log", // 日志目录
NotLoadCacheAtStart: true, // 在启动时不读取本地缓存数据true--不读取false--读取
UpdateCacheWhenEmpty: true, // 当服务列表为空时是否更新本地缓存true--更新,false--不更新
}
nacosClient, err := nacosClients.NewConfigClient(
nacosVo.NacosClientParam{
ClientConfig: &cliConf,
ServerConfigs: srvConf,
},
)
if err != nil {
log.Fatal(err)
}
return nacosKratos.NewConfigSource(nacosClient,
nacosKratos.WithGroup(getConfigKey(c.Nacos.Key, false)),
nacosKratos.WithDataID("bootstrap.yaml"),
)
}