網(wǎng)站首頁 編程語言 正文
新建yaml文件
在上文我們的 go學(xué)習(xí)筆記:使用 consul 做服務(wù)發(fā)現(xiàn)和配置共享?這里我們單獨(dú)來用viper實(shí)現(xiàn)讀取consul的配置, 我習(xí)慣與用yaml格式, 所以 首先 新建yaml文件
store:
book:
- author: john
price: 10
- author: ken
price: 12
bicycle:
color: red
price: 19.95
讀取遠(yuǎn)程配置
可以直接調(diào)用viper.AddRemoteProvider讀取遠(yuǎn)程配置, 也可以用github.com/hashicorp/consul/api 來讀取consul的內(nèi)容, 然后在用viper讀取,
整改代碼如下:
package main import ( "bytes" "fmt" "log" "time" consulapi "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api/watch" "github.com/spf13/viper" _ "github.com/spf13/viper/remote" ) var ( defaultConfig *viper.Viper consulAddress string consulPath string ) func initConfig() *viper.Viper { consulAddress = "http://192.168.100.19:8500" consulPath = "config/v1/local" defaultConfig = viper.New() defaultConfig.SetConfigType("yaml") consulClient, err := consulapi.NewClient(&consulapi.Config{Address: consulAddress}) if err != nil { log.Fatalln("consul連接失敗:", err) } kv, _, err := consulClient.KV().Get(consulPath, nil) if err != nil { log.Fatalln("consul獲取配置失敗:", err) } err = defaultConfig.ReadConfig(bytes.NewBuffer(kv.Value)) if err != nil { log.Fatalln("Viper解析配置失敗:", err) } go watchConfig() return defaultConfig } func watchConfig() { time.Sleep(time.Second * 10) params := make(map[string]interface{}) params["type"] = "key" params["key"] = consulPath w, err := watch.Parse(params) if err != nil { log.Fatalln(err) } w.Handler = func(u uint64, i interface{}) { kv := i.(*consulapi.KVPair) hotconfig := viper.New() hotconfig.SetConfigType("yaml") err = hotconfig.ReadConfig(bytes.NewBuffer(kv.Value)) if err != nil { log.Fatalln("Viper解析配置失敗:", err) } defaultConfig = hotconfig } err = w.Run(consulAddress) if err != nil { log.Fatalln("監(jiān)聽consul錯(cuò)誤:", err) } } func GetConfig() *viper.Viper { if defaultConfig == nil { defaultConfig = initConfig() } return defaultConfig } func main() { ReadOne() go func() { for { host := GetConfig().GetString("store.bicycle.color") fmt.Println("consul===", host) time.Sleep(time.Second * 10) } }() select {} } func ReadOne() { runtimeConfig := viper.New() runtimeConfig.AddRemoteProvider("consul", "http://192.168.100.19:8500", "config/v1/local") runtimeConfig.SetConfigType("yaml") err := runtimeConfig.ReadRemoteConfig() if err != nil { log.Fatalln("viper read:", err) } err = runtimeConfig.WatchRemoteConfigOnChannel() if err != nil { log.Fatalln("viper watch err:", err) } go func() { for { host := runtimeConfig.GetString("store.bicycle.color") fmt.Println("viper=====", host) time.Sleep(time.Second * 10) } }() }
如果遇到?
google.golang.org/grpc/naming: module google.golang.org/grpc@latest found (v1.31.0), but does not contain package google.golang.org/grpc/naming
這樣的錯(cuò)誤,可以在go.mod文件增加:
replace?google.golang.org/grpc?=>?google.golang.org/grpc?v1.21.1
原文鏈接:https://www.cnblogs.com/majiang/p/14315879.html
相關(guān)推薦
- 2022-05-09 C#多線程之線程池ThreadPool用法_C#教程
- 2022-08-04 關(guān)于Mybatis的sql注釋問題_MsSql
- 2022-09-02 Redis?單機(jī)安裝和哨兵模式集群安裝的實(shí)現(xiàn)_Redis
- 2022-03-07 Go?container包的介紹_Golang
- 2022-01-16 npm:使用npm link來調(diào)試本地的包
- 2022-10-05 ASP.NET?Core在Task中使用IServiceProvider的問題解析_實(shí)用技巧
- 2022-07-09 python處理excel文件之xlsxwriter?模塊_python
- 2022-06-06 C#實(shí)現(xiàn)Excel轉(zhuǎn)PDF時(shí)設(shè)置內(nèi)容適應(yīng)頁面寬度_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支