59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package nacos
|
|
|
|
import (
|
|
"apiServer_service/utils/loger"
|
|
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
|
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
|
|
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
|
)
|
|
|
|
// NewNacosConfigClient 创建一个 Nacos 配置服务
|
|
func NewNacosConfigClient() (*config_client.IConfigClient, error) {
|
|
InitNacosRegistryConfig()
|
|
cli, err := clients.NewConfigClient(
|
|
vo.NacosClientParam{
|
|
ClientConfig: cc,
|
|
ServerConfigs: sc,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &cli, nil
|
|
}
|
|
|
|
// AddConfig 新增配置
|
|
func AddConfig(dataId, group, content string) error {
|
|
client, err := NewNacosConfigClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = (*client).PublishConfig(vo.ConfigParam{
|
|
DataId: dataId,
|
|
Group: group,
|
|
Content: content,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetConfig 获取配置
|
|
func GetConfig(dataId, group string) string {
|
|
client, err := NewNacosConfigClient()
|
|
if err != nil {
|
|
loger.Error("获取配置客户端失败", err)
|
|
return ""
|
|
}
|
|
content, err := (*client).GetConfig(vo.ConfigParam{
|
|
DataId: dataId,
|
|
Group: group,
|
|
})
|
|
if err != nil {
|
|
loger.Error("获取配置失败", err)
|
|
return ""
|
|
}
|
|
return content
|
|
}
|