Skip to content

Commit

Permalink
[apache#1559]: adjust the startup process of the configuration center (
Browse files Browse the repository at this point in the history
…apache#1560)

* [apache#1559]:
1. fix the startup process of the configuration center(remove error log)
2. refactor zk registry listener

* delete redundant dynamic configuration center cache

Co-authored-by: dongjianhui03 <dongjianhui03@meituan.com>
  • Loading branch information
2 people authored and binbin0325 committed Nov 6, 2021
1 parent 79c5dfb commit 5fe7d87
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
5 changes: 3 additions & 2 deletions cluster/router/v3router/router_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/cluster/router"
"dubbo.apache.org/dubbo-go/v3/common"
conf "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
Expand All @@ -48,11 +49,11 @@ func NewUniformRouterChain() (router.PriorityRouter, error) {
// 1. add mesh route listener
r := &RouterChain{}
rootConfig := config.GetRootConfig()
if rootConfig.ConfigCenter.DynamicConfiguration == nil {
dynamicConfiguration := conf.GetEnvInstance().GetDynamicConfiguration()
if dynamicConfiguration == nil {
logger.Infof("Config center does not start, please check if the configuration center has been properly configured in dubbogo.yml")
return nil, nil
}
dynamicConfiguration := rootConfig.ConfigCenter.DynamicConfiguration
dynamicConfiguration.AddListener(rootConfig.Application.Name, r)

// 2. try to get mesh route configuration, default key is "dubbo.io.MESHAPPRULE" with group "dubbo"
Expand Down
32 changes: 12 additions & 20 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ type CenterConfig struct {
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"`

DynamicConfiguration config_center.DynamicConfiguration
}

// Prefix dubbo.config-center
Expand Down Expand Up @@ -132,11 +130,17 @@ func (c *CenterConfig) toURL() (*common.URL, error) {
// it will prepare the environment
func startConfigCenter(rc *RootConfig) error {
cc := rc.ConfigCenter
strConf, err := cc.prepareEnvironment()
dynamicConfig, err := cc.GetDynamicConfiguration()
if err != nil {
return errors.WithMessagef(err, "start config center error!")
logger.Errorf("Start dynamic configuration center error, error message is %v", err)
return err
}

strConf, err := dynamicConfig.GetProperties(cc.DataId, config_center.WithGroup(cc.Group))
if err != nil {
logger.Warnf("Dynamic onfig center has started, but config may not be initialized, because %s", err)
return nil
}
koan := koanf.New(".")
if err = koan.Load(rawbytes.Provider([]byte(strConf)), yaml.Parser()); err != nil {
return err
Expand All @@ -162,28 +166,16 @@ func (c *CenterConfig) CreateDynamicConfiguration() (config_center.DynamicConfig
}

func (c *CenterConfig) GetDynamicConfiguration() (config_center.DynamicConfiguration, error) {
if c.DynamicConfiguration != nil {
return c.DynamicConfiguration, nil
envInstance := conf.GetEnvInstance()
if envInstance.GetDynamicConfiguration() != nil {
return envInstance.GetDynamicConfiguration(), nil
}
dynamicConfig, err := c.CreateDynamicConfiguration()
if err != nil {
logger.Warnf("Create dynamic configuration error , error message is %v", err)
return nil, errors.WithStack(err)
}
c.DynamicConfiguration = dynamicConfig
return dynamicConfig, nil
}

func (c *CenterConfig) prepareEnvironment() (string, error) {
dynamicConfig, err := c.GetDynamicConfiguration()
if err != nil {
logger.Errorf("Create dynamic configuration error , error message is %v", err)
return "", errors.WithStack(err)
}
envInstance := conf.GetEnvInstance()
envInstance.SetDynamicConfiguration(dynamicConfig)

return dynamicConfig.GetProperties(c.DataId, config_center.WithGroup(c.Group))
return dynamicConfig, nil
}

func NewConfigCenterConfigBuilder() *ConfigCenterConfigBuilder {
Expand Down
2 changes: 1 addition & 1 deletion config_center/apollo/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) {
c.appConf = &config.AppConfig{
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),
NamespaceName: url.GetParam(constant.CONFIG_NAMESPACE_KEY, cc.DefaultGroup),
IP: c.getAddressWithProtocolPrefix(url),
Secret: url.GetParam(constant.CONFIG_SECRET_KEY, ""),
IsBackupConfig: url.GetParamBool(constant.CONFIG_BACKUP_CONFIG_KEY, true),
Expand Down
8 changes: 4 additions & 4 deletions config_center/dynamic_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ import (
// DynamicConfiguration
// ////////////////////////////////////////
const (
// DEFAULT_GROUP: default group
DEFAULT_GROUP = "dubbo"
// DEFAULT_CONFIG_TIMEOUT: default config timeout
DEFAULT_CONFIG_TIMEOUT = "10s"
// DefaultGroup default group
DefaultGroup = "dubbo"
// DefaultConfigTimeout default config timeout
DefaultConfigTimeout = "10s"
)

// DynamicConfiguration for modify listener and get properties file
Expand Down
2 changes: 1 addition & 1 deletion config_center/file/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (fsdc *FileSystemDynamicConfiguration) GetPath(key string, group string) st
}

if len(group) == 0 {
group = config_center.DEFAULT_GROUP
group = config_center.DefaultGroup
}

return filepath.Join(fsdc.rootPath, group, adapterKey(key))
Expand Down
2 changes: 1 addition & 1 deletion config_center/nacos/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type nacosDynamicConfiguration struct {

func newNacosDynamicConfiguration(url *common.URL) (*nacosDynamicConfiguration, error) {
c := &nacosDynamicConfiguration{
rootPath: "/" + url.GetParam(constant.CONFIG_NAMESPACE_KEY, config_center.DEFAULT_GROUP) + "/config",
rootPath: "/" + url.GetParam(constant.CONFIG_NAMESPACE_KEY, config_center.DefaultGroup) + "/config",
url: url,
done: make(chan struct{}),
}
Expand Down
6 changes: 4 additions & 2 deletions config_center/zookeeper/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type zookeeperDynamicConfiguration struct {
func newZookeeperDynamicConfiguration(url *common.URL) (*zookeeperDynamicConfiguration, error) {
c := &zookeeperDynamicConfiguration{
url: url,
rootPath: "/" + url.GetParam(constant.CONFIG_NAMESPACE_KEY, config_center.DEFAULT_GROUP) + "/config",
rootPath: "/" + url.GetParam(constant.CONFIG_NAMESPACE_KEY, config_center.DefaultGroup) + "/config",
}
if v, ok := config.GetRootConfig().ConfigCenter.Params["base64"]; ok {
base64Enabled, err := strconv.ParseBool(v)
Expand Down Expand Up @@ -146,6 +146,8 @@ func (c *zookeeperDynamicConfiguration) PublishConfig(key string, group string,
if c.base64Enabled {
valueBytes = []byte(base64.StdEncoding.EncodeToString(valueBytes))
}
// FIXME this method need to be fixed, because it will recursively
// create every node in the path with given value which we may not expected.
err := c.client.CreateWithValue(path, valueBytes)
if err != nil {
return perrors.WithStack(err)
Expand Down Expand Up @@ -246,7 +248,7 @@ func (c *zookeeperDynamicConfiguration) getPath(key string, group string) string

func (c *zookeeperDynamicConfiguration) buildPath(group string) string {
if len(group) == 0 {
group = config_center.DEFAULT_GROUP
group = config_center.DefaultGroup
}
return c.rootPath + pathSeparator + group
}
12 changes: 7 additions & 5 deletions config_center/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/remoting"
)
Expand Down Expand Up @@ -65,10 +66,12 @@ func (l *CacheListener) DataChange(event remoting.Event) bool {
// meanings new node
return true
}
key := l.pathToKey(event.Path)
var key string
// TODO use common way
if strings.HasSuffix(key, constant.MeshRouteSuffix) {
key = key[:strings.Index(key, constant.MeshRouteSuffix)]
if strings.HasSuffix(event.Path, constant.MeshRouteSuffix) {
key = config.GetRootConfig().Application.Name
} else {
key = l.pathToKey(event.Path)
}
if key != "" {
if listeners, ok := l.keyListeners.Load(key); ok {
Expand All @@ -85,8 +88,7 @@ func (l *CacheListener) pathToKey(path string) string {
key := strings.Replace(strings.Replace(path, l.rootPath+"/", "", -1), "/", ".", -1)
if strings.HasSuffix(key, constant.ConfiguratorSuffix) ||
strings.HasSuffix(key, constant.TagRouterRuleSuffix) ||
strings.HasSuffix(key, constant.ConditionRouterRuleSuffix) ||
strings.HasSuffix(key, constant.MeshRouteSuffix) {
strings.HasSuffix(key, constant.ConditionRouterRuleSuffix) {
// governance config, so we remove the "dubbo." prefix
key = key[strings.Index(key, ".")+1:]
}
Expand Down
Loading

0 comments on commit 5fe7d87

Please sign in to comment.