1. 程式人生 > 程式設計 >viper配置框架的介紹支援zookeeper的讀取和監聽

viper配置框架的介紹支援zookeeper的讀取和監聽

viper作為配置框架,其功能非常的強大,我們沒有理由不去了解一下。我們先看官網對它的功能簡介:

viper是完整配置解決方案,他可以處理所有型別和格式的配置檔案,他有如下功能:

  • 設定預設配置
  • 支援讀取 JSON TOML YAML HCL 和 Java 屬性配置檔案
  • 監聽配置檔案變化,實時讀取讀取配置檔案內容
  • 讀取環境變數值
  • 讀取遠端配置系統 (etcd Consul) 和監控配置變化
  • 讀取命令 Flag 值
  • 讀取 buffer 值
  • 讀取確切值

乍一看,未免有相見恨晚之感,可仔細一想,不免腦袋裡有另外一種聲音:不會不支援讀取 zookeeper 吧?好吧,至少我是這樣的。

基於這種想法,當然要去立馬嘗試,如下:

viper.AddRemoteProvider("zookeeper","xx.xx.xx.xx:2181","/viper/test")

返回結果是:

Unsupported Remote Provider Type zookeeper

果不其然,於是追蹤 viper.AddRemoteProvider 的原始碼,發現viper只支援如下幾種

var SupportedRemoteProviders = []string{"etcd","consul","firestore"}

如果就此打住,未免有點太可惜,作為偏執狂,總想著能否來改造下viper,讓其支援 zookeeper

,於是在issue上找是否有人遇到同樣的問題,還整讓我找到了, 傳送 。但是不完整,且稍微有點bug。所以根據他的基礎上,我做了些調整。進入正題,我們開始修改viper原始碼。說明下,我的viper版本是最新的 1.7.0

修改原始碼

1、新增zookeeper.go

新增的位置: github.com/bketelsen/crypt/zookeeper,zookeeper 目錄需要自己建立, github.com/bketelsen/crypt 是viper的依賴包,會自動下載

viper配置框架的介紹支援zookeeper的讀取和監聽

檔案內容:

package zookeeper

import (
	"errors"
	"fmt"
	zk "github.com/samuel/go-zookeeper/zk"
	//"github.com/xordataexchange/crypt/backend"
	"github.com/bketelsen/crypt/backend"
	"strings"
	"time"
)

type Client struct {
	client *zk.Conn
	waitIndex uint64
}

func New(machines []string) (*Client,error) {
	zkclient,_,err := zk.Connect(machines,time.Second)
	if err != nil {
		return nil,err
	}
	return &Client{zkclient,0},nil
}

func (c *Client) Get(key string) ([]byte,error) {
	resp,err := c.client.Get(key)
	if err != nil {
		return nil,err
	}
	return []byte(resp),nil
}

func nodeWalk(prefix string,c *Client,vars map[string]string) error {
	l,stat,err := c.client.Children(prefix)
	if err != nil {
		return err
	}

	if stat.NumChildren == 0 {
		b,err := c.client.Get(prefix)
		if err != nil {
			return err
		}
		vars[prefix] = string(b)

	} else {
		for _,key := range l {
			s := prefix + "/" + key
			_,err := c.client.Exists(s)
			if err != nil {
				return err
			}
			if stat.NumChildren == 0 {
				b,err := c.client.Get(s)
				if err != nil {
					return err
				}
				vars[s] = string(b)
			} else {
				nodeWalk(s,c,vars)
			}
		}
	}
	return nil
}

func (c *Client) GetValues(key string,keys []string) (map[string]string,error) {
	vars := make(map[string]string)
	for _,v := range keys {
		v = fmt.Sprintf("%s/%s",key,v)
		v = strings.Replace(v,"/*","",-1)
		_,err := c.client.Exists(v)
		if err != nil {
			return vars,err
		}
		if v == "/" {
			v = ""
		}
		err = nodeWalk(v,vars)
		if err != nil {
			return vars,err
		}
	}
	return vars,nil
}

func (c *Client) List(key string) (backend.KVPairs,error) {
	var list backend.KVPairs
	resp,err := c.client.Children(key)
	if err != nil {
		return nil,err
	}

	if stat.NumChildren == 0 {
		return list,nil
	}

	entries,err := c.GetValues(key,resp)
	if err != nil {
		return nil,err
	}

	for k,v := range entries {
		list = append(list,&backend.KVPair{Key: k,Value: []byte(v)})
	}
	return list,nil
}

func (c *Client) createParents(key string) error {
	flags := int32(0)
	acl := zk.WorldACL(zk.PermAll)

	if key[0] != '/' {
		return errors.New("Invalid path")
	}

	payload := []byte("")
	pathString := ""
	pathNodes := strings.Split(key,"/")
	for i := 1; i < len(pathNodes); i++ {
		pathString += "/" + pathNodes[i]
		_,err := c.client.Create(pathString,payload,flags,acl)
		// not being able to create the node because it exists or not having
		// sufficient rights is not an issue. It is ok for the node to already
		// exist and/or us to only have read rights
		if err != nil && err != zk.ErrNodeExists && err != zk.ErrNoAuth {
			return err
		}
	}
	return nil
}

func (c *Client) Set(key string,value []byte) error {
	err := c.createParents(key)
	if err != nil {
		return err
	}
	_,err = c.client.Set(key,[]byte(value),-1)
	return err
}

func (c *Client) Watch(key string,stop chan bool) <-chan *backend.Response {
	respChan := make(chan *backend.Response,0)
	go func() {
		for {
			resp,watch,err := c.client.GetW(key)
			if err != nil {
				respChan <- &backend.Response{nil,err}
				time.Sleep(time.Second * 5)
			}

			select {
			case e := <-watch:
				if e.Type == zk.EventNodeDataChanged {
					resp,err = c.client.Get(key)
					if err != nil {
						respChan <- &backend.Response{nil,err}
					}
					c.waitIndex = 0
					respChan <- &backend.Response{[]byte(resp),nil}
				}
			}
		}
	}()
	return respChan
}

這個檔案是實現 ConfigManager 介面,我們在上圖中看到 etcdconsulfilestore ,均有實現該介面,介面的定義很簡單

type ConfigManager interface {
	Get(key string) ([]byte,error)
	List(key string) (KVPairs,error)
	Set(key string,value []byte) error
	Watch(key string,stop chan bool) <-chan *Response
}

2、修改config.go

檔案的位置: github.com/bketelsen/crypt/config/config.go ,如下圖

viper配置框架的介紹支援zookeeper的讀取和監聽

func NewStandardEtcdConfigManager(machines []string) (ConfigManager,error) 方法下面新增如下方法:

// NewStandardZookeeperConfigManager returns a new ConfigManager backed by Zookeeper.
// Data will be encrypted.
func NewStandardZookeeperConfigManager(machines []string) (ConfigManager,error) {
	store,err := zookeeper.New(machines)
	if err != nil {
		return nil,err
	}
	return NewStandardConfigManager(store)
}

func NewEtcdConfigManager(machines []string,keystore io.Reader) (ConfigManager,error) 方法下面新增如下方法:

// NewZookeeperConfigManager returns a new ConfigManager backed by zookeeper.
// Data will be encrypted.
func NewZookeeperConfigManager(machines []string,err
	}
	return NewConfigManager(store,keystore)
}

這兩個方法是初始化 ConfigManager 物件,也就是我們剛才新增的 zookeeper.go 檔案的物件

3、修改remote.go

檔案的位置: github.com/spf13/viper/remote/remote.go,如下圖

viper配置框架的介紹支援zookeeper的讀取和監聽

找到74行,用下面的程式碼替換 func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager,error) 方法

func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager,error) {
	var cm crypt.ConfigManager
	var err error

	if rp.SecretKeyring() != "" {
		var kr *os.File
		kr,err = os.Open(rp.SecretKeyring())
		if err != nil {
			return nil,err
		}
		defer kr.Close()
		switch rp.Provider() {
		case "etcd":
			cm,err = crypt.NewEtcdConfigManager([]string{rp.Endpoint()},kr)
		case "zookeeper":
			cm,err = crypt.NewZookeeperConfigManager([]string{rp.Endpoint()},kr)
		case "firestore":
			cm,err = crypt.NewFirestoreConfigManager([]string{rp.Endpoint()},kr)
		default:
			cm,err = crypt.NewConsulConfigManager([]string{rp.Endpoint()},kr)
		}
	} else {
		switch rp.Provider() {
		case "etcd":
			cm,err = crypt.NewStandardEtcdConfigManager([]string{rp.Endpoint()})
		case "zookeeper":
			cm,err = crypt.NewStandardZookeeperConfigManager([]string{rp.Endpoint()})
		case "firestore":
			cm,err = crypt.NewStandardFirestoreConfigManager([]string{rp.Endpoint()})
		default:
			cm,err = crypt.NewStandardConsulConfigManager([]string{rp.Endpoint()})
		}
	}
	if err != nil {
		return nil,err
	}
	return cm,nil
}

細心的讀者可能已經發現,其實就添加了兩個case選項:

viper配置框架的介紹支援zookeeper的讀取和監聽

4、修改viper.go

檔案的位置: github.com/spf13/viper/viper.go,如下圖

viper配置框架的介紹支援zookeeper的讀取和監聽

取+監聽zookeeper(1)\image-20200521222843002.png)

找到兩個 SupportedRemoteProviders 定義的定法,1.7.0版本的行號分別是:290,331。只要新增 zookeeper,即可

SupportedRemoteProviders = []string{"etcd","firestore","zookeeper"}

好了,修改程式碼的工作已經完了,接下來我們來測試:

測試

注意:zookeeper中已經設定了內容

set /viper/test {"appName":"test","nodes":["127.0.0.1","127.0.0.2","127.0.0.3"]}

package main

import (
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	_ "github.com/spf13/viper/remote"
	"time"
)


type config struct {
	AppName string
	Nodes []string
}

func main() {
	var waitGroup=sync.WaitGroup{}
	waitGroup.Add(1)
	readRemoteZookeeper()
	go watchRemoteZookeeper()
	waitGroup.Wait()
}

func readRemoteZookeeper() {
	viper.AddRemoteProvider("zookeeper","62.234.15.24:2181","/viper/test")
	viper.SetConfigType("json") 
	err := viper.ReadRemoteConfig()
	if err != nil {
		panic(fmt.Sprintf("read remote zookeeper error:+%v",err))
	}

	var C config
	viper.Unmarshal(&C)
	fmt.Printf("從zookeeper讀取配置內容:%+v\n",C)
}

func watchRemoteZookeeper() {
	go func() {
		for {
  //delay after each request
			time.Sleep(time.Second * 5)
			err := viper.WatchRemoteConfig()
			if err != nil {
				fmt.Errorf("unable to read remote config: %v",err)
				continue
			}
			fmt.Printf("從zookeeper讀取更新內容:appName=%s,nodes=%+v\n",viper.Get("appName"),viper.Get("nodes"))
		}
	}()
}

輸出內容:

從zookeeper讀取配置內容:{AppName:test Nodes:[127.0.0.1 127.0.0.2 127.0.0.3]}
從zookeeper讀取更新內容:appName=test,nodes=[127.0.0.1 127.0.0.2 127.0.0.3]

如果我們修改zookeeper的內容,則viper會讀取到更新後的內容:

set /viper/test {"appName":"test","127.0.0.3","127.0.0.4"]}
從zookeeper讀取更新內容:appName=test,nodes=[127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4]

結語

讓viper支援 zookeeper 並不複雜的,並且基本上不需要修改原有的方法, 這要歸結於viper用到一個非常重要的設計原則: 開閉原則 ,讀者可以自行體會。

關於viper的基本使用, github 已經有非常詳細的例子,這裡就不再贅述,如有疑問,可以私信我

到此這篇關於viper配置框架的介紹支援zookeeper的讀取和監聽的文章就介紹到這了,更多相關viper配置框架支援zookeeper的讀取和監聽內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!