Skip to content
Snippets Groups Projects
Commit 18fefc89 authored by 邹毅贤's avatar 邹毅贤
Browse files

split config center getconfig method to getRule/getinternalproperty/getProperties

parent 23b89866
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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
}
......@@ -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...)
}
......@@ -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",
}}
......
......@@ -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 {
......
......@@ -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{
......
......@@ -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 {
......
......@@ -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)
......
......@@ -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
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment