github地址:github.com/spf13/viper
Viper 是一个完整的 Go 应用程序配置解决方案,优势就在于开发项目中你不必去操心配置文件的格式而是让你腾出手来专注于项目的开发。其特性如下:
-
支持 JSON/TOML/YAML/HCL/envfile/Java properties 等多种格式的配置文件;
-
可以设置监听配置文件的修改,修改时自动加载新的配置;
-
从环境变量、命令行选项和io.Reader中读取配置;
-
从远程配置系统中读取和监听修改,如 etcd/Consul;
-
代码逻辑中显示设置键值
注:Viper让需要重启服务器才能使配置生效的日子一去不复返!!!这才是VIper最大的魅力
//global
var (GVA_DB *gorm.DBGVA_DBList map[string]*gorm.DBGVA_REDIS *redis.ClientGVA_CONFIG config.ServerGVA_VP *viper.Viper// GVA_LOG *oplogging.LoggerGVA_LOG *zap.LoggerGVA_Concurrency_Control = &singleflight.Group{}BlackCache local_cache.Cachelock sync.RWMutex)
// config.Server
type Server struct {JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`Zap Zap `mapstructure:"zap" json:"zap" yaml:"zap"`Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`Email Email `mapstructure:"email" json:"email" yaml:"email"`Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"`System System `mapstructure:"system" json:"system" yaml:"system"`Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`// autoAutoCode Autocode `mapstructure:"autocode" json:"autocode" yaml:"autocode"`// gormMysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"`DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"`// ossLocal Local `mapstructure:"local" json:"local" yaml:"local"`Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyun-oss" yaml:"aliyun-oss"`HuaWeiObs HuaWeiObs `mapstructure:"hua-wei-obs" json:"hua-wei-obs" yaml:"hua-wei-obs"`TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencent-cos" yaml:"tencent-cos"`AwsS3 AwsS3 `mapstructure:"aws-s3" json:"aws-s3" yaml:"aws-s3"`Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`// 跨域配置Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`}
下方是使用方法
func Viper(path ...string) {//一般放在初始化 init中执行v := viper.New()//创建新的if len(path) <= 0 {//判断是否传入配置文件地址v.AddConfigPath("./")//没有地址就用默认的地址} else {v.AddConfigPath(path[0])//使用第一个参数作为配置文件地址}//路径v.SetConfigType("yml")//设置配置文件的类型v.SetConfigName("config")//设置配置文件的名称err := v.ReadInConfig() //读取配置文件if err != nil {panic(err)}if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {//将读取的配置文件信息赋值给全局配置文件panic(err)}v.WatchConfig()//监听配置文件是否有变动v.OnConfigChange(func(e fsnotify.Event) { //fsnotify.Event 文件变更事件fmt.Println("config file changed:", e.Name)//事件名称if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {fmt.Println(err)}})global.GVA_VP = v}
在Viper中有多个获取配置文件内容的方法,可根据值的类型进行选择:
-
Get(key string) : interface{} -
GetBool(key string) : bool -
GetFloat64(key string) : float64 -
GetInt(key string) : int -
GetIntSlice(key string) : []int -
GetString(key string) : string -
GetStringMap(key string) : map[string]interface{} -
GetStringMapString(key string) : map[string]string -
GetStringSlice(key string) : []string -
GetTime(key string) : time.Time -
GetDuration(key string) : time.Duration -
IsSet(key string) : bool -
AllSettings() : map[string]interface{}
本文介绍了Viper,一个Go语言的配置管理库,支持多种格式,实时监听配置变化,简化项目配置管理。核心功能包括自动加载配置、环境变量优先级和远程配置支持。
3340

被折叠的 条评论
为什么被折叠?



