diff --git a/config/config_center_config.go b/config/config_center_config.go index 8e2ec489900591fdb1873b08cf375010e794c67d..aa02124f9101cdf0131436a35d0360814cef4efa 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -40,7 +40,7 @@ type ConfigCenterConfig struct { Username string `yaml:"username" json:"username,omitempty"` Password string `yaml:"password" json:"password,omitempty"` ConfigFile string `default:"dubbo.properties" yaml:"config_file" json:"config_file,omitempty"` - Namespace string `yaml:"namespace" json:"namespace,omitempty"` + Namespace string `default:"dubbo.properties" yaml:"namespace" json:"namespace,omitempty"` AppConfigFile string `default:"dubbo.properties" yaml:"app_config_file" json:"app_config_file,omitempty"` TimeoutStr string `yaml:"timeout" json:"timeout,omitempty"` timeout time.Duration diff --git a/config_center/apollo/impl.go b/config_center/apollo/impl.go index f32cc3bbf376c0d96c702e4d3152e62cfe2058de..0b86c19c50d9ce4b763c4876cc970020ecf2edc2 100644 --- a/config_center/apollo/impl.go +++ b/config_center/apollo/impl.go @@ -60,7 +60,7 @@ func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) { appId := url.GetParam(constant.CONFIG_GROUP_KEY, DEFAULT_GROUP) namespaces := url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP)) - readyConfig := &agollo.AppConfig{ + c.appConf = &agollo.AppConfig{ AppId: appId, Cluster: configCluster, NamespaceName: namespaces, @@ -68,7 +68,7 @@ func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) { } agollo.InitCustomConfig(func() (*agollo.AppConfig, error) { - return readyConfig, nil + return c.appConf, nil }) return c, agollo.Start() @@ -124,11 +124,27 @@ func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, err for _, opt := range opts { opt(k) } - config := agollo.GetConfig(key) + /** + * 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) if config == nil { return "", errors.New(fmt.Sprintf("nothiing in namespace:%s ", key)) } - return config.GetContent(agollo.Properties), nil + return config.GetStringValue(key, ""), nil } func (c *apolloConfiguration) getAddressWithProtocolPrefix(url *common.URL) string { diff --git a/config_center/apollo/impl_test.go b/config_center/apollo/impl_test.go index f445358f116bce9c6303f9b928407f46c5ef98c2..c5d8b76a6ea86d1662a9d2999b5819a0fea1dea7 100644 --- a/config_center/apollo/impl_test.go +++ b/config_center/apollo/impl_test.go @@ -173,6 +173,15 @@ func Test_GetConfig(t *testing.T) { assert.Equal(t, "ikurento.com", mapContent["application.organization"]) } +func Test_GetConfigItem(t *testing.T) { + configuration := initMockApollo(t) + configs, err := configuration.GetConfig("application.organization") + assert.NoError(t, err) + configuration.SetParser(&parser.DefaultConfigurationParser{}) + assert.NoError(t, err) + assert.Equal(t, "ikurento.com", configs) +} + func initMockApollo(t *testing.T) *apolloConfiguration { c := &config.BaseConfig{ConfigCenterConfig: &config.ConfigCenterConfig{ Protocol: "apollo",