首頁 > 軟體

go學習筆記讀取consul組態檔詳解

2022-05-19 13:00:41

新建yaml檔案

在上文我們的 go學習筆記:使用 consul 做服務發現和設定共用 這裡我們單獨來用viper實現讀取consul的設定, 我習慣與用yaml格式, 所以 首先 新建yaml檔案

store:
  book:
    - author: john
      price: 10
    - author: ken
      price: 12
  bicycle:
    color: red
    price: 19.95

讀取遠端設定

可以直接呼叫viper.AddRemoteProvider讀取遠端設定, 也可以用github.com/hashicorp/consul/api 來讀取consul的內容, 然後在用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("監聽consul錯誤:", 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

這樣的錯誤,可以在go.mod檔案增加:

replace google.golang.org/grpc => google.golang.org/grpc v1.21.1

以上就是go學習筆記讀取consul組態檔詳解的詳細內容,更多關於go讀取consul組態檔的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com