Skip to content

Commit

Permalink
Merge pull request #1499 from apache/config-api
Browse files Browse the repository at this point in the history
New Config API support
  • Loading branch information
zhaoyunxing92 committed Oct 4, 2021
2 parents a9e9f9a + f7ecfb6 commit a32573b
Show file tree
Hide file tree
Showing 40 changed files with 1,051 additions and 1,495 deletions.
2 changes: 1 addition & 1 deletion common/url.go
Expand Up @@ -210,7 +210,7 @@ func WithToken(token string) Option {
if len(token) > 0 {
value := token
if strings.ToLower(token) == "true" || strings.ToLower(token) == "default" {
u := uuid.NewV4()
u, _ := uuid.NewV4()
value = u.String()
}
url.SetParam(constant.TOKEN_KEY, value)
Expand Down
69 changes: 32 additions & 37 deletions config/application_config.go
Expand Up @@ -55,61 +55,56 @@ func (ac *ApplicationConfig) Init() error {
return nil
}

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

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

type ApplicationConfigOpt func(config *ApplicationConfig)
func NewApplicationConfigBuilder() *ApplicationConfigBuilder {
return &ApplicationConfigBuilder{application: &ApplicationConfig{}}
}

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

func WithName(name string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Name = name
}
func (acb *ApplicationConfigBuilder) SetOrganization(organization string) *ApplicationConfigBuilder {
acb.application.Organization = organization
return acb
}

func WithModule(module string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Module = module
}
func (acb *ApplicationConfigBuilder) SetName(name string) *ApplicationConfigBuilder {
acb.application.Name = name
return acb
}

func WithVersion(version string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Version = version
}
func (acb *ApplicationConfigBuilder) SetModule(module string) *ApplicationConfigBuilder {
acb.application.Module = module
return acb
}

func WithOwner(owner string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Owner = owner
}
func (acb *ApplicationConfigBuilder) SetVersion(version string) *ApplicationConfigBuilder {
acb.application.Version = version
return acb
}

func WithEnvironment(env string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.Environment = env
}
func (acb *ApplicationConfigBuilder) SetOwner(owner string) *ApplicationConfigBuilder {
acb.application.Owner = owner
return acb
}

func WithMetadataType(metadataType string) ApplicationConfigOpt {
return func(ac *ApplicationConfig) {
ac.MetadataType = metadataType
}
func (acb *ApplicationConfigBuilder) SetEnvironment(environment string) *ApplicationConfigBuilder {
acb.application.Environment = environment
return acb
}

func (acb *ApplicationConfigBuilder) SetMetadataType(metadataType string) *ApplicationConfigBuilder {
acb.application.MetadataType = metadataType
return acb
}

func (acb *ApplicationConfigBuilder) Build() *ApplicationConfig {
return acb.application
}
143 changes: 35 additions & 108 deletions config/config_center_config.go
Expand Up @@ -76,16 +76,6 @@ 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
Expand Down Expand Up @@ -140,21 +130,6 @@ func (c *CenterConfig) translateConfigAddress() string {
// toURL will compatible with baseConfig.ShutdownConfig.Address and baseConfig.ShutdownConfig.RemoteRef before 1.6.0
// After 1.6.0 will not compatible, only baseConfig.ShutdownConfig.RemoteRef
func (c *CenterConfig) toURL() (*common.URL, error) {
//remoteRef := baseConfig.ConfigCenterConfig.RemoteRef
//// if set remote ref use remote
//if len(remoteRef) <= 0 {
// return common.NewURL(baseConfig.ConfigCenterConfig.Address,
// common.WithProtocol(baseConfig.ConfigCenterConfig.Protocol),
// common.WithParams(baseConfig.ConfigCenterConfig.GetUrlMap()))
//}
//rc, ok := baseConfig.GetRemoteConfig(remoteRef)
//if !ok {
// return nil, perrors.New("Could not find out the remote ref config, name: " + remoteRef)
//}
//// set protocol if remote not set
//if len(rc.Protocol) <= 0 {
// rc.Protocol = baseConfig.ConfigCenterConfig.Protocol
//}
return common.NewURL(c.Address,
common.WithProtocol(c.Protocol),
common.WithParams(c.GetUrlMap()))
Expand Down Expand Up @@ -211,105 +186,57 @@ func (c *CenterConfig) prepareEnvironment(configCenterUrl *common.URL) (string,
envInstance.SetDynamicConfiguration(dynamicConfig)

return dynamicConfig.GetProperties(c.DataId, config_center.WithGroup(c.Group))
//if err != nil {
// logger.Errorf("Get config content in dynamic configuration error , error message is %v", err)
// return errors.WithStack(err)
//}
//yaml.Unmarshal([]byte(conten),rootConfig)
//var appGroup string
//var appContent string
//if config2.providerConfig != nil && config2.providerConfig.ApplicationConfig != nil &&
// reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ProviderConfig" {
// appGroup = config2.providerConfig.ApplicationConfig.Name
//} else if config2.consumerConfig != nil && config2.consumerConfig.ApplicationConfig != nil &&
// reflect.ValueOf(baseConfig.fatherConfig).Elem().Type().Name() == "ConsumerConfig" {
// appGroup = config2.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)
//}
//envInstance.UpdateExternalConfigMap(mapContent)
//
//// appGroup config file
//if len(appContent) != 0 {
// appMapContent, err := dynamicConfig.Parser().Parse(appContent)
// if err != nil {
// return perrors.WithStack(err)
// }
// envInstance.UpdateAppExternalConfigMap(appMapContent)
//}
}

type CenterConfigOpt func(config *CenterConfig)
func NewConfigCenterConfigBuilder() *ConfigCenterConfigBuilder {
return &ConfigCenterConfigBuilder{configCenterConfig: newEmptyConfigCenterConfig()}
}

func NewConfigCenterConfig(opts ...CenterConfigOpt) *CenterConfig {
centerConfig := &CenterConfig{
Params: make(map[string]string),
}
for _, o := range opts {
o(centerConfig)
}
return centerConfig
type ConfigCenterConfigBuilder struct {
configCenterConfig *CenterConfig
}

// WithConfigCenterProtocol set ProtocolConfig with given protocolName protocol
func WithConfigCenterProtocol(protocol string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Protocol = protocol
}
func (ccb *ConfigCenterConfigBuilder) SetProtocol(protocol string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Protocol = protocol
return ccb
}

// WithConfigCenterAddress set ProtocolConfig with given @addr
func WithConfigCenterAddress(addr string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Address = addr
}
func (ccb *ConfigCenterConfigBuilder) SetUserName(userName string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Username = userName
return ccb
}

// WithConfigCenterDataID set ProtocolConfig with given @dataID
func WithConfigCenterDataID(dataID string) CenterConfigOpt {
return func(config *CenterConfig) {
config.DataId = dataID
}
func (ccb *ConfigCenterConfigBuilder) SetAddress(address string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Address = address
return ccb
}

// WithConfigCenterGroup set ProtocolConfig with given @group
func WithConfigCenterGroup(group string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Group = group
}
func (ccb *ConfigCenterConfigBuilder) SetPassword(password string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Password = password
return ccb
}

// WithConfigCenterUsername set ProtocolConfig with given @username
func WithConfigCenterUsername(username string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Username = username
}
func (ccb *ConfigCenterConfigBuilder) SetNamespace(namespace string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Namespace = namespace
return ccb
}

// WithConfigCenterPassword set ProtocolConfig with given @password
func WithConfigCenterPassword(password string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Password = password
}
func (ccb *ConfigCenterConfigBuilder) SetDataID(dataID string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.DataId = dataID
return ccb
}

// WithConfigCenterNamespace set ProtocolConfig with given @namespace
func WithConfigCenterNamespace(namespace string) CenterConfigOpt {
return func(config *CenterConfig) {
config.Namespace = namespace
func (ccb *ConfigCenterConfigBuilder) SetGroup(group string) *ConfigCenterConfigBuilder {
ccb.configCenterConfig.Group = group
return ccb
}

func (ccb *ConfigCenterConfigBuilder) Build() *CenterConfig {
return ccb.configCenterConfig
}

func newEmptyConfigCenterConfig() *CenterConfig {
return &CenterConfig{
Params: make(map[string]string),
}
}
14 changes: 7 additions & 7 deletions config/config_loader.go
Expand Up @@ -36,7 +36,7 @@ import (
)

var (
rootConfig = GetInstance()
rootConfig = NewRootConfigBuilder().Build()
maxWait = 3
)

Expand Down Expand Up @@ -158,19 +158,19 @@ func RPCService(service common.RPCService) {
// So you don't need to worry about the race condition
func GetMetricConfig() *MetricConfig {
// todo
//if GetBaseConfig().MetricConfig == nil {
//if GetBaseConfig().Metric == nil {
// configAccessMutex.Lock()
// defer configAccessMutex.Unlock()
// if GetBaseConfig().MetricConfig == nil {
// GetBaseConfig().MetricConfig = &metric.MetricConfig{}
// if GetBaseConfig().Metric == nil {
// GetBaseConfig().Metric = &metric.Metric{}
// }
//}
//return GetBaseConfig().MetricConfig
return rootConfig.MetricConfig
//return GetBaseConfig().Metric
return rootConfig.Metric
}

func GetMetadataReportConfg() *MetadataReportConfig {
return rootConfig.MetadataReportConfig
return rootConfig.MetadataReport
}

func IsProvider() bool {
Expand Down

0 comments on commit a32573b

Please sign in to comment.