2021-04-29 22:13:55 +08:00
|
|
|
# Viper remote for Nacos
|
2021-04-29 22:01:07 +08:00
|
|
|
|
|
|
|
Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs.
|
2021-04-29 22:07:32 +08:00
|
|
|
|
|
|
|
```go
|
2021-07-30 11:07:27 +08:00
|
|
|
config_viper := viper.New()
|
2021-04-29 22:07:32 +08:00
|
|
|
|
2021-04-29 22:09:10 +08:00
|
|
|
remote.SetOptions(&remote.Option{
|
|
|
|
Url: "localhost",
|
|
|
|
Port: 80,
|
|
|
|
NamespaceId: "public",
|
|
|
|
GroupName: "DEFAULT_GROUP",
|
|
|
|
Config: remote.Config{ DataId: "config_dev" },
|
|
|
|
Auth: nil,
|
|
|
|
})
|
2021-04-29 22:07:32 +08:00
|
|
|
|
2021-07-30 11:07:27 +08:00
|
|
|
remote_viper := viper.New()
|
2021-04-29 22:09:10 +08:00
|
|
|
err := remote_viper.AddRemoteProvider("nacos", "localhost", "")
|
|
|
|
remote_viper.SetConfigType("yaml")
|
2021-07-30 11:07:27 +08:00
|
|
|
err = remote_viper.ReadRemoteConfig() //sync get remote configs to remote_viper instance memory . for example , remote_viper.GetString(key)
|
2021-04-29 22:07:32 +08:00
|
|
|
|
2021-07-30 11:07:27 +08:00
|
|
|
if err == nil {
|
|
|
|
config_viper = remote_viper
|
|
|
|
fmt.Println("used remote viper")
|
|
|
|
provider := remote.NewRemoteProvider("yaml")
|
|
|
|
respChan := provider.WatchRemoteConfigOnChannel(config_viper)
|
2021-04-29 22:07:32 +08:00
|
|
|
|
2021-07-30 11:07:27 +08:00
|
|
|
go func(rc <-chan bool) {
|
|
|
|
for {
|
|
|
|
<-rc
|
|
|
|
fmt.Printf("remote async: %s", config_viper.GetString("yoyogo.application.name"))
|
|
|
|
}
|
|
|
|
}(respChan)
|
|
|
|
}
|
2021-04-29 22:07:32 +08:00
|
|
|
|
2021-07-30 11:07:27 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 30) // delay after each request
|
|
|
|
appName = config_viper.GetString("yoyogo.application.name")
|
|
|
|
fmt.Println("sync:" + appName)
|
|
|
|
}
|
|
|
|
}()
|
2021-04-29 22:07:32 +08:00
|
|
|
```
|