-
Notifications
You must be signed in to change notification settings - Fork 14
/
config_remote_provider.go
62 lines (55 loc) · 1.42 KB
/
config_remote_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package nacos_viper_remote
import (
"bytes"
"fmt"
"github.com/spf13/viper"
)
type ViperRemoteProvider struct {
configType string
configSet string
}
func NewRemoteProvider(configType string) *ViperRemoteProvider {
return &ViperRemoteProvider{
configType: configType,
configSet: "yoyogo.cloud.discovery.metadata"}
}
func (provider *ViperRemoteProvider) GetProvider(runtimeViper *viper.Viper) *viper.Viper {
var option *Option
err := runtimeViper.Sub(provider.configSet).Unmarshal(&option)
if err != nil {
panic(err)
return nil
}
SetOptions(option)
remote_viper := viper.New()
err = remote_viper.AddRemoteProvider("nacos", "localhost", "")
if provider.configType == "" {
provider.configType = "yaml"
}
remote_viper.SetConfigType(provider.configType)
err = remote_viper.ReadRemoteConfig()
if err == nil {
//err = remote_viper.WatchRemoteConfigOnChannel()
if err == nil {
fmt.Println("used remote viper")
return remote_viper
}
} else {
panic(err)
}
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
}