From 887c8e78ea18dbe935bd20b79fc18d7f3f425077 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Thu, 29 Jul 2021 19:30:03 +0800 Subject: [PATCH] notify event on remote config changed --- config_remote_provider.go | 26 ++++++++++++++++++++++---- example/main.go | 21 ++++++++++++++------- nacosprovider.go | 28 ++++++++++++++++++++++++++++ viper_remote.go | 20 +++++++++----------- 4 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 nacosprovider.go diff --git a/config_remote_provider.go b/config_remote_provider.go index 3759533..5cc84b6 100644 --- a/config_remote_provider.go +++ b/config_remote_provider.go @@ -1,6 +1,7 @@ package nacos_viper_remote import ( + "bytes" "fmt" "github.com/spf13/viper" ) @@ -16,9 +17,9 @@ func NewRemoteProvider(configType string) *ViperRemoteProvider { configSet: "yoyogo.cloud.discovery.metadata"} } -func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *viper.Viper { +func (provider *ViperRemoteProvider) GetProvider(runtimeViper *viper.Viper) *viper.Viper { var option *Option - err := runtime_viper.Sub(provider.configSet).Unmarshal(&option) + err := runtimeViper.Sub(provider.configSet).Unmarshal(&option) if err != nil { panic(err) return nil @@ -32,7 +33,7 @@ func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *vi remote_viper.SetConfigType(provider.configType) err = remote_viper.ReadRemoteConfig() if err == nil { - err = remote_viper.WatchRemoteConfigOnChannel() + //err = remote_viper.WatchRemoteConfigOnChannel() if err == nil { fmt.Println("used remote viper") return remote_viper @@ -40,5 +41,22 @@ func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *vi } else { panic(err) } - return runtime_viper + return runtimeViper +} + +func (provider *ViperRemoteProvider) WatchRemoteConfigOnChannel(remoteViper *viper.Viper) <-chan bool { + updater := make(chan bool) + + respChan, _ := viper.RemoteConfig.WatchChannel(DefaultRemoteProvider()) + go func(rc <-chan *viper.RemoteResponse) { + for { + b := <-rc + reader := bytes.NewReader(b.Value) + _ = remoteViper.ReadConfig(reader) + // configuration on changed + updater <- true + } + }(respChan) + + return updater } diff --git a/example/main.go b/example/main.go index e3b46a4..8ab844f 100644 --- a/example/main.go +++ b/example/main.go @@ -32,14 +32,21 @@ func main() { remote_viper := viper.New() err := remote_viper.AddRemoteProvider("nacos", "localhost", "") remote_viper.SetConfigType("yaml") - err = remote_viper.ReadRemoteConfig() + if err == nil { - err = remote_viper.WatchRemoteConfigOnChannel() - if err == nil { - config_viper = remote_viper - fmt.Println("used remote viper") - } + config_viper = remote_viper + fmt.Println("used remote viper") + provider := remote.NewRemoteProvider("yaml") + respChan := provider.WatchRemoteConfigOnChannel(config_viper) + + go func(rc <-chan bool) { + for { + <-rc + fmt.Printf("remote async: %s", config_viper.GetString("yoyogo.application.name")) + } + }(respChan) + } appName := config_viper.GetString("yoyogo.application.name") @@ -50,7 +57,7 @@ func main() { for { time.Sleep(time.Second * 30) // delay after each request appName = config_viper.GetString("yoyogo.application.name") - fmt.Println(appName) + fmt.Println("sync:" + appName) } }() diff --git a/nacosprovider.go b/nacosprovider.go new file mode 100644 index 0000000..dba4ba5 --- /dev/null +++ b/nacosprovider.go @@ -0,0 +1,28 @@ +package nacos_viper_remote + +type nacosRemoteProvider struct { + provider string + endpoint string + path string + secretKeyring string +} + +func DefaultRemoteProvider() *nacosRemoteProvider { + return &nacosRemoteProvider{provider: "nacos", endpoint: "localhost", path: "", secretKeyring: ""} +} + +func (rp nacosRemoteProvider) Provider() string { + return rp.provider +} + +func (rp nacosRemoteProvider) Endpoint() string { + return rp.endpoint +} + +func (rp nacosRemoteProvider) Path() string { + return rp.path +} + +func (rp nacosRemoteProvider) SecretKeyring() string { + return rp.secretKeyring +} diff --git a/viper_remote.go b/viper_remote.go index 4bad50d..19be861 100644 --- a/viper_remote.go +++ b/viper_remote.go @@ -7,17 +7,20 @@ import ( "io" ) -var nacosOptions = &Option{} +//var nacosOptions = &Option{} func SetOptions(option *Option) { - nacosOptions = option + manager, _ := NewNacosConfigManager(option) + viper.SupportedRemoteProviders = []string{"nacos"} + viper.RemoteConfig = &remoteConfigProvider{ConfigManager: manager} } type remoteConfigProvider struct { + ConfigManager *nacosConfigManager } func (rc *remoteConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) { - cmt, err := getConfigManager(rp) + cmt, err := rc.getConfigManager(rp) if err != nil { return nil, err } @@ -37,7 +40,7 @@ func (rc *remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error } func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) { - cmt, err := getConfigManager(rp) + cmt, err := rc.getConfigManager(rp) if err != nil { return nil, nil } @@ -52,15 +55,10 @@ func (rc *remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *v return nil, nil } -func getConfigManager(rp viper.RemoteProvider) (interface{}, error) { +func (rc *remoteConfigProvider) getConfigManager(rp viper.RemoteProvider) (interface{}, error) { if rp.Provider() == "nacos" { - return NewNacosConfigManager(nacosOptions) + return rc.ConfigManager, nil } else { return nil, errors.New("The Nacos configuration manager is not supported!") } } - -func init() { - viper.SupportedRemoteProviders = []string{"nacos"} - viper.RemoteConfig = &remoteConfigProvider{} -}