Skip to content

Commit

Permalink
split config center getconfig method to getRule/getinternalproperty/g…
Browse files Browse the repository at this point in the history
…etProperties
  • Loading branch information
zouyx committed Nov 27, 2019
1 parent c30bba3 commit c85abd2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 32 deletions.
4 changes: 2 additions & 2 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
36 changes: 16 additions & 20 deletions config_center/apollo/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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...)
}
6 changes: 3 additions & 3 deletions config_center/apollo/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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",
}}
Expand Down
5 changes: 3 additions & 2 deletions config_center/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions config_center/mock_dynamic_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 7 additions & 3 deletions config_center/zookeeper/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion config_center/zookeeper/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion registry/base_configuration_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c85abd2

Please sign in to comment.