viper provider
This commit is contained in:
parent
bcd01f97cc
commit
6d316c475f
39
config_remote_provider.go
Normal file
39
config_remote_provider.go
Normal file
@ -0,0 +1,39 @@
|
||||
package nacos_viper_remote
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ViperRemoteProvider struct {
|
||||
configSet string
|
||||
}
|
||||
|
||||
func NewRemoteProvider() *ViperRemoteProvider {
|
||||
return &ViperRemoteProvider{
|
||||
configSet: "yoyogo.cloud.discovery.metadata"}
|
||||
}
|
||||
|
||||
func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *viper.Viper {
|
||||
var option *Option
|
||||
err := runtime_viper.Sub("yoyogo.cloud.discovery.metadata").Unmarshal(&option)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil
|
||||
}
|
||||
SetOptions(option)
|
||||
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 {
|
||||
fmt.Println("used remote viper")
|
||||
return remote_viper
|
||||
}
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
return runtime_viper
|
||||
}
|
Loading…
Reference in New Issue
Block a user