fusenapi/utils/fsconfig/config_test.go

71 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}