Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rft: Optimize remotes configuration #687

Merged
merged 11 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ 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 {
Expand All @@ -52,7 +49,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"`
Expand All @@ -72,74 +68,6 @@ 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
}

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)
}
}
// 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
}

func getKeyPrefix(val reflect.Value) []string {
var (
prefix string
Expand Down
19 changes: 0 additions & 19 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (

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"
)

Expand Down Expand Up @@ -282,23 +280,6 @@ func TestRefreshProvider(t *testing.T) {
assert.Equal(t, "20001", father.Protocols["jsonrpc1"].Port)
}

func TestStartConfigCenter(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
})
c := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{
Protocol: "mock",
Address: "172.0.0.1",
Group: "dubbo",
ConfigFile: "mockDubbo.properties",
}}
err := c.startConfigCenter()
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 TestInitializeStruct(t *testing.T) {
testConsumerConfig := &ConsumerConfig{}
tp := reflect.TypeOf(ConsumerConfig{})
Expand Down
99 changes: 99 additions & 0 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package config
import (
"context"
"net/url"
"reflect"
"time"
)

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -77,3 +85,94 @@ func (c *ConfigCenterConfig) GetUrlMap() url.Values {
urlMap.Set(constant.CONFIG_LOG_DIR_KEY, c.LogDir)
return urlMap
}

type configCenter struct {
}

// toURL will compatible with baseConfig.ConfigCenterConfig.Address and baseConfig.ConfigCenterConfig.RemoteRef before 1.6.0
// After 1.6.0 will not compatible, only baseConfig.ConfigCenterConfig.RemoteRef
func (b *configCenter) toURL(baseConfig BaseConfig) (common.URL, error) {
if len(baseConfig.ConfigCenterConfig.Address) > 0 {
return common.NewURL(baseConfig.ConfigCenterConfig.Address,
common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol), common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap()))
}

remoteRef := baseConfig.ConfigCenterConfig.RemoteRef
rc, ok := GetBaseConfig().GetRemoteConfig(remoteRef)

if !ok {
return common.URL{}, perrors.New("Could not find out the remote ref config, name: " + remoteRef)
}

newURL, err := rc.toURL()
if err == nil {
newURL.SetParams(baseConfig.ConfigCenterConfig.GetUrlMap())
}
return newURL, err
}

// startConfigCenter will start the config center.
// it will prepare the environment
func (b *configCenter) startConfigCenter(baseConfig BaseConfig) error {
url, err := b.toURL(baseConfig)
if err != nil {
return err
}
if b.prepareEnvironment(baseConfig, &url) != nil {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
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(configCenterUrl.Protocol)
dynamicConfig, err := factory.GetDynamicConfiguration(configCenterUrl)
config.GetEnvInstance().SetDynamicConfiguration(dynamicConfig)
zouyx marked this conversation as resolved.
Show resolved Hide resolved
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(baseConfig.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
}
95 changes: 95 additions & 0 deletions config/config_center_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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 (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
)

func TestStartConfigCenter(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
})
baseConfig := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{
Protocol: "mock",
Address: "172.0.0.1",
Group: "dubbo",
ConfigFile: "mockDubbo.properties",
}}

c := &configCenter{}
err := c.startConfigCenter(*baseConfig)
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 TestStartConfigCenterWithRemoteRef(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
})
m := make(map[string]*RemoteConfig)
m["mock"] = &RemoteConfig{Protocol: "mock", Address: "172.0.0.1"}
baseConfig = &BaseConfig{
Remotes: m,
ConfigCenterConfig: &ConfigCenterConfig{
Group: "dubbo",
RemoteRef: "mock",
ConfigFile: "mockDubbo.properties",
}}

c := &configCenter{}
err := c.startConfigCenter(*baseConfig)
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)

baseConfig = nil
zouyx marked this conversation as resolved.
Show resolved Hide resolved
}

func TestStartConfigCenterWithRemoteRefError(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
})
m := make(map[string]*RemoteConfig)
m["mock"] = &RemoteConfig{Address: "172.0.0.1"}
baseConfig := &BaseConfig{
Remotes: m,
ConfigCenterConfig: &ConfigCenterConfig{
Protocol: "mock",
Group: "dubbo",
RemoteRef: "mock",
ConfigFile: "mockDubbo.properties",
}}

c := &configCenter{}
err := c.startConfigCenter(*baseConfig)
assert.Error(t, err)
}
17 changes: 9 additions & 8 deletions config/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,13 +126,6 @@ func ConsumerInit(confConFile string) error {
func configCenterRefreshConsumer() error {
//fresh it
var err error
if consumerConfig.ConfigCenterConfig != nil {
consumerConfig.SetFatherConfig(consumerConfig)
if err = consumerConfig.startConfigCenter(); err != nil {
return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err))
}
consumerConfig.fresh()
}
if consumerConfig.Request_Timeout != "" {
if consumerConfig.RequestTimeout, err = time.ParseDuration(consumerConfig.Request_Timeout); err != nil {
return perrors.WithMessagef(err, "time.ParseDuration(Request_Timeout{%#v})", consumerConfig.Request_Timeout)
Expand All @@ -142,5 +136,12 @@ func configCenterRefreshConsumer() error {
return perrors.WithMessagef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout)
}
}
if consumerConfig.ConfigCenterConfig != nil {
consumerConfig.SetFatherConfig(consumerConfig)
if err = consumerConfig.startConfigCenter((*consumerConfig).BaseConfig); err != nil {
return perrors.Errorf("start config center error , error message is {%v}", perrors.WithStack(err))
}
consumerConfig.fresh()
}
return nil
}
Loading