From 2e96585d2001f529946af92b49d137b973f42786 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Sun, 2 Aug 2020 22:42:40 +0800 Subject: [PATCH 01/11] refactor config center --- config/base_config.go | 76 +++++---------------------------- config/config_center_config.go | 78 ++++++++++++++++++++++++++++++++++ config/consumer_config.go | 5 ++- config/provider_config.go | 5 ++- 4 files changed, 94 insertions(+), 70 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index 0ba5bc7ef9..e15b58f8bf 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -31,9 +31,7 @@ import ( import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/config" - "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" - "github.com/apache/dubbo-go/config_center" ) type multiConfiger interface { @@ -52,7 +50,6 @@ type BaseConfig struct { // application config ApplicationConfig *ApplicationConfig `yaml:"application" json:"application,omitempty" property:"application"` - configCenterUrl *common.URL prefix string fatherConfig interface{} EventDispatcherType string `default:"direct" yaml:"event_dispatcher_type" json:"event_dispatcher_type,omitempty"` @@ -72,72 +69,19 @@ func (c *BaseConfig) GetRemoteConfig(name string) (config *RemoteConfig, ok bool return } -// startConfigCenter will start the config center. -// it will prepare the environment -func (c *BaseConfig) startConfigCenter() error { - url, err := common.NewURL(c.ConfigCenterConfig.Address, - common.WithProtocol(c.ConfigCenterConfig.Protocol), common.WithParams(c.ConfigCenterConfig.GetUrlMap())) - if err != nil { - return err - } - c.configCenterUrl = &url - if c.prepareEnvironment() != nil { - return perrors.WithMessagef(err, "start config center error!") - } - // c.fresh() - return err -} - -func (c *BaseConfig) prepareEnvironment() error { - factory := extension.GetConfigCenterFactory(c.ConfigCenterConfig.Protocol) - dynamicConfig, err := factory.GetDynamicConfiguration(c.configCenterUrl) - config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig) - if err != nil { - logger.Errorf("Get dynamic configuration error , error message is %v", err) - return perrors.WithStack(err) - } - content, err := dynamicConfig.GetProperties(c.ConfigCenterConfig.ConfigFile, config_center.WithGroup(c.ConfigCenterConfig.Group)) - if err != nil { - logger.Errorf("Get config content in dynamic configuration error , error message is %v", err) - return perrors.WithStack(err) - } - var appGroup string - var appContent string - if providerConfig != nil && providerConfig.ApplicationConfig != nil && - reflect.ValueOf(c.fatherConfig).Elem().Type().Name() == "ProviderConfig" { - appGroup = providerConfig.ApplicationConfig.Name - } else if consumerConfig != nil && consumerConfig.ApplicationConfig != nil && - reflect.ValueOf(c.fatherConfig).Elem().Type().Name() == "ConsumerConfig" { - appGroup = consumerConfig.ApplicationConfig.Name - } +func (c *BaseConfig) newURL(name string, protocol string) (common.URL, error) { + rc, ok := GetBaseConfig().GetRemoteConfig(name) - if len(appGroup) != 0 { - configFile := c.ConfigCenterConfig.AppConfigFile - if len(configFile) == 0 { - configFile = c.ConfigCenterConfig.ConfigFile - } - appContent, err = dynamicConfig.GetProperties(configFile, config_center.WithGroup(appGroup)) - if err != nil { - return perrors.WithStack(err) - } + if !ok { + return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + name) } - // global config file - mapContent, err := dynamicConfig.Parser().Parse(content) - if err != nil { - return perrors.WithStack(err) - } - config.GetEnvInstance().UpdateExternalConfigMap(mapContent) - // appGroup config file - if len(appContent) != 0 { - appMapConent, err := dynamicConfig.Parser().Parse(appContent) - if err != nil { - return perrors.WithStack(err) - } - config.GetEnvInstance().UpdateAppExternalConfigMap(appMapConent) - } - - return nil + return common.NewURL(rc.Address, + common.WithUsername(rc.Username), + common.WithPassword(rc.Password), + common.WithLocation(rc.Address), + common.WithProtocol(protocol), + ) } func getKeyPrefix(val reflect.Value) []string { diff --git a/config/config_center_config.go b/config/config_center_config.go index c9133dc26d..cd0612afb6 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -20,6 +20,7 @@ package config import ( "context" "net/url" + "reflect" "time" ) @@ -28,7 +29,13 @@ import ( ) import ( + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/common/logger" + "github.com/apache/dubbo-go/config_center" + perrors "github.com/pkg/errors" ) // ConfigCenterConfig is configuration for config center @@ -52,6 +59,7 @@ type ConfigCenterConfig struct { AppConfigFile string `default:"dubbo.properties" yaml:"app_config_file" json:"app_config_file,omitempty"` AppId string `default:"dubbo" yaml:"app_id" json:"app_id,omitempty"` TimeoutStr string `yaml:"timeout" json:"timeout,omitempty"` + RemoteRef string `required:"false" yaml:"remote_ref" json:"remote_ref,omitempty"` timeout time.Duration } @@ -77,3 +85,73 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values { urlMap.Set(constant.CONFIG_LOG_DIR_KEY, c.LogDir) return urlMap } + +type configCenter struct { +} + +// startConfigCenter will start the config center. +// it will prepare the environment +func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error { + url, err := common.NewURL(baseConfig.ConfigCenterConfig.Address, + common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) + if err != nil { + return err + } + if b.prepareEnvironment(baseConfig, &url) != nil { + return perrors.WithMessagef(err, "start config center error!") + } + // c.fresh() + return err +} + +func (b *configCenter) prepareEnvironment(baseConfig BaseConfig, configCenterUrl *common.URL) error { + factory := extension.GetConfigCenterFactory(baseConfig.ConfigCenterConfig.Protocol) + dynamicConfig, err := factory.GetDynamicConfiguration(configCenterUrl) + config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig) + if err != nil { + logger.Errorf("Get dynamic configuration error , error message is %v", err) + return perrors.WithStack(err) + } + content, err := dynamicConfig.GetProperties(baseConfig.ConfigCenterConfig.ConfigFile, config_center.WithGroup(c.ConfigCenterConfig.Group)) + if err != nil { + logger.Errorf("Get config content in dynamic configuration error , error message is %v", err) + return perrors.WithStack(err) + } + var appGroup string + var appContent string + if providerConfig != nil && providerConfig.ApplicationConfig != nil && + reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ProviderConfig" { + appGroup = providerConfig.ApplicationConfig.Name + } else if consumerConfig != nil && consumerConfig.ApplicationConfig != nil && + reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ConsumerConfig" { + appGroup = consumerConfig.ApplicationConfig.Name + } + + if len(appGroup) != 0 { + configFile := baseConfig.ConfigCenterConfig.AppConfigFile + if len(configFile) == 0 { + configFile = baseConfig.ConfigCenterConfig.ConfigFile + } + appContent, err = dynamicConfig.GetProperties(configFile, config_center.WithGroup(appGroup)) + if err != nil { + return perrors.WithStack(err) + } + } + // global config file + mapContent, err := dynamicConfig.Parser().Parse(content) + if err != nil { + return perrors.WithStack(err) + } + config.GetEnvInstance().UpdateExternalConfigMap(mapContent) + + // appGroup config file + if len(appContent) != 0 { + appMapConent, err := dynamicConfig.Parser().Parse(appContent) + if err != nil { + return perrors.WithStack(err) + } + config.GetEnvInstance().UpdateAppExternalConfigMap(appMapConent) + } + + return nil +} diff --git a/config/consumer_config.go b/config/consumer_config.go index 48f29f0e70..d26709c978 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -41,7 +41,8 @@ import ( // ConsumerConfig is Consumer default configuration type ConsumerConfig struct { BaseConfig `yaml:",inline"` - Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` + configCenter + Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` // client Connect_Timeout string `default:"100ms" yaml:"connect_timeout" json:"connect_timeout,omitempty" property:"connect_timeout"` ConnectTimeout time.Duration @@ -127,7 +128,7 @@ func configCenterRefreshConsumer() error { var err error if consumerConfig.ConfigCenterConfig != nil { consumerConfig.SetFatherConfig(consumerConfig) - if err = consumerConfig.startConfigCenter(); err != nil { + if err = consumerConfig.startConfigCenter((*consumerConfig).BaseConfig); err != nil { return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err)) } consumerConfig.fresh() diff --git a/config/provider_config.go b/config/provider_config.go index 7cd3c1e98b..c710e48dc2 100644 --- a/config/provider_config.go +++ b/config/provider_config.go @@ -37,7 +37,8 @@ import ( // ProviderConfig is the default configuration of service provider type ProviderConfig struct { - BaseConfig `yaml:",inline"` + BaseConfig `yaml:",inline"` + configCenter Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"` ProxyFactory string `yaml:"proxy_factory" default:"default" json:"proxy_factory,omitempty" property:"proxy_factory"` Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"` @@ -101,7 +102,7 @@ func configCenterRefreshProvider() error { // fresh it if providerConfig.ConfigCenterConfig != nil { providerConfig.fatherConfig = providerConfig - if err := providerConfig.startConfigCenter(); err != nil { + if err := providerConfig.startConfigCenter((*providerConfig).BaseConfig); err != nil { return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err)) } providerConfig.fresh() From 73e7bdd1402f30da2031ca91ae7977fa2199afc0 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 3 Aug 2020 22:48:43 +0800 Subject: [PATCH 02/11] refactor config center --- config/base_config.go | 2 +- config/base_config_test.go | 19 ----------- config/config_center_config.go | 15 +++++++-- config/config_center_config_test.go | 51 +++++++++++++++++++++++++++++ config/consumer_config.go | 14 ++++---- 5 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 config/config_center_config_test.go diff --git a/config/base_config.go b/config/base_config.go index e15b58f8bf..c0992006da 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -69,7 +69,7 @@ func (c *BaseConfig) GetRemoteConfig(name string) (config *RemoteConfig, ok bool return } -func (c *BaseConfig) newURL(name string, protocol string) (common.URL, error) { +func (c *BaseConfig) toURL(name string, protocol string) (common.URL, error) { rc, ok := GetBaseConfig().GetRemoteConfig(name) if !ok { diff --git a/config/base_config_test.go b/config/base_config_test.go index 6db6a8dcb8..849a9c4586 100644 --- a/config/base_config_test.go +++ b/config/base_config_test.go @@ -28,8 +28,6 @@ import ( import ( "github.com/apache/dubbo-go/common/config" - "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/config_center" _ "github.com/apache/dubbo-go/config_center/apollo" ) @@ -282,23 +280,6 @@ func TestRefreshProvider(t *testing.T) { assert.Equal(t, "20001", father.Protocols["jsonrpc1"].Port) } -func TestStartConfigCenter(t *testing.T) { - extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { - return &config_center.MockDynamicConfigurationFactory{} - }) - c := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{ - Protocol: "mock", - Address: "172.0.0.1", - Group: "dubbo", - ConfigFile: "mockDubbo.properties", - }} - err := c.startConfigCenter() - assert.NoError(t, err) - b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization") - assert.True(t, b) - assert.Equal(t, "ikurento.com", v) -} - func TestInitializeStruct(t *testing.T) { testConsumerConfig := &ConsumerConfig{} tp := reflect.TypeOf(ConsumerConfig{}) diff --git a/config/config_center_config.go b/config/config_center_config.go index cd0612afb6..a70415004c 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -89,11 +89,20 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values { type configCenter struct { } +// toURL will compatible with baseConfig.ConfigCenterConfig.Address before 1.6.0 +// After 1.6.0 will not compatible, only baseConfig.ConfigCenterConfig.RemoteRef +func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { + if len(baseConfig.ConfigCenterConfig.Address) > 0 { + return common.NewURL(baseConfig.ConfigCenterConfig.Address, + common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) + } + return baseConfig.toURL(baseConfig.ConfigCenterConfig.RemoteRef, baseConfig.ConfigCenterConfig.Protocol) +} + // startConfigCenter will start the config center. // it will prepare the environment func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error { - url, err := common.NewURL(baseConfig.ConfigCenterConfig.Address, - common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) + url, err := b.toURL(baseConfig) if err != nil { return err } @@ -112,7 +121,7 @@ func (b *configCenter) prepareEnvironment(baseConfig BaseConfig, configCenterUrl logger.Errorf("Get dynamic configuration error , error message is %v", err) return perrors.WithStack(err) } - content, err := dynamicConfig.GetProperties(baseConfig.ConfigCenterConfig.ConfigFile, config_center.WithGroup(c.ConfigCenterConfig.Group)) + content, err := dynamicConfig.GetProperties(baseConfig.ConfigCenterConfig.ConfigFile, config_center.WithGroup(baseConfig.ConfigCenterConfig.Group)) if err != nil { logger.Errorf("Get config content in dynamic configuration error , error message is %v", err) return perrors.WithStack(err) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go new file mode 100644 index 0000000000..ae653181e9 --- /dev/null +++ b/config/config_center_config_test.go @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/apache/dubbo-go/common/config" + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/config_center" +) + +func TestStartConfigCenter(t *testing.T) { + extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { + return &config_center.MockDynamicConfigurationFactory{} + }) + baseConfig := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{ + Protocol: "mock", + Address: "172.0.0.1", + Group: "dubbo", + ConfigFile: "mockDubbo.properties", + }} + + c := &configCenter{} + err := c.startConfigCenter(*baseConfig) + assert.NoError(t, err) + b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization") + assert.True(t, b) + assert.Equal(t, "ikurento.com", v) +} diff --git a/config/consumer_config.go b/config/consumer_config.go index d26709c978..1775312092 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -126,13 +126,6 @@ func ConsumerInit(confConFile string) error { func configCenterRefreshConsumer() error { //fresh it var err error - if consumerConfig.ConfigCenterConfig != nil { - consumerConfig.SetFatherConfig(consumerConfig) - if err = consumerConfig.startConfigCenter((*consumerConfig).BaseConfig); err != nil { - return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err)) - } - consumerConfig.fresh() - } if consumerConfig.Request_Timeout != "" { if consumerConfig.RequestTimeout, err = time.ParseDuration(consumerConfig.Request_Timeout); err != nil { return perrors.WithMessagef(err, "time.ParseDuration(Request_Timeout{%#v})", consumerConfig.Request_Timeout) @@ -143,5 +136,12 @@ func configCenterRefreshConsumer() error { return perrors.WithMessagef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout) } } + if consumerConfig.ConfigCenterConfig != nil { + consumerConfig.SetFatherConfig(consumerConfig) + if err = consumerConfig.startConfigCenter((*consumerConfig).BaseConfig); err != nil { + return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err)) + } + consumerConfig.fresh() + } return nil } From d5a491ee51c978e9ef24e6e56fe7acf45d8a5346 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 3 Aug 2020 23:03:20 +0800 Subject: [PATCH 03/11] add case for this feature --- config/config_center_config.go | 2 +- config/config_center_config_test.go | 45 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/config/config_center_config.go b/config/config_center_config.go index a70415004c..e423582012 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -89,7 +89,7 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values { type configCenter struct { } -// toURL will compatible with baseConfig.ConfigCenterConfig.Address before 1.6.0 +// toURL will compatible with baseConfig.ConfigCenterConfig.Address and baseConfig.ConfigCenterConfig.RemoteRef before 1.6.0 // After 1.6.0 will not compatible, only baseConfig.ConfigCenterConfig.RemoteRef func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { if len(baseConfig.ConfigCenterConfig.Address) > 0 { diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index ae653181e9..3a08e01302 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -49,3 +49,48 @@ func TestStartConfigCenter(t *testing.T) { assert.True(t, b) assert.Equal(t, "ikurento.com", v) } + +func TestStartConfigCenterWithRemoteRef(t *testing.T) { + extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { + return &config_center.MockDynamicConfigurationFactory{} + }) + m := make(map[string]*RemoteConfig) + m["mock"] = &RemoteConfig{Address: "172.0.0.1"} + baseConfig = &BaseConfig{ + Remotes: m, + ConfigCenterConfig: &ConfigCenterConfig{ + Protocol: "mock", + Group: "dubbo", + RemoteRef: "mock", + ConfigFile: "mockDubbo.properties", + }} + + c := &configCenter{} + err := c.startConfigCenter(*baseConfig) + assert.NoError(t, err) + b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization") + assert.True(t, b) + assert.Equal(t, "ikurento.com", v) + + baseConfig = nil +} + +func TestStartConfigCenterWithRemoteRefError(t *testing.T) { + extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory { + return &config_center.MockDynamicConfigurationFactory{} + }) + m := make(map[string]*RemoteConfig) + m["mock"] = &RemoteConfig{Address: "172.0.0.1"} + baseConfig := &BaseConfig{ + Remotes: m, + ConfigCenterConfig: &ConfigCenterConfig{ + Protocol: "mock", + Group: "dubbo", + RemoteRef: "mock", + ConfigFile: "mockDubbo.properties", + }} + + c := &configCenter{} + err := c.startConfigCenter(*baseConfig) + assert.Error(t, err) +} From 3e9f57a12159006bac5b848b57f2ed4dec57d58a Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Mon, 3 Aug 2020 23:08:32 +0800 Subject: [PATCH 04/11] refactor config center --- config/config_center_config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/config_center_config.go b/config/config_center_config.go index e423582012..e2943dcc5c 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -96,7 +96,11 @@ func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { return common.NewURL(baseConfig.ConfigCenterConfig.Address, common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) } - return baseConfig.toURL(baseConfig.ConfigCenterConfig.RemoteRef, baseConfig.ConfigCenterConfig.Protocol) + newURL, err := baseConfig.toURL(baseConfig.ConfigCenterConfig.RemoteRef, baseConfig.ConfigCenterConfig.Protocol) + if err == nil { + newURL.SetParams(baseConfig.ConfigCenterConfig.GetUrlMap()) + } + return newURL, err } // startConfigCenter will start the config center. From d32cbdb2d4ea6cd79eb7526e24d13442c8e68292 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Tue, 4 Aug 2020 23:12:21 +0800 Subject: [PATCH 05/11] fix review comment --- config/base_config.go | 6 +++--- config/config_center_config.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index c0992006da..3602c17508 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -69,8 +69,8 @@ func (c *BaseConfig) GetRemoteConfig(name string) (config *RemoteConfig, ok bool return } -func (c *BaseConfig) toURL(name string, protocol string) (common.URL, error) { - rc, ok := GetBaseConfig().GetRemoteConfig(name) +func (c *BaseConfig) toConfigCenterURL() (common.URL, error) { + rc, ok := GetBaseConfig().GetRemoteConfig(baseConfig.ConfigCenterConfig.RemoteRef) if !ok { return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + name) @@ -80,7 +80,7 @@ func (c *BaseConfig) toURL(name string, protocol string) (common.URL, error) { common.WithUsername(rc.Username), common.WithPassword(rc.Password), common.WithLocation(rc.Address), - common.WithProtocol(protocol), + common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), ) } diff --git a/config/config_center_config.go b/config/config_center_config.go index e2943dcc5c..514e2d9a0e 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -96,7 +96,7 @@ func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { return common.NewURL(baseConfig.ConfigCenterConfig.Address, common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) } - newURL, err := baseConfig.toURL(baseConfig.ConfigCenterConfig.RemoteRef, baseConfig.ConfigCenterConfig.Protocol) + newURL, err := baseConfig.toConfigCenterURL() if err == nil { newURL.SetParams(baseConfig.ConfigCenterConfig.GetUrlMap()) } From e0dd74ead3d66b3a11b05bcd89e1fdcfd0ac99ce Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Tue, 4 Aug 2020 23:18:45 +0800 Subject: [PATCH 06/11] fix review comment --- config/base_config.go | 16 ---------------- config/config_center_config.go | 10 +++++++++- config/remote_config.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index 3602c17508..22a0832731 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -29,7 +29,6 @@ import ( ) import ( - "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/logger" ) @@ -69,21 +68,6 @@ func (c *BaseConfig) GetRemoteConfig(name string) (config *RemoteConfig, ok bool return } -func (c *BaseConfig) toConfigCenterURL() (common.URL, error) { - rc, ok := GetBaseConfig().GetRemoteConfig(baseConfig.ConfigCenterConfig.RemoteRef) - - if !ok { - return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + name) - } - - return common.NewURL(rc.Address, - common.WithUsername(rc.Username), - common.WithPassword(rc.Password), - common.WithLocation(rc.Address), - common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), - ) -} - func getKeyPrefix(val reflect.Value) []string { var ( prefix string diff --git a/config/config_center_config.go b/config/config_center_config.go index 514e2d9a0e..e50fabd555 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -96,7 +96,15 @@ func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { return common.NewURL(baseConfig.ConfigCenterConfig.Address, common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap())) } - newURL, err := baseConfig.toConfigCenterURL() + + remoteRef := baseConfig.ConfigCenterConfig.RemoteRef + rc, ok := GetBaseConfig().GetRemoteConfig(remoteRef) + + if !ok { + return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + remoteRef) + } + + newURL, err := rc.toURL(baseConfig.ConfigCenterConfig.Protocol) if err == nil { newURL.SetParams(baseConfig.ConfigCenterConfig.GetUrlMap()) } diff --git a/config/remote_config.go b/config/remote_config.go index 5e0330c571..618313d7dc 100644 --- a/config/remote_config.go +++ b/config/remote_config.go @@ -18,6 +18,7 @@ package config import ( + "github.com/apache/dubbo-go/common" "time" ) @@ -56,3 +57,12 @@ func (rc *RemoteConfig) GetParam(key string, def string) string { } return param } + +func (rc *RemoteConfig) toURL(protocol string) (common.URL, error) { + return common.NewURL(rc.Address, + common.WithUsername(rc.Username), + common.WithPassword(rc.Password), + common.WithLocation(rc.Address), + common.WithProtocol(protocol), + ) +} From 8e6d2ac875c2c65aaecfd58c779d1b48768330e5 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Wed, 5 Aug 2020 00:32:26 +0800 Subject: [PATCH 07/11] fix review comment --- config/config_center_config.go | 4 ++-- config/config_center_config_test.go | 3 +-- config/remote_config.go | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/config_center_config.go b/config/config_center_config.go index e50fabd555..84f307d455 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -104,7 +104,7 @@ func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + remoteRef) } - newURL, err := rc.toURL(baseConfig.ConfigCenterConfig.Protocol) + newURL, err := rc.toURL() if err == nil { newURL.SetParams(baseConfig.ConfigCenterConfig.GetUrlMap()) } @@ -126,7 +126,7 @@ func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error { } func (b *configCenter) prepareEnvironment(baseConfig BaseConfig, configCenterUrl *common.URL) error { - factory := extension.GetConfigCenterFactory(baseConfig.ConfigCenterConfig.Protocol) + factory := extension.GetConfigCenterFactory(configCenterUrl.Protocol) dynamicConfig, err := factory.GetDynamicConfiguration(configCenterUrl) config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig) if err != nil { diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index 3a08e01302..05912a9fb9 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -55,11 +55,10 @@ func TestStartConfigCenterWithRemoteRef(t *testing.T) { return &config_center.MockDynamicConfigurationFactory{} }) m := make(map[string]*RemoteConfig) - m["mock"] = &RemoteConfig{Address: "172.0.0.1"} + m["mock"] = &RemoteConfig{Protocol: "mock", Address: "172.0.0.1"} baseConfig = &BaseConfig{ Remotes: m, ConfigCenterConfig: &ConfigCenterConfig{ - Protocol: "mock", Group: "dubbo", RemoteRef: "mock", ConfigFile: "mockDubbo.properties", diff --git a/config/remote_config.go b/config/remote_config.go index 618313d7dc..f1ee734aa7 100644 --- a/config/remote_config.go +++ b/config/remote_config.go @@ -31,6 +31,7 @@ import ( // so that other module, like config center, registry could reuse the config // but now, only metadata report, metadata service, service discovery use this structure type RemoteConfig struct { + Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty"` Address string `yaml:"address" json:"address,omitempty"` TimeoutStr string `default:"5s" yaml:"timeout" json:"timeout,omitempty"` Username string `yaml:"username" json:"username,omitempty" property:"username"` @@ -58,11 +59,11 @@ func (rc *RemoteConfig) GetParam(key string, def string) string { return param } -func (rc *RemoteConfig) toURL(protocol string) (common.URL, error) { +func (rc *RemoteConfig) toURL() (common.URL, error) { return common.NewURL(rc.Address, common.WithUsername(rc.Username), common.WithPassword(rc.Password), common.WithLocation(rc.Address), - common.WithProtocol(protocol), + common.WithProtocol(rc.Protocol), ) } From 3bc2938860adc5a6d55940e4a639c8b277811e6d Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Wed, 5 Aug 2020 22:38:28 +0800 Subject: [PATCH 08/11] fix review comment --- config/remote_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/remote_config.go b/config/remote_config.go index f1ee734aa7..a3f58b845a 100644 --- a/config/remote_config.go +++ b/config/remote_config.go @@ -18,11 +18,11 @@ package config import ( - "github.com/apache/dubbo-go/common" "time" ) import ( + "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/logger" ) From eb919e19d82afec1d28c660791e0f341166b725c Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Thu, 6 Aug 2020 00:41:03 +0800 Subject: [PATCH 09/11] fix review comment --- config/config_center_config.go | 6 +++--- config/config_center_config_test.go | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config/config_center_config.go b/config/config_center_config.go index 84f307d455..b0a0090ce7 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -118,21 +118,21 @@ func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error { if err != nil { return err } - if b.prepareEnvironment(baseConfig, &url) != nil { + if err = b.prepareEnvironment(baseConfig, &url); err != nil { return perrors.WithMessagef(err, "start config center error!") } // c.fresh() - return err + return nil } func (b *configCenter) prepareEnvironment(baseConfig BaseConfig, configCenterUrl *common.URL) error { factory := extension.GetConfigCenterFactory(configCenterUrl.Protocol) dynamicConfig, err := factory.GetDynamicConfiguration(configCenterUrl) - config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig) if err != nil { logger.Errorf("Get dynamic configuration error , error message is %v", err) return perrors.WithStack(err) } + config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig) content, err := dynamicConfig.GetProperties(baseConfig.ConfigCenterConfig.ConfigFile, config_center.WithGroup(baseConfig.ConfigCenterConfig.Group)) if err != nil { logger.Errorf("Get config content in dynamic configuration error , error message is %v", err) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index 05912a9fb9..0f43d01655 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -70,8 +70,6 @@ func TestStartConfigCenterWithRemoteRef(t *testing.T) { b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization") assert.True(t, b) assert.Equal(t, "ikurento.com", v) - - baseConfig = nil } func TestStartConfigCenterWithRemoteRefError(t *testing.T) { From bcffde9b63a2f52f0c4f2954d80703351d42f64b Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Thu, 6 Aug 2020 12:35:02 +0800 Subject: [PATCH 10/11] fix travis --- config/remote_config.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/config/remote_config.go b/config/remote_config.go index a3f58b845a..55380dd5a0 100644 --- a/config/remote_config.go +++ b/config/remote_config.go @@ -21,6 +21,10 @@ import ( "time" ) +import ( + perrors "github.com/pkg/errors" +) + import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/logger" @@ -31,7 +35,7 @@ import ( // so that other module, like config center, registry could reuse the config // but now, only metadata report, metadata service, service discovery use this structure type RemoteConfig struct { - Protocol string `required:"true" yaml:"protocol" json:"protocol,omitempty"` + Protocol string `yaml:"protocol" json:"protocol,omitempty"` Address string `yaml:"address" json:"address,omitempty"` TimeoutStr string `default:"5s" yaml:"timeout" json:"timeout,omitempty"` Username string `yaml:"username" json:"username,omitempty" property:"username"` @@ -60,6 +64,9 @@ func (rc *RemoteConfig) GetParam(key string, def string) string { } func (rc *RemoteConfig) toURL() (common.URL, error) { + if len(rc.Protocol) == 0 { + return common.URL{}, perrors.Errorf("Must provide protocol in RemoteConfig.") + } return common.NewURL(rc.Address, common.WithUsername(rc.Username), common.WithPassword(rc.Password), From 6300a24d968ff979fb4f3a17eb2292a7ef29ac12 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Thu, 6 Aug 2020 13:45:24 +0800 Subject: [PATCH 11/11] fix travis --- config/config_center_config.go | 2 +- config/config_center_config_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config_center_config.go b/config/config_center_config.go index b0a0090ce7..0fc4007940 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -98,7 +98,7 @@ func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) { } remoteRef := baseConfig.ConfigCenterConfig.RemoteRef - rc, ok := GetBaseConfig().GetRemoteConfig(remoteRef) + rc, ok := baseConfig.GetRemoteConfig(remoteRef) if !ok { return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + remoteRef) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index 0f43d01655..2299167bb6 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -56,7 +56,7 @@ func TestStartConfigCenterWithRemoteRef(t *testing.T) { }) m := make(map[string]*RemoteConfig) m["mock"] = &RemoteConfig{Protocol: "mock", Address: "172.0.0.1"} - baseConfig = &BaseConfig{ + baseConfig := &BaseConfig{ Remotes: m, ConfigCenterConfig: &ConfigCenterConfig{ Group: "dubbo",