1. 项目地址:
    https://github.com/spf13/viper

  2. 从配置文件中读取:

    1. 创建 test.json 文件:

       {
           "name": "lee",
           "one": {
               "two": 2
           }
       }
    2. 示例代码:

       package main
      
       import (
           "fmt"
           "github.com/spf13/viper"
       )
      
       type Config struct {
           Name string  `mapstructure:"name"`
           One  OneConf `mapstructure:"one"`
       }
       type OneConf struct {
           Two int `mapstructure:"two"`
       }
      
       var config Config
      
       func main() {
           viper.SetConfigName("test") // 配置文件名
           viper.SetConfigType("json") // 配置文件格式,支持:json, toml, yaml, hcl, ini, env
           viper.AddConfigPath("./")   // 配置文件路径,"./"表示当前文件夹
           err := viper.ReadInConfig()
           if err != nil {
               panic(fmt.Errorf("Fatal error read in config: %w \n", err))
           }
           fmt.Println(viper.Get("name"))    // 获取单层
           fmt.Println(viper.Get("one.two")) // 获取多层
           var oneConf OneConf
           err = viper.UnmarshalKey("one", &oneConf) // 解析指定 key
           if err != nil {
               panic(fmt.Errorf("Fatal error unmarshalKey: %w \n", err))
           }
           fmt.Printf("one.two is:%d \n", oneConf.Two)
           if err := viper.Unmarshal(&config); err != nil { // 解析全部配置
               panic(fmt.Errorf("Fatal error unmarshal: %w \n", err))
           }
           fmt.Printf("config is %v: \n", config)
       }
    3. 运行:

      cd /path/to/project/
      go run main.go
  3. 从 etcd 中读取:

    1. 往 etcd 中添加 key:
      etcdctl put /config/config.json \

       {
           "name": "lee",
           "one": {
               "two": 2
           }
       }
    2. 示例代码:

       package main
      
       import (
           "fmt"
           "github.com/spf13/viper"
           _ "github.com/spf13/viper/remote"
           "time"
       )
      
       type Config struct {
           Name string  `mapstructure:"name"`
           One  OneConf `mapstructure:"one"`
       }
       type OneConf struct {
           Two int `mapstructure:"two"`
       }
      
       var config Config
      
       func main() {
           if err := viper.AddRemoteProvider("etcd3", "http://127.0.0.1:2379", "/config/config.json"); err != nil {
               panic(fmt.Errorf("Fatal error add remote provider: %w \n", err))
           }
           viper.SetConfigType("json") // 配置文件格式,支持:json, toml, yaml, hcl, ini, env
           err := viper.ReadRemoteConfig()
           if err != nil {
               panic(fmt.Errorf("Fatal error read remote config: %w \n", err))
           }
           if err := viper.Unmarshal(&config); err != nil {
               panic(fmt.Errorf("Fatal error unmarshal: %w \n", err))
           }
           fmt.Println("初始配置:", config)
           // watch -- 监听 key 的变化
           go func() {
               for {
                   time.Sleep(time.Second * 5)
                   if err := viper.WatchRemoteConfig(); err != nil {
                       fmt.Printf("error when watch remote config: %w \n", err)
                       continue
                   }
                   if err = viper.Unmarshal(&config); nil != err {
                       fmt.Printf("unable to read remote config: %v", err)
                       continue
                   }
                   fmt.Println("监听配置:", config)
               }
           }()
           select {}
       }
文档更新时间: 2024-04-20 10:57   作者:lee