Golang使用第三方包viper读取yaml配置信息操作

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)来执行这个函数。

到此,第一个疑问也就解决了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

(0)

相关推荐

  • 聊聊Golang中很好用的viper配置模块

    前言 viper 支持Yaml.Json. TOML.HCL 等格式,读取非常的方便. 安装 go get github.com/spf13/viper 如果提示找不到golang.org/x/text/这个库,是因为golang.org/x/text/这个库在GitHub上托管的路径不一致. 解决办法: 可以从https://github.com/golang/text下载源码下来,然后到$GOPATH/src下面创建golang.org/x/文件夹(已存在的忽略),把压缩包的文件解压到gol

  • golang 使用 viper 读取自定义配置文件

    viper 支持 Yaml.Json. TOML.HCL 等格式,读取非常的方便. viper 官网有案例:https://github.com/spf13/viper go get github.com/spf13/viper 创建 config.yaml 文件 database: driver: mysql host: 127.0.0.1 port: 3306 username: blog dbname: blog password: 123456 建一个 config.go 用于初始化配置

  • Golang使用第三方包viper读取yaml配置信息操作

    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&quo

  • golang常用库之配置文件解析库-viper使用详解

    golang常用库:gorilla/mux-http路由库使用 golang常用库:配置文件解析库-viper使用 golang常用库:操作数据库的orm框架-gorm基本使用 golang常用库:字段参数验证库-validator使用 一.viper简介 viper 配置管理解析库,是由大神 Steve Francia 开发,他在google领导着 golang 的产品开发,他也是 gohugo.io 的创始人之一,命令行解析库 cobra 开发者.总之,他在golang领域是专家,很牛的一个

  • Python脚本读取Consul配置信息的方法示例

    先来说一下背景,为什么要写脚本去读Consul的配置信息呢?Consul是啥呢?consul是google开源的一个使用go语言开发的服务发现.配置管理中心服务.目前公司用的是这个东西去管理项目上的一些配置信息.公司的环境是通过docker镜像的方式去部署的,镜像是通过rancher去进行管理的.这一套东西面临的一个问题是:服务每次更新之后,服务对应的ip地址是动态变化的.每次需要使用swagger去测接口的时候,都要去rancher上去重新找新的ip地址,比较麻烦.正好呢,最近部门在考虑准备做

  • docker修改未启动容器的配置信息操作

    之前一开始用docker并没有使用dockerfile或者docker-compose编排服务,直接使用docker run启动的容器,结果一不小心配置文件出错或者给停掉了那就导致服务再也启动不了了,这时候不要着急,使用以下办法既可以修改配置文件还可以修改启动容器的端口信息和其他配置信息,这样容器中的文件和数据还是存在的. 1.修改容器内的配置信息 这里以nginx启动为例子,比如用docke run创建了一个nginx的容器,结果因为使用docker exec ** bash 修改了内部ngi

  • golang实践-第三方包为私有库的配置方案

    正常使用了go 1.8一段时间没有发现异常,为了发布便捷,以及后期引入plug-in,开始将大项目分解.涉及到通过vendor引入私有库保存的第三方包. 参考网上那些反复转帖的材料,始终无法成功,总是都会出现类似以下的错误: package git.oschina.net/xxx/yyy: unrecognized import path "git.oschina.net/xxx/yyy" (parse https://git.oschina.net/xxx/yyy?go-get=1:

  • Golang获取目录下的文件及目录信息操作

    一.获取当前目录下的文件或目录信息(不包含多级子目录) func main() { pwd,_ := os.Getwd() //获取文件或目录相关信息 fileInfoList,err := ioutil.ReadDir(pwd) if err != nil { log.Fatal(err) } fmt.Println(len(fileInfoList)) for i := range fileInfoList { fmt.Println(fileInfoList[i].Name()) //打印

  • Go语言读取YAML 配置文件的两种方式分享

    目录 前言 yaml.v3 包 读取 yaml 文件 viper 包 读取 yaml 文件 小结 前言 在日常开发中,YAML 格式的文件基本上被默认为是配置文件,其内容因为缩进带来的层级感看起来非常直观和整洁.本文将会对 YAML 内容的读取进行介绍. yaml.v3 包 yaml.v3 的包,可以让我们在 Go 里面轻松地操作 yaml 格式的数据(如将 yaml 格式转成结构体等).在使用 yaml.v3 包之前,我们需要先安装它: go get gopkg.in/yaml.v3 读取 y

  • Laravel第三方包报class not found的解决方法

    出现的问题 公司开发使用PHP,技术框架使用Laravel.最近线上出现一个问题,就是上线之后,每次都会出错.查看出错原因,是composer安装的第三方出现class not found.因为这个问题,在线下使用Lumen框架的时候,遇到过,查找问题原因是因为依赖的composer包中composer.json中的"autoload":{"psr-4":{}}书写格式问题.解决方法使用命令:composer dump-autoload -o; 虽然知道问题的所在,

  • golang如何通过viper读取config.yaml文件

    目录 1.导入依赖包 2.编写yaml文件 3.编写读取yaml文件的go文件 4.使用config对象 5.viper源码分析 1.导入依赖包 import (     "github.com/spf13/viper" ) 2.编写yaml文件 放在conf目录下,文件名叫config.yaml # TODO  本地调试时放开 KubeSphere_URL: http://192.168.103.48:3188 # TODO 部署到环境时放开 #KubeSphere_URL: htt

  • Go读取yaml文件到struct类的实现方法

    目录 1.yaml文件准备 2.config配置类准备 3.读取配置文件到配置类 3.1.安装Viper组件 3.2.golang** **代码编写 1.yaml文件准备 common: secretid: AKIDxxxxx secretKey: 3xgGxxxx egion: ap-guangzhou zone: ap-guangzhou-7 InstanceChargeType: POSTPAID_BY_HOUR 2.config配置类准备 可以通过在线配置工具转换成struct 例如:h

随机推荐