Skip to content

Commit

Permalink
feat(config): add root config api builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dongjianhui03 committed Sep 27, 2021
1 parent c164f38 commit ac7f80b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 25 deletions.
22 changes: 11 additions & 11 deletions config/dubbo_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ var (
func GetInstance(opts ...RootConfigOpt) *RootConfig {
registerPOJO()
rc := &RootConfig{
ConfigCenter: GetConfigCenterInstance(),
ServiceDiscoveries: make(map[string]*ServiceDiscoveryConfig),
MetadataReportConfig: &MetadataReportConfig{},
Application: GetApplicationInstance(),
Registries: make(map[string]*RegistryConfig),
Protocols: GetProtocolsInstance(),
Provider: GetProviderInstance(),
Consumer: GetConsumerInstance(),
MetricConfig: &MetricConfig{},
Logger: GetLoggerConfigInstance(),
ConfigCenter: GetConfigCenterInstance(),
ServiceDiscoveries: make(map[string]*ServiceDiscoveryConfig),
MetadataReport: &MetadataReportConfig{},
Application: GetApplicationInstance(),
Registries: make(map[string]*RegistryConfig),
Protocols: GetProtocolsInstance(),
Provider: GetProviderInstance(),
Consumer: GetConsumerInstance(),
Metric: &MetricConfig{},
Logger: GetLoggerConfigInstance(),
}
for _, opt := range opts {
opt(rc)
Expand Down Expand Up @@ -80,7 +80,7 @@ func (rc *RootConfig) Init() error {
if err := initServiceDiscoveryConfig(rc); err != nil {
return err
}
if err := rc.MetadataReportConfig.Init(rc); err != nil {
if err := rc.MetadataReport.Init(rc); err != nil {
return err
}
if err := initMetricConfig(rc); err != nil {
Expand Down
107 changes: 94 additions & 13 deletions config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ type RootConfig struct {
// ServiceDiscoveries to be remove in 3.0 config-enhance
ServiceDiscoveries map[string]*ServiceDiscoveryConfig `yaml:"service-discovery" json:"service-discovery,omitempty" property:"service-discovery"`

MetadataReportConfig *MetadataReportConfig `yaml:"metadata-report" json:"metadata-report,omitempty" property:"metadata-report"`
MetadataReport *MetadataReportConfig `yaml:"metadata-report" json:"metadata-report,omitempty" property:"metadata-report"`

// provider config
Provider *ProviderConfig `yaml:"provider" json:"provider" property:"provider"`

// consumer config
Consumer *ConsumerConfig `yaml:"consumer" json:"consumer" property:"consumer"`

MetricConfig *MetricConfig `yaml:"metrics" json:"metrics,omitempty" property:"metrics"`
Metric *MetricConfig `yaml:"metrics" json:"metrics,omitempty" property:"metrics"`

// Logger log
Logger *LoggerConfig `yaml:"logger" json:"logger,omitempty" property:"logger"`
Expand All @@ -67,6 +67,87 @@ type RootConfig struct {
CacheFile string `yaml:"cache_file" json:"cache_file,omitempty" property:"cache_file"`
}

type RootConfigBuilder struct {
*RootConfig
}

func (rb *RootConfigBuilder) Application(application *ApplicationConfig) *RootConfigBuilder {
rb.RootConfig.Application = application
return rb
}

func (rb *RootConfigBuilder) Protocols(protocols map[string]*ProtocolConfig) *RootConfigBuilder {
rb.RootConfig.Protocols = protocols
return rb
}

func (rb *RootConfigBuilder) Registries(registries map[string]*RegistryConfig) *RootConfigBuilder {
rb.RootConfig.Registries = registries
return rb
}

func (rb *RootConfigBuilder) Remotes(remotes map[string]*RemoteConfig) *RootConfigBuilder {
rb.RootConfig.Remotes = remotes
return rb
}

func (rb *RootConfigBuilder) ServiceDiscoveries(serviceDiscoveries map[string]*ServiceDiscoveryConfig) *RootConfigBuilder {
rb.RootConfig.ServiceDiscoveries = serviceDiscoveries
return rb
}

func (rb *RootConfigBuilder) MetadataReport(metadataReport *MetadataReportConfig) *RootConfigBuilder {
rb.RootConfig.MetadataReport = metadataReport
return rb
}

func (rb *RootConfigBuilder) Provider(provider *ProviderConfig) *RootConfigBuilder {
rb.RootConfig.Provider = provider
return rb
}

func (rb *RootConfigBuilder) Consumer(consumer *ConsumerConfig) *RootConfigBuilder {
rb.RootConfig.Consumer = consumer
return rb
}

func (rb *RootConfigBuilder) Metric(metric *MetricConfig) *RootConfigBuilder {
rb.RootConfig.Metric = metric
return rb
}

func (rb *RootConfigBuilder) Logger(logger *LoggerConfig) *RootConfigBuilder {
rb.RootConfig.Logger = logger
return rb
}

func (rb *RootConfigBuilder) Shutdown(shutdown *ShutdownConfig) *RootConfigBuilder {
rb.RootConfig.Shutdown = shutdown
return rb
}

func (rb *RootConfigBuilder) Router(router []*RouterConfig) *RootConfigBuilder {
rb.RootConfig.Router = router
return rb
}

func (rb *RootConfigBuilder) EventDispatcherType(eventDispatcherType string) *RootConfigBuilder {
rb.RootConfig.EventDispatcherType = eventDispatcherType
return rb
}

func (rb *RootConfigBuilder) CacheFile(cacheFile string) *RootConfigBuilder {
rb.RootConfig.CacheFile = cacheFile
return rb
}

func (rb *RootConfigBuilder) Build() *RootConfig {
if err := rb.RootConfig.Init(); err != nil {
panic(err)
}
return rb.RootConfig
}

func SetRootConfig(r RootConfig) {
rootConfig = &r
}
Expand Down Expand Up @@ -198,15 +279,15 @@ func (rc *RootConfig) getRegistryIds() []string {
// NewRootConfig get root config
func NewRootConfig(opts ...RootConfigOpt) *RootConfig {
newRootConfig := &RootConfig{
ConfigCenter: &CenterConfig{},
ServiceDiscoveries: make(map[string]*ServiceDiscoveryConfig),
MetadataReportConfig: &MetadataReportConfig{},
Application: &ApplicationConfig{},
Registries: make(map[string]*RegistryConfig),
Protocols: make(map[string]*ProtocolConfig),
Provider: GetProviderInstance(),
Consumer: GetConsumerInstance(),
MetricConfig: &MetricConfig{},
ConfigCenter: &CenterConfig{},
ServiceDiscoveries: make(map[string]*ServiceDiscoveryConfig),
MetadataReport: &MetadataReportConfig{},
Application: &ApplicationConfig{},
Registries: make(map[string]*RegistryConfig),
Protocols: make(map[string]*ProtocolConfig),
Provider: GetProviderInstance(),
Consumer: GetConsumerInstance(),
Metric: &MetricConfig{},
}
for _, o := range opts {
o(newRootConfig)
Expand All @@ -219,7 +300,7 @@ type RootConfigOpt func(config *RootConfig)
// WithMetricsConfig set root config with given @metricsConfig
func WithMetricsConfig(metricsConfig *MetricConfig) RootConfigOpt {
return func(rc *RootConfig) {
rc.MetricConfig = metricsConfig
rc.Metric = metricsConfig
}
}

Expand Down Expand Up @@ -261,7 +342,7 @@ func WithRootApplicationConfig(appConfig *ApplicationConfig) RootConfigOpt {
// WithRootMetadataReportConfig set root config with given @metadataReportConfig
func WithRootMetadataReportConfig(metadataReportConfig *MetadataReportConfig) RootConfigOpt {
return func(rc *RootConfig) {
rc.MetadataReportConfig = metadataReportConfig
rc.MetadataReport = metadataReportConfig
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (svc *ServiceConfig) GetExportedUrls() []*common.URL {
}

func (svc *ServiceConfig) publishServiceDefinition(url *common.URL) {
//svc.rootConfig.MetadataReportConfig.
//svc.rootConfig.MetadataReport.
if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil {
remoteMetadataService.PublishServiceDefinition(url)
}
Expand Down

0 comments on commit ac7f80b

Please sign in to comment.