diff --git a/config_center/apollo/factory.go b/config_center/apollo/factory.go index a1c1409aa59f089150ff3ec8e4a898a1cd58a89a..14f9bf369113bb3d5ce28f70c9612c94f5866618 100644 --- a/config_center/apollo/factory.go +++ b/config_center/apollo/factory.go @@ -24,24 +24,29 @@ import ( import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/config_center" + . "github.com/apache/dubbo-go/config_center" "github.com/apache/dubbo-go/config_center/parser" ) func init() { - extension.SetConfigCenterFactory("apollo", func() config_center.DynamicConfigurationFactory { return &apolloDynamicConfigurationFactory{} }) + extension.SetConfigCenterFactory("apollo", createDynamicConfigurationFactory) } -type apolloDynamicConfigurationFactory struct { +func createDynamicConfigurationFactory() DynamicConfigurationFactory { + return &apolloConfigurationFactory{} } -var once sync.Once -var dynamicConfiguration *apolloDynamicConfiguration +type apolloConfigurationFactory struct{} -func (f *apolloDynamicConfigurationFactory) GetDynamicConfiguration(url *common.URL) (config_center.DynamicConfiguration, error) { +var ( + once sync.Once + dynamicConfiguration *apolloConfiguration +) + +func (f *apolloConfigurationFactory) GetDynamicConfiguration(url *common.URL) (DynamicConfiguration, error) { var err error once.Do(func() { - dynamicConfiguration, err = newApolloDynamicConfiguration(url) + dynamicConfiguration, err = newApolloConfiguration(url) }) if err != nil { return nil, err diff --git a/config_center/apollo/impl.go b/config_center/apollo/impl.go index 545294e192791b339acd3b12f39c715363c4a84b..94fb34760f67d288e8d40acf02eb8dd5ab691685 100644 --- a/config_center/apollo/impl.go +++ b/config_center/apollo/impl.go @@ -26,7 +26,7 @@ import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" - "github.com/apache/dubbo-go/config_center" + . "github.com/apache/dubbo-go/config_center" "github.com/apache/dubbo-go/config_center/parser" "github.com/apache/dubbo-go/remoting" @@ -38,7 +38,7 @@ const ( apolloConfigFormat = "%s.%s" ) -type apolloDynamicConfiguration struct { +type apolloConfiguration struct { url *common.URL listeners sync.Map @@ -46,15 +46,15 @@ type apolloDynamicConfiguration struct { parser parser.ConfigurationParser } -func newApolloDynamicConfiguration(url *common.URL) (*apolloDynamicConfiguration, error) { - c := &apolloDynamicConfiguration{ +func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) { + c := &apolloConfiguration{ url: url, } configAddr := c.getAddressWithProtocolPrefix(url) configCluster := url.GetParam(constant.CONFIG_CLUSTER_KEY, "") - appId := url.GetParam(constant.CONFIG_GROUP_KEY, config_center.DEFAULT_GROUP) - namespaces := url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(config_center.DEFAULT_GROUP)) + appId := url.GetParam(constant.CONFIG_GROUP_KEY, DEFAULT_GROUP) + namespaces := url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP)) readyConfig := &agollo.AppConfig{ AppId: appId, Cluster: configCluster, @@ -82,8 +82,8 @@ func getChangeType(change agollo.ConfigChangeType) remoting.EventType { } } -func (c *apolloDynamicConfiguration) AddListener(key string, listener config_center.ConfigurationListener, opts ...config_center.Option) { - k := &config_center.Options{} +func (c *apolloConfiguration) AddListener(key string, listener ConfigurationListener, opts ...Option) { + k := &Options{} for _, opt := range opts { opt(k) } @@ -93,8 +93,8 @@ func (c *apolloDynamicConfiguration) AddListener(key string, listener config_cen l.(*apolloListener).AddListener(listener) } -func (c *apolloDynamicConfiguration) RemoveListener(key string, listener config_center.ConfigurationListener, opts ...config_center.Option) { - k := &config_center.Options{} +func (c *apolloConfiguration) RemoveListener(key string, listener ConfigurationListener, opts ...Option) { + k := &Options{} for _, opt := range opts { opt(k) } @@ -114,12 +114,12 @@ func getNamespaceName(namespace string, configFileFormat agollo.ConfigFileFormat return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat) } -func (c *apolloDynamicConfiguration) GetConfig(key string, opts ...config_center.Option) (string, error) { - k := &config_center.Options{} +func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, error) { + k := &Options{} for _, opt := range opts { opt(k) } - namespace := c.url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(config_center.DEFAULT_GROUP)) + namespace := c.url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP)) config := agollo.GetConfig(namespace) if config == nil { return "", errors.New(fmt.Sprintf("nothiing in namespace:%s ", namespace)) @@ -127,7 +127,7 @@ func (c *apolloDynamicConfiguration) GetConfig(key string, opts ...config_center return config.GetContent(agollo.Properties), nil } -func (c *apolloDynamicConfiguration) getAddressWithProtocolPrefix(url *common.URL) string { +func (c *apolloConfiguration) getAddressWithProtocolPrefix(url *common.URL) string { address := url.Location converted := address if len(address) != 0 { @@ -145,13 +145,13 @@ func (c *apolloDynamicConfiguration) getAddressWithProtocolPrefix(url *common.UR return converted } -func (c *apolloDynamicConfiguration) Parser() parser.ConfigurationParser { +func (c *apolloConfiguration) Parser() parser.ConfigurationParser { return c.parser } -func (c *apolloDynamicConfiguration) SetParser(p parser.ConfigurationParser) { +func (c *apolloConfiguration) SetParser(p parser.ConfigurationParser) { c.parser = p } -func (c *apolloDynamicConfiguration) GetConfigs(key string, opts ...config_center.Option) (string, error) { +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 571de8b0b691699c473b2bee8b06c91e63445481..2767521ab06c7d38654aed33c7d38e89d5474182 100644 --- a/config_center/apollo/impl_test.go +++ b/config_center/apollo/impl_test.go @@ -100,7 +100,7 @@ func Test_GetConfig(t *testing.T) { assert.Equal(t, "ikurento.com", mapContent["application.organization"]) } -func initMockApollo(t *testing.T) *apolloDynamicConfiguration { +func initMockApollo(t *testing.T) *apolloConfiguration { c := &config.BaseConfig{ConfigCenterConfig: &config.ConfigCenterConfig{ Protocol: "apollo", Address: "106.12.25.204:8080", @@ -112,7 +112,7 @@ func initMockApollo(t *testing.T) *apolloDynamicConfiguration { apolloUrl := strings.ReplaceAll(apollo.URL, "http", "apollo") url, err := common.NewURL(context.TODO(), apolloUrl, common.WithParams(c.ConfigCenterConfig.GetUrlMap())) assert.NoError(t, err) - configuration, err := newApolloDynamicConfiguration(&url) + configuration, err := newApolloConfiguration(&url) assert.NoError(t, err) return configuration }