Skip to content

Commit

Permalink
Merge pull request #267 from zouyx/feature/apollo
Browse files Browse the repository at this point in the history
Add: Split config center GetConfig method
  • Loading branch information
fangyincheng authored Dec 9, 2019
2 parents 33b75f1 + bd7d791 commit 4d82cfe
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 36 deletions.
1 change: 1 addition & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const (
const (
CONFIG_NAMESPACE_KEY = "config.namespace"
CONFIG_GROUP_KEY = "config.group"
CONFIG_APP_ID_KEY = "config.appId"
CONFIG_CLUSTER_KEY = "config.cluster"
CONFIG_CHECK_KEY = "config.check"
CONFIG_TIMEOUT_KET = "config.timeout"
Expand Down
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
2 changes: 2 additions & 0 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ConfigCenterConfig struct {
ConfigFile string `default:"dubbo.properties" yaml:"config_file" json:"config_file,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"`
AppId string `default:"dubbo" yaml:"app_id" json:"app_id,omitempty"`
TimeoutStr string `yaml:"timeout" json:"timeout,omitempty"`
timeout time.Duration
}
Expand All @@ -62,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
}
40 changes: 16 additions & 24 deletions config_center/apollo/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) {
configAddr := c.getAddressWithProtocolPrefix(url)
configCluster := url.GetParam(constant.CONFIG_CLUSTER_KEY, "")

appId := url.GetParam(constant.CONFIG_GROUP_KEY, DEFAULT_GROUP)
appId := url.GetParam(constant.CONFIG_APP_ID_KEY, "")
namespaces := url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP))
c.appConf = &agollo.AppConfig{
AppId: appId,
Expand Down Expand Up @@ -116,32 +116,28 @@ func getNamespaceName(namespace string, configFileFormat agollo.ConfigFileFormat
return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat)
}

func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, error) {
k := &Options{}
for _, opt := range opts {
opt(k)
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) {
/**
* 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 +166,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
10 changes: 8 additions & 2 deletions config_center/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ 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 get properties file
GetProperties(string, ...Option) (string, error)

//GetRule get Router rule properties file
GetRule(string, ...Option) (string, error)

//GetInternalProperty get value by key in Default properties file(dubbo.properties)
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 4d82cfe

Please sign in to comment.