From c85abd2cf1bd8c3952818e2a56f33744247a46c6 Mon Sep 17 00:00:00 2001 From: Joe Zou Date: Wed, 27 Nov 2019 18:05:34 +0800 Subject: [PATCH] split config center getconfig method to getRule/getinternalproperty/getProperties --- config/base_config.go | 4 +-- config/config_center_config.go | 1 + config_center/apollo/impl.go | 36 +++++++++++-------------- config_center/apollo/impl_test.go | 6 ++--- config_center/dynamic_configuration.go | 5 ++-- config_center/mock_dynamic_config.go | 12 +++++++++ config_center/zookeeper/impl.go | 10 ++++--- config_center/zookeeper/impl_test.go | 2 +- registry/base_configuration_listener.go | 2 +- 9 files changed, 46 insertions(+), 32 deletions(-) diff --git a/config/base_config.go b/config/base_config.go index 6678e7c681..64418f0a6d 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -68,7 +68,7 @@ func (c *BaseConfig) prepareEnvironment() error { logger.Errorf("Get dynamic configuration error , error message is %v", err) return perrors.WithStack(err) } - content, err := dynamicConfig.GetConfig(c.ConfigCenterConfig.ConfigFile, config_center.WithGroup(c.ConfigCenterConfig.Group)) + 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) @@ -88,7 +88,7 @@ func (c *BaseConfig) prepareEnvironment() error { if len(configFile) == 0 { configFile = c.ConfigCenterConfig.ConfigFile } - appContent, err = dynamicConfig.GetConfig(configFile, config_center.WithGroup(appGroup)) + appContent, err = dynamicConfig.GetProperties(configFile, config_center.WithGroup(appGroup)) } //global config file mapContent, err := dynamicConfig.Parser().Parse(content) diff --git a/config/config_center_config.go b/config/config_center_config.go index b10d658ecf..013d23946a 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -63,5 +63,6 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values { urlMap.Set(constant.CONFIG_NAMESPACE_KEY, c.Namespace) urlMap.Set(constant.CONFIG_GROUP_KEY, c.Group) urlMap.Set(constant.CONFIG_CLUSTER_KEY, c.Cluster) + urlMap.Set(constant.CONFIG_APP_ID_KEY, c.AppId) return urlMap } diff --git a/config_center/apollo/impl.go b/config_center/apollo/impl.go index ffe959e821..a3397b6f19 100644 --- a/config_center/apollo/impl.go +++ b/config_center/apollo/impl.go @@ -116,7 +116,19 @@ func getNamespaceName(namespace string, configFileFormat agollo.ConfigFileFormat return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat) } -func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, error) { +func (c *apolloConfiguration) GetInternalProperty(key string, opts ...Option) (string, error) { + config := agollo.GetConfig(c.appConf.NamespaceName) + if config == nil { + return "", errors.New(fmt.Sprintf("nothing in namespace:%s ", key)) + } + return config.GetStringValue(key, ""), nil +} + +func (c *apolloConfiguration) GetRule(key string, opts ...Option) (string, error) { + return c.GetInternalProperty(key, opts...) +} + +func (c *apolloConfiguration) GetProperties(key string, opts ...Option) (string, error) { k := &Options{} for _, opt := range opts { opt(k) @@ -125,23 +137,11 @@ func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, err * when group is not null, we are getting startup configs(config file) from Config Center, for example: * key=dubbo.propertie */ - if len(k.Group) != 0 { - config := agollo.GetConfig(key) - if config == nil { - return "", errors.New(fmt.Sprintf("nothiing in namespace:%s ", key)) - } - return config.GetContent(agollo.Properties), nil - } - - /** - * when group is null, we are fetching governance rules(config item) from Config Center, for example: - * namespace=use default, key =application.organization - */ - config := agollo.GetConfig(c.appConf.NamespaceName) + config := agollo.GetConfig(key) if config == nil { - return "", errors.New(fmt.Sprintf("nothiing in namespace:%s ", key)) + return "", errors.New(fmt.Sprintf("nothing in namespace:%s ", key)) } - return config.GetStringValue(key, ""), nil + return config.GetContent(agollo.Properties), nil } func (c *apolloConfiguration) getAddressWithProtocolPrefix(url *common.URL) string { @@ -170,7 +170,3 @@ func (c *apolloConfiguration) Parser() parser.ConfigurationParser { func (c *apolloConfiguration) SetParser(p parser.ConfigurationParser) { c.parser = p } - -func (c *apolloConfiguration) GetConfigs(key string, opts ...Option) (string, error) { - return c.GetConfig(key, opts...) -} diff --git a/config_center/apollo/impl_test.go b/config_center/apollo/impl_test.go index 2bb8b0ad69..e898be91ee 100644 --- a/config_center/apollo/impl_test.go +++ b/config_center/apollo/impl_test.go @@ -165,7 +165,7 @@ func runMockConfigServer(handlerMap map[string]func(http.ResponseWriter, *http.R func Test_GetConfig(t *testing.T) { configuration := initMockApollo(t) - configs, err := configuration.GetConfig(mockNamespace, config_center.WithGroup("dubbo")) + configs, err := configuration.GetProperties(mockNamespace, config_center.WithGroup("dubbo")) assert.NoError(t, err) configuration.SetParser(&parser.DefaultConfigurationParser{}) mapContent, err := configuration.Parser().Parse(configs) @@ -175,7 +175,7 @@ func Test_GetConfig(t *testing.T) { func Test_GetConfigItem(t *testing.T) { configuration := initMockApollo(t) - configs, err := configuration.GetConfig("application.organization") + configs, err := configuration.GetInternalProperty("application.organization") assert.NoError(t, err) configuration.SetParser(&parser.DefaultConfigurationParser{}) assert.NoError(t, err) @@ -186,7 +186,7 @@ func initMockApollo(t *testing.T) *apolloConfiguration { c := &config.BaseConfig{ConfigCenterConfig: &config.ConfigCenterConfig{ Protocol: "apollo", Address: "106.12.25.204:8080", - Group: "testApplication_yang", + AppId: "testApplication_yang", Cluster: "dev", Namespace: "mockDubbog.properties", }} diff --git a/config_center/dynamic_configuration.go b/config_center/dynamic_configuration.go index 1028b26d96..21e785cf71 100644 --- a/config_center/dynamic_configuration.go +++ b/config_center/dynamic_configuration.go @@ -36,8 +36,9 @@ type DynamicConfiguration interface { SetParser(parser.ConfigurationParser) AddListener(string, ConfigurationListener, ...Option) RemoveListener(string, ConfigurationListener, ...Option) - GetConfig(string, ...Option) (string, error) - GetConfigs(string, ...Option) (string, error) + GetProperties(string, ...Option) (string, error) + GetRule(string, ...Option) (string, error) + GetInternalProperty(string, ...Option) (string, error) } type Options struct { diff --git a/config_center/mock_dynamic_config.go b/config_center/mock_dynamic_config.go index 47b509231d..79c7c17194 100644 --- a/config_center/mock_dynamic_config.go +++ b/config_center/mock_dynamic_config.go @@ -108,6 +108,18 @@ func (c *MockDynamicConfiguration) Parser() parser.ConfigurationParser { func (c *MockDynamicConfiguration) SetParser(p parser.ConfigurationParser) { c.parser = p } +func (c *MockDynamicConfiguration) GetProperties(key string, opts ...Option) (string, error) { + return c.content, nil +} + +//For zookeeper, getConfig and getConfigs have the same meaning. +func (c *MockDynamicConfiguration) GetInternalProperty(key string, opts ...Option) (string, error) { + return c.GetProperties(key, opts...) +} + +func (c *MockDynamicConfiguration) GetRule(key string, opts ...Option) (string, error) { + return c.GetProperties(key, opts...) +} func (c *MockDynamicConfiguration) MockServiceConfigEvent() { config := &parser.ConfiguratorConfig{ diff --git a/config_center/zookeeper/impl.go b/config_center/zookeeper/impl.go index 84e4b54e23..504d491058 100644 --- a/config_center/zookeeper/impl.go +++ b/config_center/zookeeper/impl.go @@ -109,7 +109,7 @@ func (c *zookeeperDynamicConfiguration) RemoveListener(key string, listener conf c.cacheListener.RemoveListener(key, listener) } -func (c *zookeeperDynamicConfiguration) GetConfig(key string, opts ...config_center.Option) (string, error) { +func (c *zookeeperDynamicConfiguration) GetProperties(key string, opts ...config_center.Option) (string, error) { tmpOpts := &config_center.Options{} for _, opt := range opts { @@ -141,8 +141,12 @@ func (c *zookeeperDynamicConfiguration) GetConfig(key string, opts ...config_cen } //For zookeeper, getConfig and getConfigs have the same meaning. -func (c *zookeeperDynamicConfiguration) GetConfigs(key string, opts ...config_center.Option) (string, error) { - return c.GetConfig(key, opts...) +func (c *zookeeperDynamicConfiguration) GetInternalProperty(key string, opts ...config_center.Option) (string, error) { + return c.GetProperties(key, opts...) +} + +func (c *zookeeperDynamicConfiguration) GetRule(key string, opts ...config_center.Option) (string, error) { + return c.GetProperties(key, opts...) } func (c *zookeeperDynamicConfiguration) Parser() parser.ConfigurationParser { diff --git a/config_center/zookeeper/impl_test.go b/config_center/zookeeper/impl_test.go index 2f620457f7..e614009faa 100644 --- a/config_center/zookeeper/impl_test.go +++ b/config_center/zookeeper/impl_test.go @@ -81,7 +81,7 @@ func initZkData(group string, t *testing.T) (*zk.TestCluster, *zookeeperDynamicC func Test_GetConfig(t *testing.T) { ts, reg := initZkData("dubbo", t) defer ts.Stop() - configs, err := reg.GetConfig("dubbo.properties", config_center.WithGroup("dubbo")) + configs, err := reg.GetProperties("dubbo.properties", config_center.WithGroup("dubbo")) assert.NoError(t, err) m, err := reg.Parser().Parse(configs) assert.NoError(t, err) diff --git a/registry/base_configuration_listener.go b/registry/base_configuration_listener.go index 925baa2198..056a93aaff 100644 --- a/registry/base_configuration_listener.go +++ b/registry/base_configuration_listener.go @@ -47,7 +47,7 @@ func (bcl *BaseConfigurationListener) InitWith(key string, listener config_cente } bcl.defaultConfiguratorFunc = f bcl.dynamicConfiguration.AddListener(key, listener) - if rawConfig, err := bcl.dynamicConfiguration.GetConfig(key, config_center.WithGroup(constant.DUBBO)); err != nil { + if rawConfig, err := bcl.dynamicConfiguration.GetInternalProperty(key, config_center.WithGroup(constant.DUBBO)); err != nil { //set configurators to empty bcl.configurators = []config_center.Configurator{} return