diff --git a/config/base_config_test.go b/config/base_config_test.go index 6dc3749e55f7efbfb1177079f613360cd0d4cc33..9452c9e6178e1c052c735ca0cb89d220f2fe9cec 100644 --- a/config/base_config_test.go +++ b/config/base_config_test.go @@ -29,6 +29,7 @@ import ( "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/config_center" + _ "github.com/apache/dubbo-go/config_center/apollo" ) func Test_refresh(t *testing.T) { @@ -490,6 +491,20 @@ func Test_startConfigCenter(t *testing.T) { assert.Equal(t, "ikurento.com", v) } +func Test_startApolloConfigCenter(t *testing.T) { + c := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{ + Protocol: "apollo", + Address: "106.12.25.204:8080", + Group: "mockDubbo", + ConfigFile: "mockDubbo.properties", + }} + err := c.startConfigCenter(context.Background()) + assert.NoError(t, err) + b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization") + assert.True(t, b) + assert.Equal(t, "ikurento.com", v) +} + func Test_initializeStruct(t *testing.T) { consumerConfig := &ConsumerConfig{} tp := reflect.TypeOf(ConsumerConfig{}) diff --git a/config_center/apollo/impl.go b/config_center/apollo/impl.go index dde30a7e38e24256c56dae0706d97c48fb36a6b9..57ada6f08a7ce4bb37913dea2410f03a46dc93f2 100644 --- a/config_center/apollo/impl.go +++ b/config_center/apollo/impl.go @@ -18,6 +18,8 @@ limitations under the License. package apollo import ( + "fmt" + "github.com/go-errors/errors" "os" "strconv" "strings" @@ -37,6 +39,7 @@ const ( apolloAddrKey = "apollo.meta" apolloClusterKey = "apollo.cluster" apolloProtocolPrefix = "http://" + apolloConfigFormat = "%s.%s" ) type apolloDynamicConfiguration struct { @@ -68,7 +71,7 @@ func newApolloDynamicConfiguration(url *common.URL) (*apolloDynamicConfiguration readyConfig := &agollo.AppConfig{ AppId: appId, Cluster: configCluster, - NamespaceName: namespace, + NamespaceName: getNamespaceName(namespace,agollo.YML), Ip: configAddr, } @@ -76,9 +79,7 @@ func newApolloDynamicConfiguration(url *common.URL) (*apolloDynamicConfiguration return readyConfig, nil }) - agollo.Start() - - return c, nil + return c, agollo.Start() } type apolloChangeListener struct { @@ -142,6 +143,10 @@ func (c *apolloDynamicConfiguration) RemoveListener(key string, listener config_ } } +func getNamespaceName(namespace string,configFileFormat agollo.ConfigFileFormat ) string{ + return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat) +} + func (c *apolloDynamicConfiguration) GetConfig(key string, opts ...config_center.Option) (string, error) { k := &config_center.Options{} for _, opt := range opts { @@ -149,20 +154,13 @@ func (c *apolloDynamicConfiguration) GetConfig(key string, opts ...config_center } group := k.Group if len(group) != 0 && c.url.GetParam(constant.CONFIG_GROUP_KEY, config_center.DEFAULT_GROUP) != group { - readyConfig := &agollo.AppConfig{ - AppId: c.appConf.AppId, - Cluster: c.appConf.Cluster, - NamespaceName: "application", - Ip: c.appConf.Ip, + namespace := c.url.GetParam(constant.CONFIG_GROUP_KEY, config_center.DEFAULT_GROUP) + fileNamespace := getNamespaceName(namespace, agollo.Properties) + config := agollo.GetConfig(fileNamespace) + if config==nil{ + return "",errors.New(fmt.Sprintf("nothiing in namespace:%s ",fileNamespace)) } - - agollo.InitCustomConfig(func() (*agollo.AppConfig, error) { - return readyConfig, nil - }) - - agollo.Start() - //Config config = ConfigService.getAppConfig(); - //return config.getProperty(key, null); + return config.GetContent(),nil } return agollo.GetStringValue(key, ""), nil }