Go 学习笔记(82)— Go 第三方库之 viper(解析配置文件、热更新配置文件)
1. viper 特點
viper 是一個完整的 Go應用程序的配置解決方案,它被設計為在應用程序中工作,并能處理所有類型的配置需求和格式。支持特性功能如下:
- 設置默認值
- 讀取 JSON、TOML、YAML、HCL、envfile和Java屬性的配置文件
- 監控配置文件改動,并熱加載配置文件
- 從環境變量中讀取
- 從遠程配置中心讀取配置(etcd/consul),并監控變動
- 從命令行標志中讀取
- 從緩沖區讀取
- 支持直接設置配置項的值
viper讀取配置文件的優先級順序:
- viper.Set()所設置的值
- 命令行 flag
- 環境變量
- 配置文件
- 配置中心etcd/consul
- 默認值
注意:
viper的配置鍵是不區分大小寫的。
2. 安裝
官網地址:https://github.com/spf13/viper 安裝命令
go get github.com/spf13/viper
3. 將配置注冊到 viper
3.1 創建默認值
viper.SetDefault("Name", "wohu")
viper.SetDefault("Gender", "male")
viper.SetDefault("City", map[string]string{"country": "China", "Province": "Beijing"})3.2 從配置文件讀取值
viper默認不配置文件的搜索路徑,將配置文件路徑的搜索決定權放在用戶程序中。
viper.SetConfigName("config") // 配置文件名字,注意沒有擴展名
viper.SetConfigType("yaml") // 如果配置文件的名稱中沒有包含擴展名,那么該字段是必需的
viper.AddConfigPath("/etc/appname/")   // 配置文件的路徑
viper.AddConfigPath("$HOME/.appname")  // 多次調用添加多個配置文件的路徑
viper.AddConfigPath(".")               // 在當前工作目錄尋找配置文件
err := viper.ReadInConfig() // 查找并讀取配置文件
if err != nil { panic(fmt.Errorf("Fatal error config file: %w \n", err))
}
3.3 將 viper值保存到配置文件
viper.WriteConfig()
將當前的配置寫到預先通過 viper.AddConfigPath() 和 viper.SetConfigName() 配置的路徑,如果該目錄下已經有對應的配置文件則會覆蓋,如果找不到對應的路徑則報錯。
viper.SafeWriteConfig() // 與第一個區別是不會覆蓋當前已經存在的文件
viper.WriteConfigAs("/path/to/my/.config") // 會覆蓋當前已經存在的文件
viper.SafeWriteConfigAs("/path/to/my/.config")  // 不會覆蓋當前已經存在的文件
viper.SafeWriteConfigAs("/path/to/my/.other_config")
3.4 監測并熱加載配置文件
viper支持應用程序在運行中實時讀取配置文件的能力。確保在調用 WatchConfig()之前添加所有的configPaths。
viper.OnConfigChange(func(e fsnotify.Event) {fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()
3.5 從 io.Reader 讀取配置
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")// any approach to require this configuration into your program.
var yamlExample = []byte(`
Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:jacket: leathertrousers: denim
age: 35
eyes : brown
beard: true
`)viper.ReadConfig(bytes.NewBuffer(yamlExample))viper.Get("name") // this would be "steve"
4. 從 viper 讀取配置
4.1 讀取單個值方法
在 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{}
注意:如果沒有找到,每個
Get函數將返回一個0值。為了檢查一個給定的鍵是否存在,已經提供了IsSet()方法。
4.1 讀取嵌套的配置
{"host": {"address": "localhost","port": 5799},"datastore": {"metric": {"host": "127.0.0.1","port": 3099},"warehouse": {"host": "198.0.0.1","port": 2112}}
}
viper可以通過傳遞一個以 .為界的鍵值路徑來訪問一個嵌套字段。
GetString("datastore.metric.host") // (returns "127.0.0.1")
viper可以通過使用路徑中的數字來訪問數組索引,例如
{"host": {"address": "localhost","ports": [5799,6029]},"datastore": {"metric": {"host": "127.0.0.1","port": 3099},"warehouse": {"host": "198.0.0.1","port": 2112}}
}GetInt("host.ports.1") // returns 6029
如果存在一個與劃定的鍵路徑相匹配的鍵,其值將被返回。例如。
{"datastore.metric.host": "0.0.0.0","host": {"address": "localhost","port": 5799},"datastore": {"metric": {"host": "127.0.0.1","port": 3099},"warehouse": {"host": "198.0.0.1","port": 2112}}
}GetString("datastore.metric.host") // returns "0.0.0.0"5. 使用示例
代碼結構如下:
.
├── conf
│   └── config.yaml
├── config
│   └── config.go
├── go.mod
├── go.sum
├── main.go
└── README.md2 directories, 6 files
config.yaml 內容
name: demo
host: 127.0.0.1:3306
username: root
password: root         
config.go內容:
package configimport ("log""github.com/fsnotify/fsnotify""github.com/spf13/viper"
)type Config struct {Name     stringHost     stringUsername stringPassword string
}func Init() (*Config, error) {viper.AddConfigPath("conf")   // 設置配置文件路徑viper.SetConfigName("config") // 設置配置文件名viper.SetConfigType("yaml")   // 設置配置文件類型格式為YAML// 初始化配置文件if err := viper.ReadInConfig(); err != nil { // viper解析配置文件return &Config{}, err}// 監控配置文件變化并熱加載程序,即不重啟程序進程就可以加載最新的配置viper.WatchConfig()viper.OnConfigChange(func(e fsnotify.Event) {log.Printf("Config file changed: %s", e.Name)})c := &Config{Name:     viper.GetString("name"),Host:     viper.GetString("host"),Username: viper.GetString("username"),Password: viper.GetString("password"),}return c, nil
}main.go內容:
package mainimport ("fmt""time""webserver/config""github.com/spf13/viper"
)func main() {// init config_, err := config.Init()if err != nil {fmt.Println(err)}// 注意:只能在 init 之后再次通過 viper.Get 方法讀取配置,否則不生效for {cfg := &config.Config{Name:     viper.GetString("name"),Host:     viper.GetString("host"),Username: viper.GetString("username"),Password: viper.GetString("password"),}fmt.Println(cfg.Name)time.Sleep(4 * time.Second)}}運行 main.go 之后修改配置文件查看打印:
$ go run main.go 
123
123
2021/12/03 14:05:45 Config file changed: /home/wohu/goProject/webserver/conf/config.yaml
2021/12/03 14:05:45 Config file changed: /home/wohu/goProject/webserver/conf/config.yaml
345
345
345
2021/12/03 14:05:56 Config file changed: /home/wohu/goProject/webserver/conf/config.yaml
2021/12/03 14:05:56 Config file changed: /home/wohu/goProject/webserver/conf/config.yaml
demo
demo
總結
以上是生活随笔為你收集整理的Go 学习笔记(82)— Go 第三方库之 viper(解析配置文件、热更新配置文件)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 2022-2028年中国应急救援装备行业
- 下一篇: 2022-2028年中国内衣行业研究及前
