Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop
This commit is contained in:
1
utils/fsconfig/.gitignore
vendored
Normal file
1
utils/fsconfig/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
log
|
||||
53
utils/fsconfig/config.go
Normal file
53
utils/fsconfig/config.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package fsconfig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type EnvConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port uint64 `yaml:"port"`
|
||||
|
||||
UserName string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
NamespaceId string `yaml:"namespace"`
|
||||
DataId string `yaml:"dataid"`
|
||||
Group string `yaml:"group"`
|
||||
}
|
||||
|
||||
var OptPathDir = "/opt"
|
||||
|
||||
var nacosConfig *EnvConfig
|
||||
|
||||
func GetEnvCofing() *EnvConfig {
|
||||
return nacosConfig
|
||||
}
|
||||
|
||||
func init() {
|
||||
if OptPathDir[len(OptPathDir)-1] != '/' {
|
||||
OptPathDir = OptPathDir + "/"
|
||||
}
|
||||
|
||||
for _, yname := range []string{"env.yaml", "env.yml"} {
|
||||
f, err := os.Open(OptPathDir + "/" + yname)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
cfg := &EnvConfig{}
|
||||
err = yaml.NewDecoder(f).Decode(&cfg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
nacosConfig = cfg
|
||||
return
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("can't find %s(env.yaml|env.yml) ", OptPathDir))
|
||||
}
|
||||
70
utils/fsconfig/config_test.go
Normal file
70
utils/fsconfig/config_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user