简介 Viper
是一个 Go 应用的完整配置解决方案,包括微服务的12要素 ,主要支持一下特性
默认值设置
Golang的配置管理库
支持从JSON, TOML, YAML, HCL, envfile , Java 配置文件读取
实时监视和重新读取配置文件
读取环境变量配置
从远处配置系统(etcd or Consul)中读取
从命令行标志读取
从缓冲区读取
显示设置配置
viper
读取配置文件的优先级顺序:
viper.Set()
所设置的值
命令行 flag
环境变量
配置文件
配置中心etcd/consul
默认值
注意:viper
的配置键是不区分大小写的。
安装 go get -u https://github.com/spf13/viper
从配置文件读取值 1 2 3 4 5 6 7 8 viper.SetConfigName("production" ) viper.SetConfigType("yaml" ) viper.AddConfigPath("./config" ) viper.AddConfigPath("." ) err := viper.ReadInConfig() if err != nil { panic (fmt.Errorf("Fatal error config file: %w \n" , err)) }
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 { "datastore.metric.host" : "0.0.0.0" , "host" : { "address" : "localhost" , "port" : [ 5799 , 6029 ] }, "datastore" : { "metric" : { "host" : "127.0.0.1" , "port" : 3099 }, "warehouse" : { "host" : "198.0.0.1" , "port" : 2112 } } } viper.GetString("datastore.warehouse.host" ) viper.GetInt("host.ports.1" ) viper.GetString("datastore.metric.host" )
设置默认值 1 2 viper.SetDefault("username" , "stolen" ) viper.SetDefault("city" , map [string ]string {"country" : "China" , "Province" : "Chengdu" })
写到配置文件 1 2 3 4 viper.WriteConfig() viper.SafeWriteConfig() viper.WriteConfigAs("/path/to/my/.config" ) viper.SafeWriteConfigAs("/path/to/my/.config" )
监测并热加载配置文件 viper
支持应用程序在运行中实时读取配置文件的能力。确保在调用 WatchConfig()
之前添加所有的configPaths
1 2 3 4 viper.OnConfigChange(func (e fsnotify.Event) { fmt.Println("Config file changed:" , e.Name) }) viper.WatchConfig()
从 io.Reader 读取配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 viper.SetConfigType("yaml" ) var yamlExample = []byte (` Hacker: true name: steve hobbies: - skateboarding - snowboarding - go clothing: jacket: leather trousers: denim age: 35 eyes : brown beard: true ` )viper.ReadConfig(bytes.NewBuffer(yamlExample)) viper.Get("name" )
设置字段值 1 2 3 4 5 6 7 8 9 10 viper.Set("Verbose" , true ) viper.Set("LogFile" , LogFile) viper.RegisterAlias("aliasString" , "keyString" ) viper.Set("aliasString" , true ) viper.Set("keyString" , true ) viper.GetBool("aliasString" ) viper.GetBool("aliasString" )
读配置字段 1 2 3 4 5 6 7 8 9 10 11 12 13 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 {}
如果没有找到,每个 Get
函数将返回一个 0
值。为了检查一个给定的键是否存在,已经提供了 IsSet()
方法来检查键是否被设置了。
vipers viper 是一个不需要配置或初始化就可以开箱即用的工具,因为大部分应用都使用单一的配置中心来管理配置,类似单例。
但如果用户需要使用多个配置中心,可以进行实例化创建不同的 viper,每个 viper 可以读取不同的配置文件,设置不同的参数,像下面代码这样
1 2 3 4 5 6 7 x := viper.New() y := viper.New() x.SetDefault("ContentDir" , "content" ) y.SetDefault("ContentDir" , "foobar" )