181 lines
4.4 KiB
Go
181 lines
4.4 KiB
Go
package config
|
||
|
||
import (
|
||
"agent-go/g"
|
||
"fmt"
|
||
"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/common/constant"
|
||
"github.com/nacos-group/nacos-sdk-go/v2/vo"
|
||
"github.com/spf13/viper"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
type OctopusAgentNacosConfig struct {
|
||
Spring Spring `json:"spring"`
|
||
Server Server `json:"server"`
|
||
}
|
||
|
||
type Server struct {
|
||
Port int64 `json:"port"`
|
||
}
|
||
|
||
type Spring struct {
|
||
Application Application `json:"application"`
|
||
Profiles Profiles `json:"profiles"`
|
||
Cloud Cloud `json:"cloud"`
|
||
}
|
||
|
||
type Application struct {
|
||
Name string `json:"name"`
|
||
}
|
||
|
||
type Cloud struct {
|
||
Nacos Nacos `json:"nacos"`
|
||
}
|
||
|
||
type Nacos struct {
|
||
Config Config `json:"config"`
|
||
}
|
||
|
||
type Config struct {
|
||
Group string `json:"group"`
|
||
ConfigRetryTime int64 `json:"config-retry-time"`
|
||
FileExtension string `json:"file-extension"`
|
||
MaxRetry int64 `json:"max-retry"`
|
||
ServerAddr string `json:"server-addr"`
|
||
Timeout uint64 `json:"timeout"`
|
||
ConfigLongPollTimeout int64 `json:"config-long-poll-timeout"`
|
||
ExtensionConfigs []ExtensionConfig `json:"extension-configs"`
|
||
}
|
||
|
||
type ExtensionConfig struct {
|
||
Group string `json:"group"`
|
||
DataID string `json:"data-id"`
|
||
}
|
||
|
||
type Profiles struct {
|
||
Active string `json:"active"`
|
||
}
|
||
|
||
var log = g.G.LOG
|
||
|
||
func InitNacos(configFileName string) {
|
||
|
||
v := parseAgentConfigFile(configFileName, nil)
|
||
group := v.GetString("spring.cloud.nacos.config.group")
|
||
|
||
configClient := startNacosConnection(v, group)
|
||
|
||
allNacosConfig := getAllNacosConfig(v, group, configClient)
|
||
|
||
for _, nacosConfig := range allNacosConfig {
|
||
parseAgentConfigFile(nacosConfig, v)
|
||
}
|
||
|
||
}
|
||
|
||
func parseAgentConfigFile(configFileName string, v *viper.Viper) *viper.Viper {
|
||
|
||
// 使用Viper框架读取
|
||
if v == nil {
|
||
v = viper.New()
|
||
}
|
||
|
||
// 设置配置文件路径和名称
|
||
v.SetConfigName(configFileName)
|
||
v.AddConfigPath(".")
|
||
v.SetConfigType("yaml")
|
||
|
||
// 读取默认的总配置文件
|
||
err := v.ReadInConfig()
|
||
if err != nil {
|
||
panic(fmt.Errorf("fatal error config file: %s", err))
|
||
}
|
||
|
||
log.Info(fmt.Sprintf("%s config read result are %v", configFileName, v.AllSettings()))
|
||
|
||
return v
|
||
}
|
||
|
||
func startNacosConnection(v *viper.Viper, group string) config_client.IConfigClient {
|
||
|
||
clientConfig := constant.ClientConfig{
|
||
NamespaceId: group,
|
||
TimeoutMs: v.GetUint64("spring.cloud.nacos.config.timeout"),
|
||
NotLoadCacheAtStart: true,
|
||
LogDir: "/tmp/nacos/log",
|
||
CacheDir: "/tmp/nacos/cache",
|
||
}
|
||
|
||
serverAddr := v.GetString("spring.cloud.nacos.config.server-addr")
|
||
split := strings.Split(serverAddr, ":")
|
||
if len(split) != 2 {
|
||
log.Error("nacos server addr error!")
|
||
}
|
||
|
||
port, _ := strconv.ParseUint(split[1], 10, 64)
|
||
serverConfigs := []constant.ServerConfig{
|
||
{
|
||
IpAddr: split[0],
|
||
Port: port,
|
||
},
|
||
}
|
||
|
||
// Another way of create config client for dynamic configuration (recommend)
|
||
configClient, err := clients.NewConfigClient(
|
||
vo.NacosClientParam{
|
||
ClientConfig: &clientConfig,
|
||
ServerConfigs: serverConfigs,
|
||
},
|
||
)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
return configClient
|
||
}
|
||
|
||
func getAllNacosConfig(v *viper.Viper, group string, configClient config_client.IConfigClient) []string {
|
||
|
||
result := make([]string, 0)
|
||
|
||
// main nacos configs
|
||
mainNacosConfigFileName := v.GetString("spring.application.name") + v.GetString("spring.profiles.active") + v.GetString("spring.cloud.nacos.config.file-extension")
|
||
getConfig(mainNacosConfigFileName, group, configClient)
|
||
result = append(result, mainNacosConfigFileName)
|
||
|
||
// additional nacos config
|
||
additionalNacosConfig := v.Get("spring.cloud.nacos.config.extension-configs")
|
||
// 增加断言,判定map的类型
|
||
m, ok := additionalNacosConfig.(map[string]string)
|
||
if !ok {
|
||
fmt.Println("additionalNacosConfig is not a map")
|
||
return nil
|
||
}
|
||
|
||
for additionalNacosConfigFileName, additionalNacosConfigGroup := range m {
|
||
getConfig(additionalNacosConfigFileName, additionalNacosConfigGroup, configClient)
|
||
result = append(result, additionalNacosConfigFileName)
|
||
}
|
||
|
||
return result
|
||
|
||
}
|
||
|
||
// getConfig 从Nacos中获取相应的
|
||
func getConfig(dataId string, group string, configClient config_client.IConfigClient) string {
|
||
|
||
content, err := configClient.GetConfig(vo.ConfigParam{
|
||
DataId: dataId,
|
||
Group: group,
|
||
Type: "yaml",
|
||
})
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
return content
|
||
}
|