Golang使用第三方包viper读取yaml配置信息操作
- 作者: 叶子落丶潇潇
- 来源: 51数据库
- 2021-08-14
golang有很多第三方包,其中的 viper 支持读取多种配置文件信息。本文只是做一个小小demo,用来学习入门用的。
1、安装
go get github.com/spf13/viper
2、编写一个yaml的配置文件,config.yaml
database: host: 127.0.0.1 user: root dbname: test pwd: 123456
3、编写学习脚本main.go,读取config.yaml配置信息
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func main() {
//获取项目的执行路径
path, err := os.getwd()
if err != nil {
panic(err)
}
config := viper.new()
config.addconfigpath(path) //设置读取的文件路径
config.setconfigname("config") //设置读取的文件名
config.setconfigtype("yaml") //设置文件的类型
//尝试进行配置读取
if err := config.readinconfig(); err != nil {
panic(err)
}
//打印文件读取出来的内容:
fmt.println(config.get("database.host"))
fmt.println(config.get("database.user"))
fmt.println(config.get("database.dbname"))
fmt.println(config.get("database.pwd"))
}
4、执行go run main.go
输出:
127.0.0.1 root test 123456
ok!
补充:go基于viper实现配置文件热更新及其源码分析
go第三方库 github.com/spf13/viper 实现了对配置文件的读取并注入到结构中,好用方便。
其中以
viperinstance := viper.new() // viper实例
viperinstance.watchconfig()
viperinstance.onconfigchange(func(e fsnotify.event) {
log.print("config file updated.")
viperloadconf(viperinstance) // 加载配置的方法
})
可实现配置的热更新,不用重启项目新配置即可生效(实现热加载的方法也不止这一种,比如以文件的上次修改时间来判断等)。
为什么这么写?这样写为什么就能立即生效?基于这两个问题一起来看看viper是怎样实现热更新的。
上面代码的核心一共两处:watchconfig()方法、onconfigchange()方法。watchconfig()方法用来开启事件监听,确定用户操作文件后该文件是否可正常读取,并将内容注入到viper实例的config字段,先来看看watchconfig()方法:
func (v *viper) watchconfig() {
go func() {
// 建立新的监视处理程序,开启一个协程开始等待事件
// 从i/o完成端口读取,将事件注入到event对象中:watcher.events
watcher, err := fsnotify.newwatcher()
if err != nil {
log.fatal(err)
}
defer watcher.close()
// we have to watch the entire directory to pick up renames/atomic saves in a cross-platform way
filename, err := v.getconfigfile()
if err != nil {
log.println("error:", err)
return
}
configfile := filepath.clean(filename) //配置文件e:\etc\bizsvc\config.yml
configdir, _ := filepath.split(configfile) // e:\etc\bizsvc\
done := make(chan bool)
go func() {
for {
select {
// 读取的event对象有两个属性,name为e:\etc\bizsvc\config.yml,op为write(对文件的操作)
case event := <-watcher.events:
// 清除内部的..和他前面的元素,清除当前路径.,用来判断操作的文件是否是configfile
if filepath.clean(event.name) == configfile {
// 如果对该文件进行了创建操作或写操作
if event.op&fsnotify.write == fsnotify.write || event.op&fsnotify.create == fsnotify.create {
err := v.readinconfig()
if err != nil {
log.println("error:", err)
}
v.onconfigchange(event)
}
}
case err := <-watcher.errors:
// 有错误将打印
log.println("error:", err)
}
}
}()
watcher.add(configdir)
<-done
}()
}
其中,fsnotify是用来监控目录及文件的第三方库; watcher, err := fsnotify.newwatcher() 用来建立新的监视处理程序,它会开启一个协程开始等待读取事件,完成 从i / o完成端口读取任务,将事件注入到event对象中,即watcher.events;

执行v.readinconfig()后配置文件的内容将重新读取到viper实例中,如下图:

执行完v.readinconfig()后,config字段的内容已经是用户修改的最新内容了;
其中这行v.onconfigchange(event)的onconfigchange是核心结构体viper的一个属性,类型是func:
type viper struct {
// delimiter that separates a list of keys
// used to access a nested value in one go
keydelim string
// a set of paths to look for the config file in
configpaths []string
// the filesystem to read config from.
fs afero.fs
// a set of remote providers to search for the configuration
remoteproviders []*defaultremoteprovider
// name of file to look for inside the path
configname string
configfile string
configtype string
envprefix string
automaticenvapplied bool
envkeyreplacer *strings.replacer
config map[string]interface{}
override map[string]interface{}
defaults map[string]interface{}
kvstore map[string]interface{}
pflags map[string]flagvalue
env map[string]string
aliases map[string]string
typebydefvalue bool
// store read properties on the object so that we can write back in order with comments.
// this will only be used if the configuration read is a properties file.
properties *properties.properties
onconfigchange func(fsnotify.event)
}
它用来传入本次event来执行你写的函数。为什么修改会立即生效?相信第二个疑问已经得到解决了。
接下来看看onconfigchange(func(e fsnotify.event) {...... })的运行情况:
func (v *viper) onconfigchange(run func(in fsnotify.event)) {
v.onconfigchange = run
}
方法参数为一个函数,类型为func(in fsnotify.event)) {},这就意味着开发者需要把你自己的执行逻辑放到这个func里面,在监听到event时就会执行你写的函数,所以就可以这样写:
viperinstance.onconfigchange(func(e fsnotify.event) {
log.print("config file updated.")
viperloadconf(viperinstance) // viperloadconf函数就是将最新配置注入到自定义结构体对象的逻辑
})
而onconfigchange方法的参数会赋值给形参run并传到viper实例的onconfigchange属性,以watchconfig()方法中的v.onconfigchange(event)来执行这个函数。
到此,第一个疑问也就解决了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
