前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang生态:使用viper管理配置

Golang生态:使用viper管理配置

作者头像
王录华
发布2019-11-19 19:58:24
4.7K1
发布2019-11-19 19:58:24
举报
文章被收录于专栏:云服务与SRE架构师社区

配置管理

通过配置,我们可以动态地改变程序的行为,常用的方式包括配置文件,命令行参数,环境变量等。我原来一直很欣赏ssh的配置管理方式:

  • 每一个配置项都有一个默认值
  • 这些默认值可以配置文件(/etc/ssh/ssh_config)中修改
  • 每个配置项都可以通过命令行参数临时覆盖

一直以来笔者都希望在自己的项目中实现类似的功能,但是毫无疑问,这些东西做起来是相当费工夫的。在研究Golang周边生态时,我发现了viper(https://github.com/spf14/viper)这个全面的配置解决方案,它提供的比我能想到的还要多:

  • 支持多种配置文件格式,包括 JSON,TOML,YAML,HECL,envfile,甚至还包括Java properties
  • 支持为配置项设置默认值
  • 可以通过命令行参数覆盖指定的配置项
  • 支持参数别名

viper按照这个优先级(从高到低)获取配置项的取值:

  • explicit call to Set: 在代码逻辑中通过viper.Set()直接设置配置项的值
  • flag:命令行参数
  • env:环境变量
  • config:配置文件
  • key/value store:etcd或者consul
  • default:默认值

下面通过一个myapp(https://github.com/4179e1/misc/tree/master/go/src/viper)的例子来简单介绍这些特性。

配置文件

myapp使用一个配置文件,yaml的格式如下:

代码语言:javascript
复制
Global:
  Source: "config(local)"
  LogLevel: "info"
  ChangeMe: "v3"

Server:
  Address: "127.0.0.1"
  Port: 8080

Client:
  Echo: true
  Foo: "bar"
  Servers:
    - "127.0.0.1"
    - "192.168.1.1"

直接运行./myapp 会尝试从以下路径寻找名为config.ext的配置文件

  • /etc/maypp/
  • ~/.myapp/
  • . (当前目录)

其中.ext表示配置文件的后缀如yaml,我们甚至不用指定这个后缀,viper会找自己支持的格式。如果想要指定配置文件路径,可以使用./myapp --config /path/to/config.yaml

代码语言:javascript
复制
    if configVar != "" {
        viper.SetConfigFile(configVar)
    } else {
        viper.SetConfigName("config") //name of config file (without extension)
        viper.AddConfigPath("/etc/myapp")
        viper.AddConfigPath("$HOME/.myapp/")
        viper.AddConfigPath(".")
    }
    
    err = viper.ReadInConfig()
    if err != nil {
        panic(fmt.Errorf("error reading config: %s", err))
    }
    fmt.Printf("Using configuration file '%s'\n", viper.ConfigFileUsed())

    fmt.Printf("Global.Source: '%s'\n", viper.GetString("global.source"))

这里要执行viper.ReadInConfig()之后,viper.ReadInConfig()才能确定到底用了哪个文件,viper按照上面的ConfigPath搜索,找到第一个名为config.ext的文件后即停止。如果有多个名为config的文件,按照以下顺序搜索

  • config.json
  • config.toml
  • config.yaml
  • config.yml
  • config.properties
  • config.props
  • config.prop
  • config.hcl

运行这个程序可以看到它正确的读取了globla.source的值,并且它是大小写不敏感的

代码语言:javascript
复制
$ go run main.go
Using configuration file '/Users/lyre/workspace/misc/go/src/viper/config.yaml'
Global.Source: 'config(local)'

$ go run main.go  --config ./config.yaml
Using configuration file './config.yaml'
Global.Source: 'config(local)'

环境变量

云原生的12要素里面有一条是“在环境中存储配置“, 在配置文件的基础上,我们还可以用环境变量来覆盖,甚至是完全代替配置文件,下面的代码中,viper会自动捕获所有以MYAPP_开头的环境变量,比如Global.Source这个参数映射到了MYAPP_GLOBAL_SOURCE

代码语言:javascript
复制
    viper.AutomaticEnv()
    viper.SetEnvPrefix("MYAPP")
    viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")

尽管配置文件中指定了Global.Source,但是它的值被环境变量覆盖了

代码语言:javascript
复制
$ MYAPP_GLOBAL_SOURCE=env go run main.go
Using configuration file '/Users/lyre/workspace/misc/go/src/viper/config.yaml'
Global.Source: 'env'

viper.AutomaticEnv()会绑定所有环境变量,如果只希望绑定特定的,可以使用SetEnvPrefix("global.source", "MYAPP_GLOAL_SOURCE"),注意这个函数不会自动加上MYAPP的前缀.

命令行参数

viper可以配合pflag来使用,pflag可以理解为标准库flag的一个增强版,viper可以绑定到pflag上

代码语言:javascript
复制
var globalSource = pflag.String("global.source", "default(flag)", "identify the source of configuration")
var serverAddress = pflag.String("server.address", "", "server listen address, empty for all")
var serverPort = pflag.Int("server.port", 8080, "server listen port"

func main() {
    /* ... */
    pflag.Parse()
    viper.BindPFlags(pflag.CommandLine)
    /* ... */
}

下面的例子中,虽然配置文件和环境变量都为Global.Source赋了值,但是命令行参数的优先级更高

代码语言:javascript
复制
$ MYAPP_GLOBAL_SOURCE=env go run main.go --config ./config.yaml --global.source flag
Using configuration file './config.yaml'
Globle.Source: 'flag'

viper.BindPFlags()自动绑定了所有命令行参数,如果只需要其中一部分,可以用viper.BingPflag()选择性绑定,如viper.BindPFlag("global.source", pflag.Lookup("global.source"))

默认值

viper支持为配置项设置默认值,如果在配置文件,环境变量,以及命令行参数中都没有设置,则使用默认值。这带来一个小问题,pflag命令行参数也支持默认值,它们组合关系的结果是怎么样的?

代码语言:javascript
复制
var globalUnset = pflag.String("global.unset", "default(flag)", "this parameter do not appear in config file")

func main() {
    pflag.Parse()
    viper.BindPFlags(pflag.CommandLine)
    viper.SetDefault("global.unset", "default(viper)")
    fmt.Println (viper.GetString("global.unset")) // ???
}
  • viper.SetDefault()的优先级更高,只要设置了这个,它会忽略pflag的默认值,除非运行时显式指定了命令行参数的值
  • 如果pflag设置了默认值而viper没有,viper取pflag的值

Remote KV Store

最后,作为对本地配置文件的替代品,viper支持从etcd/consul直接读取配置,以etcd为例:

代码语言:javascript
复制
viper.AddRemoteProvider("etcd", "http://127.0.0.1:4001","/config/hugo.json")
viper.SetConfigType("json") // because there is no file extension in a stream of bytes, supported extensions are "json", "toml", "yaml", "yml", "properties", "props", "prop", "env", "dotenv"
err := viper.ReadRemoteConfig()

跟本地配置文件不同的一点在于,这里要显示声明配置文件的格式,因为viper是通过文件流读取的,它不知道mime信息。

在此基础上,我对 --config 参数做了一些扩展,如果它的参数是以下两种形式之一则会从etcd或consul读取配置,而不再使用本地配置文件:

  • etcd+http://127.0.0.1:2380/path/to/key.yaml
  • consul://127.0.0.1:8500/key.json
代码语言:javascript
复制
# 往etcd里面写入这个配置
$ ETCDCTL_API=2 etcdctl set /config/myapp/config.yaml <<EOF
Global:
  Source: "etcd"
  LogLevel: "info"
  ChangeMe: "v3"

Server:
  Address: "127.0.0.1"
  Port: 8080

Client:
  Echo: true
  Foo: "bar"
  Servers:
    - "127.0.0.1"
    - "192.168.1.1"
EOF

# 通过viper读取

$ go run main.go --config etcd+http://127.0.0.1:2379/config/myapp/config.yaml
Using Remote Config: 'etcd+http://127.0.0.1:2379/config/myapp/config.yaml'
Global.Source: 'etcd'

  • viper 目前只支持etcd v2 api
  • consul不熟,没测试

Watch机制

viper还有一个重要特性是能够监控配置文件的修改,对于本地文件,是通过ionotify实现的,然后通过一个回调函数去通知应用来reload:

代码语言:javascript
复制
        viper.WatchConfig()
        viper.OnConfigChange(func(e fsnotify.Event) {
            fmt.Println("Config file changed:", e.Name)
        })

对于Remote KV Store,目前只支持etcd,但是做法就比较ugly了,用的是轮询而不是watch api:

代码语言:javascript
复制
// open a goroutine to watch remote changes forever
go func(){
    for {
        time.Sleep(time.Second * 5) // delay after each request

        // currently, only tested with etcd support
        err := runtime_viper.WatchRemoteConfig()
        if err != nil {
            log.Errorf("unable to read remote config: %v", err)
            continue
        }

        // unmarshal new config into our runtime config struct. you can also use channel
        // to implement a signal to notify the system of the changes
        runtime_viper.Unmarshal(&runtime_conf)
    }
}()

Bugfix

如果你启用了go mod,在从etcd读取配置的时候可能会遇到这个错误

代码语言:javascript
复制
panic: codecgen version mismatch: current: 8, need 10. Re-generate file: /Users/lyre/go/pkg/mod/github.com/coreos/etcd@v3.3.10+incompatible/client/keys.generated.go

解决办法是:

代码语言:javascript
复制
go get github.com/ugorji/go@v1.1.1
go get github.com/ugorji/go/codec@none

Links

  • viper(https://github.com/spf13/viper)
  • myapp(https://github.com/4179e1/misc/tree/master/go/src/viper)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云服务与SRE架构师社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置管理
  • 配置文件
  • 环境变量
  • 命令行参数
  • 默认值
  • Remote KV Store
  • Watch机制
  • Bugfix
  • Links
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档