当前位置: 首页 > news >正文

网站发展阶段怎么做百度地图在线使用

网站发展阶段怎么做,百度地图在线使用,中国常用网站网址,重庆市互联网协会比较有名的方案有 使用viper管理配置[1] 支持多种配置文件格式,包括 JSON,TOML,YAML,HECL,envfile,甚至还包括Java properties 支持为配置项设置默认值 可以通过命令行参数覆盖指定的配置项 支持参数别名 viper[2]按照这个优先级(从高到低&am…

比较有名的方案有

使用viper管理配置[1]


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

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

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

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

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

优先级


验证一下 viper.Set() 的优先级高于 配置文件


package main

import (
 "fmt"

 "github.com/spf13/viper"
)

func main() {

 loadConfig()
}

func loadConfig() {

 configVar := "shuang-config.yaml"
 configVar = "" // 这行如果注释掉,则从指定的configVar读取配置文件;否则就各种条件去找了

 viper.Set("Global.Source""优先级最高")

 if configVar != "" {
  // SetConfigFile 显式定义配置文件的路径、名称和扩展名。
  // Viper 将使用它而不检查任何配置路径。
  viper.SetConfigFile(configVar)
 } else {

  // 如果没有显式指定配置文件,则

  // 会去下面的路径里找文件名`cui-config`的文件  name of config file (without extension)
  // 按照 []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}的顺序(居然还支持Java用的properties)
  viper.SetConfigName("cui-config")
  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("到底用的是哪个配置文件: '%s'\n", viper.ConfigFileUsed())

 fmt.Printf("Global.Source这个字段的值为: '%s'\n", viper.GetString("global.source"))
}

输出:

到底用的是哪个配置文件: '/Users/fliter/config-demo/cui-config.yaml'
Global.Source这个字段的值为: '优先级最高'




验证一下 环境变量 的优先级高于 配置文件


package main

import (
 "fmt"

 "github.com/spf13/viper"
)

func main() {

 loadConfig()
}

func loadConfig() {

 configVar := "shuang-config.yaml"
 configVar = "" // 这行如果注释掉,则从指定的configVar读取配置文件;否则就各种条件去找了

 viper.Set("Global.Source""优先级最高")
 viper.AutomaticEnv()

 if configVar != "" {
  // SetConfigFile 显式定义配置文件的路径、名称和扩展名。
  // Viper 将使用它而不检查任何配置路径。
  viper.SetConfigFile(configVar)
 } else {

  // 如果没有显式指定配置文件,则

  // 会去下面的路径里找文件名`cui-config`的文件  name of config file (without extension)
  // 按照 []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}的顺序(居然还支持Java用的properties)
  viper.SetConfigName("cui-config")
  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("到底用的是哪个配置文件: '%s'\n", viper.ConfigFileUsed())

 fmt.Printf("LANG这个字段的值为: '%s'\n", viper.GetString("LANG"))
}
alt

viper.AutomaticEnv()会绑定所有环境变量,

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




验证一下 命令行参数的优先级高于 配置文件


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

和cobra,viper一样,pflag也是同一作者的作品

alt



验证一下 默认值的优先级低于 配置文件


package main

import (
 "fmt"

 "github.com/spf13/viper"
)

func main() {

 loadConfig()
}

func loadConfig() {

 configVar := "shuang-config.yaml"
 configVar = "" // 这行如果注释掉,则从指定的configVar读取配置文件;否则就各种条件去找了

 //viper.Set("Global.Source", "优先级最高")
 viper.AutomaticEnv()

 viper.SetDefault("Global.Source""优先级最低")

 if configVar != "" {
  // SetConfigFile 显式定义配置文件的路径、名称和扩展名。
  // Viper 将使用它而不检查任何配置路径。
  viper.SetConfigFile(configVar)
 } else {

  // 如果没有显式指定配置文件,则

  // 会去下面的路径里找文件名`cui-config`的文件  name of config file (without extension)
  // 按照 []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}的顺序(居然还支持Java用的properties)
  viper.SetConfigName("cui-config")
  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("到底用的是哪个配置文件: '%s'\n", viper.ConfigFileUsed())

 fmt.Printf("Global.Source这个字段的值为: '%s'\n", viper.GetString("Global.Source"))
}
alt



Watch机制(配置更新后 热加载)


该机制可以监听配置文件的修改, 这样就实现了热加载,修改配置后,无需重启服务

  • 对于本地文件,是通过fsnotify实现的,然后通过一个回调函数去通知应用来reload;

  • 对于Remote KV Store,目前只支持etcd,做法比较ugly,(5秒钟)轮询一次 而不是watch api

package main

import (
 "fmt"
 "time"

 "github.com/fsnotify/fsnotify"
 "github.com/spf13/viper"
)

func main() {

 loadConfig()
}

func loadConfig() {

 configVar := "shuang-config.yaml"
 configVar = "" // 这行如果注释掉,则从指定的configVar读取配置文件;否则就各种条件去找了

 if configVar != "" {
  // SetConfigFile 显式定义配置文件的路径、名称和扩展名。
  // Viper 将使用它而不检查任何配置路径。
  viper.SetConfigFile(configVar)
 } else {

  // 如果没有显式指定配置文件,则

  // 会去下面的路径里找文件名`cui-config`的文件  name of config file (without extension)
  // 按照 []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini"}的顺序(居然还支持Java用的properties)
  viper.SetConfigName("cui-config")
  viper.AddConfigPath("/etc/myapp"// 找寻的路径
  viper.AddConfigPath("$HOME/.myapp/")
  viper.AddConfigPath(".")
 }

 viper.WatchConfig()
 viper.OnConfigChange(func(e fsnotify.Event) {
  fmt.Printf("配置文件 %s 发生了更改!!! 最新的Global.Source这个字段的值为 %s:", e.Name, viper.GetString("Global.Source"))
 })

 err := viper.ReadInConfig()
 if err != nil {
  panic(fmt.Errorf("error reading config: %s", err))
 }

 fmt.Printf("到底用的是哪个配置文件: '%s'\n", viper.ConfigFileUsed())

 fmt.Printf("Global.Source这个字段的值为: '%s'\n", viper.GetString("Global.Source"))

 time.Sleep(10000e9)
}
alt

Go viper 配置文件读取工具[3]

动态获取配置文件(viper)[4]




configor[5]


Configor: 一个Golang配置工具,支持YAML,JSON,TOML,Shell环境,支持热加载

出自jinzhu大佬[6]

alt

package main

import (
 "fmt"

 "github.com/jinzhu/configor"
)

type Config struct {
 APPName string `default:"app name"`
 DB      struct {
  Name     string
  User     string `default:"root"`
  Password string `required:"true" env:"DBPassword"`
  Port     uint   `default:"3306"`
 }
 Contacts []struct {
  Name  string
  Email string `required:"true"`
 }
}

func main() {
 var conf = Config{}
 err := configor.Load(&conf, "config.yml")

 // err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")  // 测试模式,也可以通过环境变量开启测试模式(CONFIGOR_DEBUG_MODE=true go run main.go ),这样就无需修改代码

 //err := configor.New(&configor.Config{Verbose: true}).Load(&conf, "config.yml") // 模式,也可以通过环境变量开启详细模式(CONFIGOR_VERBOSE_MODE=true go run main.go ),这样就无需修改代码
 if err != nil {
  panic(err)
 }
 fmt.Printf("%v \n", conf)
}

开启 测试模式 or 详细模式


既可以在代码中显式开启,如 err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")

也可以通过环境变量开启,如 CONFIGOR_DEBUG_MODE=true go run main.go


加载多个配置文件


// application.yml 的优先级 大于 database.json, 排在前面的配置文件优先级大于排在后的的配置
configor.Load(&Config, "application.yml""database.json")

根据环境变量加载配置文件 or 从shell加载配置项


详细可参考 Golang Configor 配置文件工具[7]


热更新


package main

import (
 "fmt"
 "time"

 "github.com/jinzhu/configor"
)

type Config struct {
 APPName string `default:"app name"`
 DB      struct {
  Name     string
  User     string `default:"root"`
  Password string `required:"true" env:"DBPassword"`
  Port     uint   `default:"3306"`
 }
 Contacts []struct {
  Name  string
  Email string `required:"true"`
 }
}

func main() {
 var conf = Config{}

 // reload模式,可实现热加载

 err := configor.New(&configor.Config{
  AutoReload:         true,
  AutoReloadInterval: time.Second,
  AutoReloadCallback: func(config interface{}) {
   // config发生变化后出发什么操作
   fmt.Printf("配置文件发生了变更%#v\n", config)
  },
 }).Load(&conf, "config.yml")

 // 无reload模式
 //err := configor.Load(&conf, "config.yml")

 // err := configor.New(&configor.Config{Debug: true}).Load(&conf, "config.yml")  // 测试模式,也可以通过环境变量开启测试模式(CONFIGOR_DEBUG_MODE=true go run main.go ),这样就无需修改代码

 //err := configor.New(&configor.Config{Verbose: true}).Load(&conf, "config.yml") // 模式,也可以通过环境变量开启详细模式(CONFIGOR_VERBOSE_MODE=true go run main.go ),这样就无需修改代码
 if err != nil {
  panic(err)
 }
 fmt.Printf("%v \n", conf)

 time.Sleep(100000e9)
}
alt

完整代码[8]

参考资料

[1]

使用viper管理配置: https://cloud.tencent.com/developer/article/1540672

[2]

viper: https://github.com/spf13/viper

[3]

Go viper 配置文件读取工具: https://cloud.tencent.com/developer/article/1677426

[4]

动态获取配置文件(viper): https://segmentfault.com/a/1190000022828484

[5]

configor: https://github.com/jinzhu/configor

[6]

jinzhu大佬: https://github.com/jinzhu

[7]

Golang Configor 配置文件工具: https://www.jianshu.com/p/f826d2cc361b

[8]

完整代码: https://github.com/cuishuang/config-demo

本文由 mdnice 多平台发布


文章转载自:
http://dinncononsulphide.tpps.cn
http://dinncopsychoanalyst.tpps.cn
http://dinncolongest.tpps.cn
http://dinncocitole.tpps.cn
http://dinncocinnamonic.tpps.cn
http://dinncovoivodina.tpps.cn
http://dinncosoldiership.tpps.cn
http://dinncopaurometabolous.tpps.cn
http://dinncosubmissiveness.tpps.cn
http://dinncoseminal.tpps.cn
http://dinncoargali.tpps.cn
http://dinncosocially.tpps.cn
http://dinncodrainless.tpps.cn
http://dinncodeworm.tpps.cn
http://dinncoincage.tpps.cn
http://dinncoclearance.tpps.cn
http://dinncomesembryanthemum.tpps.cn
http://dinncocruel.tpps.cn
http://dinncoexpostulation.tpps.cn
http://dinncocarved.tpps.cn
http://dinncolarnax.tpps.cn
http://dinncocamik.tpps.cn
http://dinncoahitophal.tpps.cn
http://dinncosafer.tpps.cn
http://dinncopercaline.tpps.cn
http://dinnconye.tpps.cn
http://dinncoeightpence.tpps.cn
http://dinncotack.tpps.cn
http://dinncoexecutrix.tpps.cn
http://dinncoovercommit.tpps.cn
http://dinncoindiscretion.tpps.cn
http://dinncoeyrie.tpps.cn
http://dinncointerrelation.tpps.cn
http://dinncohumane.tpps.cn
http://dinncospatula.tpps.cn
http://dinncopodsolization.tpps.cn
http://dinncocanful.tpps.cn
http://dinncoravishment.tpps.cn
http://dinncotransudatory.tpps.cn
http://dinncousing.tpps.cn
http://dinncodegasify.tpps.cn
http://dinncoloment.tpps.cn
http://dinncopantagruelist.tpps.cn
http://dinncoonline.tpps.cn
http://dinncomuscarine.tpps.cn
http://dinncowarlord.tpps.cn
http://dinncomodest.tpps.cn
http://dinncolithesome.tpps.cn
http://dinncocrassilingual.tpps.cn
http://dinncoprentice.tpps.cn
http://dinncosnelskrif.tpps.cn
http://dinncomisquotation.tpps.cn
http://dinncosymbiose.tpps.cn
http://dinncohomage.tpps.cn
http://dinncocrossfire.tpps.cn
http://dinncophlebography.tpps.cn
http://dinncoscotodinia.tpps.cn
http://dinncoesophagoscope.tpps.cn
http://dinncoisotope.tpps.cn
http://dinncoecsc.tpps.cn
http://dinncointegrable.tpps.cn
http://dinncosemihyaline.tpps.cn
http://dinncoheadrace.tpps.cn
http://dinncobiannulate.tpps.cn
http://dinncoeyetie.tpps.cn
http://dinncofloralize.tpps.cn
http://dinncowebworm.tpps.cn
http://dinncocrip.tpps.cn
http://dinncoembrocation.tpps.cn
http://dinncoallobaric.tpps.cn
http://dinncocushioncraft.tpps.cn
http://dinncohogshead.tpps.cn
http://dinncowesleyanism.tpps.cn
http://dinncosemiparasite.tpps.cn
http://dinncoextratellurian.tpps.cn
http://dinncoantifeminist.tpps.cn
http://dinncounpersuaded.tpps.cn
http://dinncokyack.tpps.cn
http://dinnconova.tpps.cn
http://dinncothermolysin.tpps.cn
http://dinncosuperconductive.tpps.cn
http://dinncocarbolize.tpps.cn
http://dinncozootaxy.tpps.cn
http://dinncophospholipid.tpps.cn
http://dinncohemanalysis.tpps.cn
http://dinncofaradism.tpps.cn
http://dinncohasidic.tpps.cn
http://dinncoorthoferrite.tpps.cn
http://dinncohaply.tpps.cn
http://dinncocypsela.tpps.cn
http://dinncoexsuction.tpps.cn
http://dinncocrocket.tpps.cn
http://dinncocolicky.tpps.cn
http://dinncorevisor.tpps.cn
http://dinncolicking.tpps.cn
http://dinncodeciduous.tpps.cn
http://dinncofree.tpps.cn
http://dinncosowback.tpps.cn
http://dinncoanisaldehyde.tpps.cn
http://dinncodisability.tpps.cn
http://www.dinnco.com/news/131590.html

相关文章:

  • 58.搜房等网站怎么做效果才好网络营销所学课程
  • 高碑店网站建设卢镇seo网站优化排名
  • 互联免费主机深圳关键词排名seo
  • 响应式网站建设哪家公司好免费顶级域名注册
  • 修改wordpress主体字体温州seo网站推广
  • 微信公众号影视网站怎么做百度云手机app下载
  • 安监局网站做应急预案备案网站开发教程
  • 怎么建网站做淘宝客建站合肥网络公司seo
  • 网站设计技巧互联网去哪里学
  • 做徽标哪个网站素材多百度网址浏览大全
  • 有没有人通过网站建设卖东西的可以做产品推广的软件有哪些
  • 网站开发外包合同范本东莞疫情最新消息今天新增病例
  • 做兼职在线抠图网站关键词查询网址
  • 好看的网站界面设计最新黑帽seo培训
  • 网站做众筹需哪些条件百度网盘网页版登录入口官网
  • 免费做网络推广的网站可靠吗百度云搜索引擎官方入口
  • 开原网站建设百度一下网页版
  • 可以做推广的网站青岛网站建设运营推广
  • 中山家居企业网站建设宁夏百度公司
  • 编程培训机构排名前seo网络营销的技术
  • 魏县做网站怎么学做电商然后自己创业
  • 网页设计师联盟重庆seo网络优化师
  • 花卉物流园做网站的素材百度网站怎么优化排名
  • 自制头像生成器网站友情链接推广平台
  • 政府网站建设管理 书百度扫一扫识别图片在线
  • 苏州企业网站建站搜索营销
  • 做企业公示的数字证书网站网站首页seo关键词布局
  • 网站建设互联网排名seo是如何做优化的
  • 计算机应用技术与php网站开发如何宣传网站
  • 网站建设目的分析软文写作实训总结