nacos

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 17 Imported by: 0

README

Nacos配置中心

更新日志

v0.4.6

  • 兼容viperyamljsontoml来映射结构体,历史配置不再需要将标签改为mapstructure

v0.3.16

  • 配置文件使用viper映射器,在使用conf struct对象映射时,需要使用mapstructure标签
  • 如果是直接使用struct对象映射,则tag不能携带.-等特殊字符,如果一定携带则推荐使用nacos.GetXX("a.b-c")获取

接入手册

1. 在配置中心创建配置文件

开发环境: http://nacos.aidi-dev.hobot.cc/nacos/#/

测试环境: http://nacos.aidi-test.hobot.cc/nacos/#/

线上环境: http://nacos.aidi.hobot.cc/nacos/#/

2. 代码中使用
// 程序启动时
import (
    "github.com/SigmaGavin/slardar/env"
    "github.com/SigmaGavin/slardar/log"
    "github.com/SigmaGavin/slardar/nacos"
)

// 注意,需要先配置环境变量
// AITC_MODE: dev/uat/prod
// APP_NAME: local.app/aidi.model.a/aidi.data.b
env.Init()
log.Init()

namespace := "model"       // 对应配置中心的namespace
confName := "aidi.olympus" // 对应配置中心的配置名
group := "DEFAULT_GROUP"   // 配置中心的
filename := "config"       // 同步到本地的配置文件名
conf := struct{
	LogLevel    string `mapstructure:"log_level"`
	TestDotConf string `mapstructure:"test.dot.conf"`  // 📢📢📢 这种key携带特殊字符的,viper映射会忽略
}{}         // 程序中的配置映射对象,非yaml格式,可以传nil
nacos.Init(namespace, group, confName, filename, &conf)
conf.TestDotConf = nacos.GetString("test.dot.conf")

// 后续读取配置
// conf.ThisIsA
// nacos.GetString("FIELD_NAME")
3. SDK特性
  1. 会自动从配置中心同步配置到本地,支持热更新

  2. 可以通过环境变量配置nacos地址,也可以通过环境变量CONF_PATH存放本地配置文件

  3. 配置兼容环境变量,前提是配置字段要大写。

Documentation

Overview

Package nacos 提供AIDI PDT后端服务的统一配置

包括从nacos配置中心拉取服务配置、服务配置变更后的自动更新

Index

Constants

This section is empty.

Variables

View Source
var (
	DevSC = constant.ServerConfig{
		Scheme:      "http",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi-dev.hobot.cc",
		Port:        80,
	}
	TestSC = constant.ServerConfig{
		Scheme:      "http",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi-test.hobot.cc",
		Port:        80,
	}
	PreSC = constant.ServerConfig{
		Scheme:      "http",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi.hobot.cc",
		Port:        80,
	}
	ProdSC = constant.ServerConfig{
		Scheme:      "http",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi.hobot.cc",
		Port:        80,
	}
	SaasTestSC = constant.ServerConfig{
		Scheme:      "https",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi-test.horizon.ai",
		Port:        443,
	}
	SaasProdSC = constant.ServerConfig{
		Scheme:      "https",
		ContextPath: "/nacos",
		IpAddr:      "nacos.aidi.horizon.ai",
		Port:        443,
	}
	SCMap = map[env.RunMode]constant.ServerConfig{
		env.DevMode:      DevSC,
		env.TestMode:     TestSC,
		env.PreMode:      PreSC,
		env.ProdMode:     ProdSC,
		env.SaasTestMode: SaasTestSC,
		env.SaasProdMode: SaasProdSC,
	}
)

Functions

func Get

func Get(key string) interface{}

Get 获取配置

func GetBool

func GetBool(key string) bool

GetBool 获取bool类型配置

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 获取float64类型配置

func GetInt32

func GetInt32(key string) (value int32)

GetInt32 获取 int32 配置

func GetInt64

func GetInt64(key string) (value int64)

GetInt64 获取 int64 配置

func GetString

func GetString(key string) (value string)

GetString 获取配置

func GetStrings

func GetStrings(key string) (s []string)

GetStrings 以,分割字符串 a,b,c,d ===> ["a", "b", "c", "d"]

func Init

func Init(namespace, group, dataID string, fileName string, conf interface{})

Init 初始化服务配置,从nacos中读取配置并反序列化到配置对象中 namespace、group和dataID分别为nacos中配置所在的命名空间、组和配置集唯一标识 fileName为从nacos同步到本地的配置文件名称,不包含文件后缀,支持yaml、json、toml格式 conf为自定义的配置对象的指针 传入conf对象会自动映射,如果是其他格式改对象可以不传(nil)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL