diff --git a/config/base_config.go b/config/base_config.go
index 0ba5bc7ef98cb30a13890b93a659c467adcbf73b..e15b58f8bf5b48caf1f8f8cae7c22f52f573bb6a 100644
--- a/config/base_config.go
+++ b/config/base_config.go
@@ -31,9 +31,7 @@ import (
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/config"
- "github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
- "github.com/apache/dubbo-go/config_center"
)
type multiConfiger interface {
@@ -52,7 +50,6 @@ type BaseConfig struct {
// application config
ApplicationConfig *ApplicationConfig `yaml:"application" json:"application,omitempty" property:"application"`
- configCenterUrl *common.URL
prefix string
fatherConfig interface{}
EventDispatcherType string `default:"direct" yaml:"event_dispatcher_type" json:"event_dispatcher_type,omitempty"`
@@ -72,72 +69,19 @@ func (c *BaseConfig) GetRemoteConfig(name string) (config *RemoteConfig, ok bool
return
}
-// startConfigCenter will start the config center.
-// it will prepare the environment
-func (c *BaseConfig) startConfigCenter() error {
- url, err := common.NewURL(c.ConfigCenterConfig.Address,
- common.WithProtocol(c.ConfigCenterConfig.Protocol), common.WithParams(c.ConfigCenterConfig.GetUrlMap()))
- if err != nil {
- return err
- }
- c.configCenterUrl = &url
- if c.prepareEnvironment() != nil {
- return perrors.WithMessagef(err, "start config center error!")
- }
- // c.fresh()
- return err
-}
-
-func (c *BaseConfig) prepareEnvironment() error {
- factory := extension.GetConfigCenterFactory(c.ConfigCenterConfig.Protocol)
- dynamicConfig, err := factory.GetDynamicConfiguration(c.configCenterUrl)
- config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig)
- if err != nil {
- logger.Errorf("Get dynamic configuration error , error message is %v", err)
- return perrors.WithStack(err)
- }
- 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)
- }
- var appGroup string
- var appContent string
- if providerConfig != nil && providerConfig.ApplicationConfig != nil &&
- reflect.ValueOf(c.fatherConfig).Elem().Type().Name() == "ProviderConfig" {
- appGroup = providerConfig.ApplicationConfig.Name
- } else if consumerConfig != nil && consumerConfig.ApplicationConfig != nil &&
- reflect.ValueOf(c.fatherConfig).Elem().Type().Name() == "ConsumerConfig" {
- appGroup = consumerConfig.ApplicationConfig.Name
- }
+func (c *BaseConfig) newURL(name string, protocol string) (common.URL, error) {
+ rc, ok := GetBaseConfig().GetRemoteConfig(name)
- if len(appGroup) != 0 {
- configFile := c.ConfigCenterConfig.AppConfigFile
- if len(configFile) == 0 {
- configFile = c.ConfigCenterConfig.ConfigFile
- }
- appContent, err = dynamicConfig.GetProperties(configFile, config_center.WithGroup(appGroup))
- if err != nil {
- return perrors.WithStack(err)
- }
+ if !ok {
+ return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + name)
}
- // global config file
- mapContent, err := dynamicConfig.Parser().Parse(content)
- if err != nil {
- return perrors.WithStack(err)
- }
- config.GetEnvInstance().UpdateExternalConfigMap(mapContent)
- // appGroup config file
- if len(appContent) != 0 {
- appMapConent, err := dynamicConfig.Parser().Parse(appContent)
- if err != nil {
- return perrors.WithStack(err)
- }
- config.GetEnvInstance().UpdateAppExternalConfigMap(appMapConent)
- }
-
- return nil
+ return common.NewURL(rc.Address,
+ common.WithUsername(rc.Username),
+ common.WithPassword(rc.Password),
+ common.WithLocation(rc.Address),
+ common.WithProtocol(protocol),
+ )
}
func getKeyPrefix(val reflect.Value) []string {
diff --git a/config/config_center_config.go b/config/config_center_config.go
index c9133dc26df0b05e3bb61df0f612d0e2914e98bb..cd0612afb6d5892ce604e03446bfb37fd923c6b8 100644
--- a/config/config_center_config.go
+++ b/config/config_center_config.go
@@ -20,6 +20,7 @@ package config
import (
"context"
"net/url"
+ "reflect"
"time"
)
@@ -28,7 +29,13 @@ import (
)
import (
+ "github.com/apache/dubbo-go/common"
+ "github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/constant"
+ "github.com/apache/dubbo-go/common/extension"
+ "github.com/apache/dubbo-go/common/logger"
+ "github.com/apache/dubbo-go/config_center"
+ perrors "github.com/pkg/errors"
)
// ConfigCenterConfig is configuration for config center
@@ -52,6 +59,7 @@ type ConfigCenterConfig struct {
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"`
+ RemoteRef string `required:"false" yaml:"remote_ref" json:"remote_ref,omitempty"`
timeout time.Duration
}
@@ -77,3 +85,73 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values {
urlMap.Set(constant.CONFIG_LOG_DIR_KEY, c.LogDir)
return urlMap
}
+
+type configCenter struct {
+}
+
+// startConfigCenter will start the config center.
+// it will prepare the environment
+func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error {
+ url, err := common.NewURL(baseConfig.ConfigCenterConfig.Address,
+ common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap()))
+ if err != nil {
+ return err
+ }
+ if b.prepareEnvironment(baseConfig, &url) != nil {
+ return perrors.WithMessagef(err, "start config center error!")
+ }
+ // c.fresh()
+ return err
+}
+
+func (b *configCenter) prepareEnvironment(baseConfig BaseConfig, configCenterUrl *common.URL) error {
+ factory := extension.GetConfigCenterFactory(baseConfig.ConfigCenterConfig.Protocol)
+ dynamicConfig, err := factory.GetDynamicConfiguration(configCenterUrl)
+ config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig)
+ if err != nil {
+ logger.Errorf("Get dynamic configuration error , error message is %v", err)
+ return perrors.WithStack(err)
+ }
+ content, err := dynamicConfig.GetProperties(baseConfig.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)
+ }
+ var appGroup string
+ var appContent string
+ if providerConfig != nil && providerConfig.ApplicationConfig != nil &&
+ reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ProviderConfig" {
+ appGroup = providerConfig.ApplicationConfig.Name
+ } else if consumerConfig != nil && consumerConfig.ApplicationConfig != nil &&
+ reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ConsumerConfig" {
+ appGroup = consumerConfig.ApplicationConfig.Name
+ }
+
+ if len(appGroup) != 0 {
+ configFile := baseConfig.ConfigCenterConfig.AppConfigFile
+ if len(configFile) == 0 {
+ configFile = baseConfig.ConfigCenterConfig.ConfigFile
+ }
+ appContent, err = dynamicConfig.GetProperties(configFile, config_center.WithGroup(appGroup))
+ if err != nil {
+ return perrors.WithStack(err)
+ }
+ }
+ // global config file
+ mapContent, err := dynamicConfig.Parser().Parse(content)
+ if err != nil {
+ return perrors.WithStack(err)
+ }
+ config.GetEnvInstance().UpdateExternalConfigMap(mapContent)
+
+ // appGroup config file
+ if len(appContent) != 0 {
+ appMapConent, err := dynamicConfig.Parser().Parse(appContent)
+ if err != nil {
+ return perrors.WithStack(err)
+ }
+ config.GetEnvInstance().UpdateAppExternalConfigMap(appMapConent)
+ }
+
+ return nil
+}
diff --git a/config/consumer_config.go b/config/consumer_config.go
index 48f29f0e70028a7c057ee3831b45afa72446f3d0..d26709c97835f89ec265837b893808aa86dec16b 100644
--- a/config/consumer_config.go
+++ b/config/consumer_config.go
@@ -41,7 +41,8 @@ import (
// ConsumerConfig is Consumer default configuration
type ConsumerConfig struct {
BaseConfig `yaml:",inline"`
- Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
+ configCenter
+ Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
// client
Connect_Timeout string `default:"100ms" yaml:"connect_timeout" json:"connect_timeout,omitempty" property:"connect_timeout"`
ConnectTimeout time.Duration
@@ -127,7 +128,7 @@ func configCenterRefreshConsumer() error {
var err error
if consumerConfig.ConfigCenterConfig != nil {
consumerConfig.SetFatherConfig(consumerConfig)
- if err = consumerConfig.startConfigCenter(); err != nil {
+ if err = consumerConfig.startConfigCenter((*consumerConfig).BaseConfig); err != nil {
return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err))
}
consumerConfig.fresh()
diff --git a/config/provider_config.go b/config/provider_config.go
index 7cd3c1e98bfb8c35abb2b414b782ec709d0a8d0d..c710e48dc233a62837b31a89828e9c612eaff093 100644
--- a/config/provider_config.go
+++ b/config/provider_config.go
@@ -37,7 +37,8 @@ import (
// ProviderConfig is the default configuration of service provider
type ProviderConfig struct {
- BaseConfig `yaml:",inline"`
+ BaseConfig `yaml:",inline"`
+ configCenter
Filter string `yaml:"filter" json:"filter,omitempty" property:"filter"`
ProxyFactory string `yaml:"proxy_factory" default:"default" json:"proxy_factory,omitempty" property:"proxy_factory"`
Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"`
@@ -101,7 +102,7 @@ func configCenterRefreshProvider() error {
// fresh it
if providerConfig.ConfigCenterConfig != nil {
providerConfig.fatherConfig = providerConfig
- if err := providerConfig.startConfigCenter(); err != nil {
+ if err := providerConfig.startConfigCenter((*providerConfig).BaseConfig); err != nil {
return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err))
}
providerConfig.fresh()