71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package fsconfig_test
|
||
|
||
import (
|
||
"fusenapi/utils/fsconfig"
|
||
"log"
|
||
"testing"
|
||
|
||
"github.com/nacos-group/nacos-sdk-go/v2/clients"
|
||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
|
||
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||
)
|
||
|
||
func TestCase1(t *testing.T) {
|
||
|
||
env := fsconfig.GetEnvCofing()
|
||
|
||
// 创建serverConfig
|
||
// 支持多个;至少一个ServerConfig
|
||
serverConfig := []constant.ServerConfig{
|
||
{
|
||
IpAddr: env.Host,
|
||
Port: uint64(env.Port),
|
||
},
|
||
}
|
||
|
||
// 创建clientConfig
|
||
clientConfig := constant.ClientConfig{
|
||
NamespaceId: env.NamespaceId, // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
|
||
TimeoutMs: 50000,
|
||
NotLoadCacheAtStart: true,
|
||
LogLevel: "debug",
|
||
LogDir: "/tmp/nacos",
|
||
CacheDir: "/tmp/nacos",
|
||
Username: env.UserName,
|
||
Password: env.Password,
|
||
}
|
||
|
||
// 创建服务发现客户端的另一种方式 (推荐)
|
||
// namingClient, err := clients.NewNamingClient(
|
||
// vo.NacosClientParam{
|
||
// ClientConfig: &clientConfig,
|
||
// ServerConfigs: serverConfig,
|
||
// },
|
||
// )
|
||
// if err != nil {
|
||
// log.Fatalf("初始化nacos失败: %s", err.Error())
|
||
// }
|
||
|
||
// log.Println(namingClient)
|
||
|
||
// 创建 Nacos 配置客户端
|
||
configClient, err := clients.CreateConfigClient(map[string]interface{}{
|
||
"clientConfig": clientConfig,
|
||
"serverConfigs": serverConfig,
|
||
})
|
||
if err != nil {
|
||
log.Fatalf("Failed to create Nacos config client: %v", err)
|
||
}
|
||
|
||
// 获取配置
|
||
content, err := configClient.GetConfig(vo.ConfigParam{
|
||
DataId: "auth.yaml",
|
||
Group: env.Group,
|
||
})
|
||
if err != nil {
|
||
log.Fatalf("Failed to get config from Nacos: %v", err)
|
||
}
|
||
log.Println(content)
|
||
|
||
}
|