Golang使用第三方包viper读取yaml配置信息操作
作者:mrtwenty 发布时间:2024-05-09 14:51:19
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)来执行这个函数。
到此,第一个疑问也就解决了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/mrtwenty/article/details/97621402
猜你喜欢
- 本文讲述了python实现的正则表达式功能。分享给大家供大家参考,具体如下:前文:首先,什么叫正则表达式(Regular Expressio
- 1. 预估执行计划 - Explain PlanExplain plan以SQL语句作为输入,得到这条SQL语句的执行计划,并将执行计划输出
- 基本介绍pandas是Python数据挖掘、数据分析中常用的库。而DataFrame生成excel中的sheet表,以及在excel中增加s
- 在使用python对网页进行多次快速爬取的时候,访问次数过于频繁,服务器不会考虑User-Agent的信息,会直接把你视为爬虫,从而过滤掉,
- 区别于redis的分布式缓存,ehcache是纯java进程内的单机缓存,根据不同的场景可选择使用,以下内容主要为springboot整合e
- 本文实例讲述了django实现分页的方法。分享给大家供大家参考。具体如下:Python代码如下:#!/usr/bin/env python#
- 1. Jupyter Notebooks作为小白,我现在使用的python编辑器是Jupyter Notebook,非常的好用,推荐!!!你
- 一:crypto库安装pycrypto,pycryptodome是crypto第三方库,pycrypto已经停止更新三年了,所以不建议安装这
- 前言由于安装某个项目的执行文件,提示要卸载MySQL以便它自身MySQL安装,然后我禁用了MYSQL服务,再把这个文件夹删除后,发现还是提示
- 本文实例讲述了js获取url传值的方法。分享给大家供大家参考,具体如下:js获取url参数值:index.htm?参数1=数值1&参
- Python 语言里有许多(而且是越来越多)的高级特性,是 Python 发烧友们非常喜欢的。在这些人的眼里,能够写出那些一般开发者看不懂的
- django-mdeditorGithub地址:https://github.com/pylixm/django-mdeditor 欢迎试用
- SET @SQL = 'SELECT * FROM Comment with(nolock) WHERE 1=1 &nbs
- 上篇博客转载了关于感知器的用法,遂这篇做个大概总结,并实现一个简单的感知器,也为了加深自己的理解。感知器是最简单的神经网络,只有一层。感知器
- 前言本文记录了对于Python的数据类型中元祖(Tuple)和字典(Dict)的一些认识,以及部分内置方法的介绍。下面话不多说,来看看详细的
- 前言最近工作中遇到了一些需求,想通过图形化的方式显示社交网络特定用户的好友关系,上网找了一下这方面的图形库有networkx、graphvi
- 本文实例讲述了python通过colorama模块在控制台输出彩色文字的方法。分享给大家供大家参考。具体分析如下:colorama是一个py
- 一、定义集合中的元素是无序的、唯一的、不可变的类型。集合是一个特殊的列表,可以对数据去重。lists = [1,3,5,7,3,4,6,2,
- 本文实例为大家分享了python实现简易动态时钟的具体代码,供大家参考,具体内容如下from turtle import *from dat
- 1.前台ajax数据提交<form id="login_form" action="" met