Skip to content

Commit

Permalink
Config enhance (apache#1467)
Browse files Browse the repository at this point in the history
* up:配置优化

* up:修改registryid获取逻辑

* up:修改registryid获取逻辑

* fmt

* fix:测试通过

* 代码结构调整

* 代码结构调整

* 代码结构调整

* 代码结构调整

* fmt

* fix:update get provider instance

* cac -> once

* up:更新初始化方法

* fmt

* 更新默认端口

* 代码优化

* 优化日志打印

* add new root config

* 解决启动问题

* up:处理nil问题

* fmt

* fmt
  • Loading branch information
zhaoyunxing92 authored Sep 25, 2021
1 parent 0697950 commit 2072171
Show file tree
Hide file tree
Showing 36 changed files with 487 additions and 573 deletions.
2 changes: 2 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const (

const (
RegistryConfigPrefix = "dubbo.registries"
ApplicationConfigPrefix = "dubbo.application"
ConfigCenterPrefix = "dubbo.config-center"
SingleRegistryConfigPrefix = "dubbo.registry"
ReferenceConfigPrefix = "dubbo.reference"
Expand All @@ -190,6 +191,7 @@ const (
ShutdownConfigPrefix = "dubbo.shutdown"
MetadataReportPrefix = "dubbo.metadata-report"
RouterConfigPrefix = "dubbo.router"
LoggerConfigPrefix = "dubbo.logger"
)

const (
Expand Down
8 changes: 4 additions & 4 deletions common/extension/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ import (
"dubbo.apache.org/dubbo-go/v3/registry"
)

var registrys = make(map[string]func(config *common.URL) (registry.Registry, error))
var registries = make(map[string]func(config *common.URL) (registry.Registry, error))

// SetRegistry sets the registry extension with @name
func SetRegistry(name string, v func(_ *common.URL) (registry.Registry, error)) {
registrys[name] = v
registries[name] = v
}

// GetRegistry finds the registry extension with @name
func GetRegistry(name string, config *common.URL) (registry.Registry, error) {
if registrys[name] == nil {
if registries[name] == nil {
panic("registry for " + name + " does not exist. please make sure that you have imported the package dubbo.apache.org/dubbo-go/v3/registry/" + name + ".")
}
return registrys[name](config)
return registries[name](config)
}
72 changes: 63 additions & 9 deletions config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config

import (
"github.com/creasty/defaults"

"github.com/pkg/errors"
)

import (
Expand All @@ -37,25 +39,77 @@ type ApplicationConfig struct {
MetadataType string `default:"local" yaml:"metadata-type" json:"metadataType,omitempty" property:"metadataType"`
}

// Prefix dubbo.applicationConfig
// Prefix dubbo.application
func (ApplicationConfig) Prefix() string {
return constant.DUBBO + ".application"
return constant.ApplicationConfigPrefix
}

func (ac *ApplicationConfig) Init(rc *RootConfig) error {
// ignore refresh action
if rc.refresh || ac == nil {
rootConfig.Application = new(ApplicationConfig)
return nil
// Init application config and set default value
func (ac *ApplicationConfig) Init() error {
if ac == nil {
return errors.New("application is null")
}
defaults.MustSet(ac)
if err := ac.check(); err != nil {
return err
}
return nil
}

func GetApplicationInstance(opts ...ApplicationConfigOpt) *ApplicationConfig {
ac := &ApplicationConfig{}
for _, opt := range opts {
opt(ac)
}
return ac
}

func (ac *ApplicationConfig) check() error {
defaults.MustSet(ac)
if err := defaults.Set(ac); err != nil {
return err
}
return verify(ac)
}

type ApplicationConfigOpt func(config *ApplicationConfig)

func WithOrganization(organization string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Organization = organization
}
}

func WithName(name string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Name = name
}
}

func WithModule(module string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Module = module
}
}

func WithVersion(version string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Version = version
}
}

func WithOwner(owner string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Owner = owner
}
}

func WithEnvironment(env string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Environment = env
}
}

func WithMetadataType(metadataType string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.MetadataType = metadataType
}
}
18 changes: 14 additions & 4 deletions config/network_config.go → config/application_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@

package config

type NetworkConfig struct {
}
import (
"testing"
)

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

func TestApplicationConfig(t *testing.T) {

err := Load(WithPath("./testdata/config/application/application.yaml"))
assert.Nil(t, err)

func initNetworkConfig(rc *RootConfig) error {
return nil
center := rootConfig.Registries
assert.NotNil(t, center)
}
29 changes: 19 additions & 10 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ import (
//
// CenterConfig has currently supported Zookeeper, Nacos, Etcd, Consul, Apollo
type CenterConfig struct {
Protocol string `yaml:"protocol" json:"protocol,omitempty"`
Address string `yaml:"address" json:"address,omitempty"`
DataId string `yaml:"data-id" json:"data-id,omitempty"`
// Deprecated
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"`
Expand All @@ -72,19 +71,31 @@ type CenterConfig struct {
Params map[string]string `yaml:"params" json:"parameters,omitempty"`
}

// Prefix dubbo.config-center
func (CenterConfig) Prefix() string {
return constant.ConfigCenterPrefix
}

func GetConfigCenterInstance(opts ...CenterConfigOpt) *CenterConfig {
cc := &CenterConfig{
Params: make(map[string]string, 1),
}
for _, opt := range opts {
opt(cc)
}
return cc
}

func (c *CenterConfig) check() error {
if err := defaults.Set(c); err != nil {
return err
}
c.translateConfigAddress()
if c.Address == "" || c.Protocol == "" {
return errors.Errorf("invalid config center config %+v", c)
}
return verify(c)
}

func (c *CenterConfig) Init(rc *RootConfig) error {
if rc.refresh || c == nil {
if c == nil {
return nil
}
if err := c.check(); err != nil {
Expand Down Expand Up @@ -171,8 +182,6 @@ func startConfigCenter(rc *RootConfig) error {
return err
}

rc.refresh = false
rc.ConfigCenter = nil
return nil
}

Expand Down
13 changes: 2 additions & 11 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
)

import (
hessian "github.com/apache/dubbo-go-hessian2"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/parsers/toml"
Expand All @@ -45,29 +43,22 @@ import (
)

var (
rootConfig *RootConfig
rootConfig = GetInstance()
maxWait = 3
)

func Load(opts ...LoaderConfOption) error {
hessian.RegisterPOJO(&common.MetadataInfo{})
hessian.RegisterPOJO(&common.ServiceInfo{})
hessian.RegisterPOJO(&common.URL{})
// conf
conf := NewLoaderConf(opts...)
// init config
rootConfig = new(RootConfig)
koan := getKoanf(conf)
if err := koan.UnmarshalWithConf(rootConfig.Prefix(),
rootConfig, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
return err
}
rootConfig.refresh = false
extension.SetAndInitGlobalDispatcher(rootConfig.EventDispatcherType)
if err := rootConfig.Init(); err != nil {
return err
}
registerServiceInstance()
rootConfig.Start()
return nil
}

Expand Down
75 changes: 23 additions & 52 deletions config/config_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@ import (
"github.com/stretchr/testify/assert"
)

import (
"dubbo.apache.org/dubbo-go/v3/config/testdata/config/service"
)

func init() {
SetProviderService(new(service.OrderService))
SetProviderService(new(service.HelloService))
}

const (
configPath = "./testdata/application.yaml"
)

func TestLoad(t *testing.T) {
Load(WithPath(configPath))

err := Load(WithPath(configPath))
assert.Nil(t, err)
t.Run("application", func(t *testing.T) {
application := rootConfig.Application

Expand All @@ -59,7 +50,7 @@ func TestLoad(t *testing.T) {
assert.Equal(t, 2, len(registries))
//address= nacos://127.0.0.1:8848 Translate Registry Address
assert.Equal(t, "nacos", registries["nacos"].Protocol)
assert.Equal(t, "10s", registries["zk"].Timeout)
assert.Equal(t, "5s", registries["zk"].Timeout)
})

//config-center
Expand All @@ -74,52 +65,32 @@ func TestLoad(t *testing.T) {
//TestLoadConfigCenter test key config_center、config-center 、configCenter
func TestLoadConfigCenter(t *testing.T) {

t.Run("config-center", func(t *testing.T) {
Load(WithPath("./testdata/config/center/conf-application.yaml"))
conf := rootConfig.ConfigCenter
assert.Equal(t, "nacos", conf.Protocol)
assert.Equal(t, "10s", conf.Timeout)
assert.Equal(t, "./logs", conf.LogDir)
})
err := Load(WithPath("./testdata/config/center/conf-application.yaml"))
assert.Nil(t, err)
conf := rootConfig.ConfigCenter
assert.Equal(t, "nacos", conf.Protocol)
assert.Equal(t, "10s", conf.Timeout)
assert.Equal(t, "./logs", conf.LogDir)
}

func TestGetRegistriesConfig(t *testing.T) {
t.Run("registry", func(t *testing.T) {
Load(WithPath("./testdata/config/registry/application.yaml"))

registries := rootConfig.Registries

assert.Equal(t, 2, len(registries))
// nacos
assert.Equal(t, "nacos", registries["nacos"].Protocol)
assert.Equal(t, "5s", registries["nacos"].Timeout)
assert.Equal(t, "127.0.0.1:8848", registries["nacos"].Address)
assert.Equal(t, "dev", registries["nacos"].Group)
// zk
assert.Equal(t, "zookeeper", registries["zk"].Protocol)
assert.Equal(t, "10s", registries["zk"].Timeout)
assert.Equal(t, "127.0.0.1:2181", registries["zk"].Address)
assert.Equal(t, "test", registries["zk"].Group)
})
}

func TestGetProviderConfig(t *testing.T) {
// empty registry
t.Run("empty registry", func(t *testing.T) {
assert.Nil(t, Load(WithPath("./testdata/config/provider/empty_registry_application.yaml")))
provider := rootConfig.Provider
assert.Equal(t, 0, len(provider.Registry))
})
err := Load(WithPath("./testdata/config/registry/application.yaml"))

t.Run("root registry", func(t *testing.T) {
Load(WithPath("./testdata/config/provider/registry_application.yaml"))
provider := rootConfig.Provider
assert.NotNil(t, provider)
})
}
assert.Nil(t, err)
registries := rootConfig.Registries

func TestRootConfig(t *testing.T) {
Load(WithPath("./testdata/config/app/application.yaml"))
assert.Equal(t, 2, len(registries))
// nacos
assert.Equal(t, "nacos", registries["nacos"].Protocol)
assert.Equal(t, "5s", registries["nacos"].Timeout)
assert.Equal(t, "127.0.0.1:8848", registries["nacos"].Address)
assert.Equal(t, "dev", registries["nacos"].Group)
// zk
assert.Equal(t, "zookeeper", registries["zk"].Protocol)
assert.Equal(t, "5s", registries["zk"].Timeout)
assert.Equal(t, "127.0.0.1:2181", registries["zk"].Address)
assert.Equal(t, "test", registries["zk"].Group)
}

//
Expand Down
1 change: 1 addition & 0 deletions config/config_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func removeDuplicateElement(items []string) []string {
func translateRegistryIds(registryIds []string) []string {
ids := make([]string, 0)
for _, id := range registryIds {

ids = append(ids, strings.Split(id, ",")...)
}
return removeDuplicateElement(ids)
Expand Down
Loading

0 comments on commit 2072171

Please sign in to comment.