From ba391688f0c5a34b0382e6b3b0633180fb1e3eff Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Sun, 24 Oct 2021 02:28:08 +0800 Subject: [PATCH 1/5] add:apollo config center --- common/constant/key.go | 3 ++ config/config_center_config.go | 30 +++++++------------- config/config_center_config_test.go | 34 +++++++++++++++++++++++ config/testdata/config/center/apollo.yaml | 8 ++++++ config_center/apollo/impl.go | 28 +++++-------------- go.mod | 3 ++ 6 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 config/config_center_config_test.go create mode 100644 config/testdata/config/center/apollo.yaml diff --git a/common/constant/key.go b/common/constant/key.go index 3160f92a1e..236fcb8190 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -174,6 +174,9 @@ const ( CONFIG_LOG_DIR_KEY = "logDir" CONFIG_VERSION_KEY = "configVersion" COMPATIBLE_CONFIG_KEY = "compatible_config" + CONFIG_SECRET_KEY = "secret" + CONFIG_BACKUP_CONFIG_KEY = "isBackupConfig" + CONFIG_BACKUP_CONFIG_PATH_KEY = "backupConfigPath" ) const ( diff --git a/config/config_center_config.go b/config/config_center_config.go index 6ac615f9b5..025ca0eb2c 100644 --- a/config/config_center_config.go +++ b/config/config_center_config.go @@ -49,25 +49,16 @@ import ( // // CenterConfig has currently supported Zookeeper, Nacos, Etcd, Consul, Apollo type CenterConfig struct { - Protocol string `validate:"required" yaml:"protocol" json:"protocol,omitempty"` - Address string `validate:"required" yaml:"address" json:"address,omitempty"` - DataId string `validate:"required" yaml:"data-id" json:"data-id,omitempty"` - Cluster string `yaml:"cluster" json:"cluster,omitempty"` - Group string `default:"dubbo" yaml:"group" json:"group,omitempty"` - Username string `yaml:"username" json:"username,omitempty"` - Password string `yaml:"password" json:"password,omitempty"` - // Deprecated - LogDir string `yaml:"log-dir" json:"log-dir,omitempty"` - // Deprecated - ConfigFile string `default:"dubbo.properties" yaml:"config-file" json:"config-file,omitempty"` - Namespace string `default:"dubbo" yaml:"namespace" json:"namespace,omitempty"` - // Deprecated - AppConfigFile string `default:"dubbo.properties" yaml:"app-config-file" json:"app-config-file,omitempty"` - // Deprecated - AppID string `default:"dubbo" yaml:"app-id" json:"app-id,omitempty"` - Timeout string `default:"10s" yaml:"timeout" json:"timeout,omitempty"` - // Deprecated - RemoteRef string `required:"false" yaml:"remote-ref" json:"remote-ref,omitempty"` + Protocol string `validate:"required" yaml:"protocol" json:"protocol,omitempty"` + Address string `validate:"required" yaml:"address" json:"address,omitempty"` + DataId string `yaml:"data-id" json:"data-id,omitempty"` + Cluster string `yaml:"cluster" json:"cluster,omitempty"` + Group string `default:"dubbo" yaml:"group" json:"group,omitempty"` + Username string `yaml:"username" json:"username,omitempty"` + Password string `yaml:"password" json:"password,omitempty"` + Namespace string `default:"dubbo" yaml:"namespace" json:"namespace,omitempty"` + AppID string `default:"dubbo" yaml:"app-id" json:"app-id,omitempty"` + Timeout string `default:"10s" yaml:"timeout" json:"timeout,omitempty"` Params map[string]string `yaml:"params" json:"parameters,omitempty"` } @@ -101,7 +92,6 @@ func (c *CenterConfig) GetUrlMap() url.Values { 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) - urlMap.Set(constant.CONFIG_LOG_DIR_KEY, c.LogDir) urlMap.Set(constant.CONFIG_USERNAME_KEY, c.Username) urlMap.Set(constant.CONFIG_PASSWORD_KEY, c.Password) urlMap.Set(constant.CONFIG_TIMEOUT_KEY, c.Timeout) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go new file mode 100644 index 0000000000..46f39d88b5 --- /dev/null +++ b/config/config_center_config_test.go @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + _ "dubbo.apache.org/dubbo-go/v3/config_center/apollo" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestApolloConfigCenterConfig(t *testing.T) { + + err := Load(WithPath("./testdata/config/center/apollo.yaml")) + assert.Nil(t, err) + + registries := rootConfig.Registries + assert.NotNil(t, registries) +} + diff --git a/config/testdata/config/center/apollo.yaml b/config/testdata/config/center/apollo.yaml new file mode 100644 index 0000000000..0b10e9e793 --- /dev/null +++ b/config/testdata/config/center/apollo.yaml @@ -0,0 +1,8 @@ +dubbo: + config-center: + address: apollo://106.54.227.205:8080 + cluster: dev + namespace: dubbogo.yaml + app-id: dubbo-go + params: + secret: 3a64cf5cd9ac4fcd94eb045de433689c \ No newline at end of file diff --git a/config_center/apollo/impl.go b/config_center/apollo/impl.go index 6594a92bb7..9df8fd7b34 100644 --- a/config_center/apollo/impl.go +++ b/config_center/apollo/impl.go @@ -30,7 +30,6 @@ import ( perrors "github.com/pkg/errors" "github.com/zouyx/agollo/v3" - agolloConstant "github.com/zouyx/agollo/v3/constant" "github.com/zouyx/agollo/v3/env/config" ) @@ -43,7 +42,6 @@ import ( const ( apolloProtocolPrefix = "http://" - apolloConfigFormat = "%s%s" ) type apolloConfiguration struct { @@ -59,22 +57,18 @@ 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_APP_ID_KEY, "") - namespaces := getProperties(url.GetParam(constant.CONFIG_NAMESPACE_KEY, cc.DEFAULT_GROUP)) c.appConf = &config.AppConfig{ - AppID: appId, - Cluster: configCluster, - NamespaceName: namespaces, - IP: configAddr, + AppID: url.GetParam(constant.CONFIG_APP_ID_KEY, ""), + Cluster: url.GetParam(constant.CONFIG_CLUSTER_KEY, ""), + NamespaceName: url.GetParam(constant.CONFIG_NAMESPACE_KEY, cc.DEFAULT_GROUP), + IP: c.getAddressWithProtocolPrefix(url), + Secret: url.GetParam(constant.CONFIG_SECRET_KEY, ""), + IsBackupConfig: url.GetParamBool(constant.CONFIG_BACKUP_CONFIG_KEY, true), + BackupConfigPath: url.GetParam(constant.CONFIG_BACKUP_CONFIG_PATH_KEY, ""), } - agollo.InitCustomConfig(func() (*config.AppConfig, error) { return c.appConf, nil }) - return c, agollo.Start() } @@ -102,14 +96,6 @@ func (c *apolloConfiguration) RemoveListener(key string, listener cc.Configurati } } -func getProperties(namespace string) string { - return getNamespaceName(namespace, agolloConstant.YAML) -} - -func getNamespaceName(namespace string, configFileFormat agolloConstant.ConfigFileFormat) string { - return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat) -} - func (c *apolloConfiguration) GetInternalProperty(key string, opts ...cc.Option) (string, error) { newConfig := agollo.GetConfig(c.appConf.NamespaceName) if newConfig == nil { diff --git a/go.mod b/go.mod index 3804e96485..18c71740a5 100644 --- a/go.mod +++ b/go.mod @@ -51,3 +51,6 @@ require ( k8s.io/apimachinery v0.16.9 k8s.io/client-go v0.16.9 ) +replace ( + //github.com/zouyx/agollo/v3 v3.4.5 => github.com/apolloconfig/agollo/v4 latest +) From ee96d12998fbd04dce45643fb28f8e5c5d1ce9d4 Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Sun, 24 Oct 2021 10:36:29 +0800 Subject: [PATCH 2/5] fmt --- config/config_center_config_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index 46f39d88b5..3307e381c3 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -31,4 +31,3 @@ func TestApolloConfigCenterConfig(t *testing.T) { registries := rootConfig.Registries assert.NotNil(t, registries) } - From 9eff8e601172c13189b2cab392c7034e91f263ee Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Sun, 24 Oct 2021 10:44:39 +0800 Subject: [PATCH 3/5] fmt --- common/constant/key.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/common/constant/key.go b/common/constant/key.go index 236fcb8190..5aeb9701da 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -164,18 +164,18 @@ const ( ) const ( - CONFIG_NAMESPACE_KEY = "namespace" - CONFIG_GROUP_KEY = "group" - CONFIG_APP_ID_KEY = "appId" - CONFIG_CLUSTER_KEY = "cluster" - CONFIG_TIMEOUT_KEY = "timeout" - CONFIG_USERNAME_KEY = "username" - CONFIG_PASSWORD_KEY = "password" - CONFIG_LOG_DIR_KEY = "logDir" - CONFIG_VERSION_KEY = "configVersion" - COMPATIBLE_CONFIG_KEY = "compatible_config" - CONFIG_SECRET_KEY = "secret" - CONFIG_BACKUP_CONFIG_KEY = "isBackupConfig" + CONFIG_NAMESPACE_KEY = "namespace" + CONFIG_GROUP_KEY = "group" + CONFIG_APP_ID_KEY = "appId" + CONFIG_CLUSTER_KEY = "cluster" + CONFIG_TIMEOUT_KEY = "timeout" + CONFIG_USERNAME_KEY = "username" + CONFIG_PASSWORD_KEY = "password" + CONFIG_LOG_DIR_KEY = "logDir" + CONFIG_VERSION_KEY = "configVersion" + COMPATIBLE_CONFIG_KEY = "compatible_config" + CONFIG_SECRET_KEY = "secret" + CONFIG_BACKUP_CONFIG_KEY = "isBackupConfig" CONFIG_BACKUP_CONFIG_PATH_KEY = "backupConfigPath" ) From 10713bed16a57985cdbc297b89914594df536fb7 Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Sun, 24 Oct 2021 11:55:53 +0800 Subject: [PATCH 4/5] fix:apollo namespace --- config_center/apollo/impl_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config_center/apollo/impl_test.go b/config_center/apollo/impl_test.go index e96ce8d9f3..0a0ab62af1 100644 --- a/config_center/apollo/impl_test.go +++ b/config_center/apollo/impl_test.go @@ -155,7 +155,7 @@ func initMockApollo(t *testing.T) *apolloConfiguration { Address: "106.12.25.204:8080", AppID: "testApplication_yang", Cluster: "dev", - Namespace: "mockDubbogo", + Namespace: "mockDubbogo.yaml", }} apollo := initApollo() apolloUrl := strings.ReplaceAll(apollo.URL, "http", "apollo") From daffbf6ee88336b8be54d8214da69b5ced2727d2 Mon Sep 17 00:00:00 2001 From: zhaoyunxing Date: Sun, 24 Oct 2021 14:14:18 +0800 Subject: [PATCH 5/5] fmt --- config/config_center_config_test.go | 8 ++++++-- go.mod | 5 +---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/config/config_center_config_test.go b/config/config_center_config_test.go index 3307e381c3..dca8632fc8 100644 --- a/config/config_center_config_test.go +++ b/config/config_center_config_test.go @@ -18,10 +18,14 @@ package config import ( - _ "dubbo.apache.org/dubbo-go/v3/config_center/apollo" - "github.com/stretchr/testify/assert" "testing" ) +import ( + "github.com/stretchr/testify/assert" +) +import ( + _ "dubbo.apache.org/dubbo-go/v3/config_center/apollo" +) func TestApolloConfigCenterConfig(t *testing.T) { diff --git a/go.mod b/go.mod index 18c71740a5..f5ef5bd5b5 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,4 @@ require ( k8s.io/api v0.16.9 k8s.io/apimachinery v0.16.9 k8s.io/client-go v0.16.9 -) -replace ( - //github.com/zouyx/agollo/v3 v3.4.5 => github.com/apolloconfig/agollo/v4 latest -) +) \ No newline at end of file