From 66c4e16397e1a2cc73d1e2cf9fd81a49f8f4ced2 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Wed, 12 May 2021 13:56:58 +0800 Subject: [PATCH] provider with config type --- .idea/.gitignore | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/nacos-viper-remote.iml | 9 +++++++++ .idea/vcs.xml | 6 ++++++ config_remote_provider.go | 15 ++++++++++----- 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/nacos-viper-remote.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..180a0ba --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/nacos-viper-remote.iml b/.idea/nacos-viper-remote.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/nacos-viper-remote.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/config_remote_provider.go b/config_remote_provider.go index 003c6ee..3759533 100644 --- a/config_remote_provider.go +++ b/config_remote_provider.go @@ -6,17 +6,19 @@ import ( ) type ViperRemoteProvider struct { - configSet string + configType string + configSet string } -func NewRemoteProvider() *ViperRemoteProvider { +func NewRemoteProvider(configType string) *ViperRemoteProvider { return &ViperRemoteProvider{ - configSet: "yoyogo.cloud.discovery.metadata"} + configType: configType, + 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) + err := runtime_viper.Sub(provider.configSet).Unmarshal(&option) if err != nil { panic(err) return nil @@ -24,7 +26,10 @@ func (provider *ViperRemoteProvider) GetProvider(runtime_viper *viper.Viper) *vi SetOptions(option) remote_viper := viper.New() err = remote_viper.AddRemoteProvider("nacos", "localhost", "") - remote_viper.SetConfigType("yaml") + if provider.configType == "" { + provider.configType = "yaml" + } + remote_viper.SetConfigType(provider.configType) err = remote_viper.ReadRemoteConfig() if err == nil { err = remote_viper.WatchRemoteConfigOnChannel()