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

feat(config): add root config api builder #1491

Merged
merged 7 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,53 @@ func WithMetadataType(metadataType string) ApplicationConfigOpt {
ac.MetadataType = metadataType
}
}

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

type ApplicationConfigBuilder struct {
application *ApplicationConfig
}

func (acb *ApplicationConfigBuilder) SetOrganization(organization string) *ApplicationConfigBuilder {
acb.application.Organization = organization
return acb
}

func (acb *ApplicationConfigBuilder) SetName(name string) *ApplicationConfigBuilder {
acb.application.Name = name
return acb
}

func (acb *ApplicationConfigBuilder) SetModule(module string) *ApplicationConfigBuilder {
acb.application.Module = module
return acb
}

func (acb *ApplicationConfigBuilder) SetVersion(version string) *ApplicationConfigBuilder {
acb.application.Version = version
return acb
}

func (acb *ApplicationConfigBuilder) SetOwner(owner string) *ApplicationConfigBuilder {
acb.application.Owner = owner
return acb
}

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 {
if err := acb.application.Init(); err != nil {
panic(err)
}
return acb.application
}
12 changes: 6 additions & 6 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo 要不干掉?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解释一下上面的问题,
&ApplicationConfigBuilder{
key1:value1,
key2:value2,
}
是一种初始化方式,这个builder是另一个问题,是前段时间讨论后针对目前with的config api一个替换做法,我个人理解builder不算是java专有的吧,这个模式go里面也有用,但是java比较普遍,我们也是讨论了觉得这种方式可读性比with更强所以借鉴这种写法。

// 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
55 changes: 55 additions & 0 deletions config/consumer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,58 @@ func WithConsumerConfigCheck(check bool) ConsumerConfigOpt {
return config
}
}

type ConsumerConfigBuilder struct {
consumerConfig *ConsumerConfig
}

func NewConsumerConfigBuilder() *ConsumerConfigBuilder {
return &ConsumerConfigBuilder{consumerConfig: &ConsumerConfig{}}
}

func (ccb *ConsumerConfigBuilder) SetFilter(filter string) *ConsumerConfigBuilder {
ccb.consumerConfig.Filter = filter
return ccb
}

func (ccb *ConsumerConfigBuilder) SetRegistry(registry []string) *ConsumerConfigBuilder {
ccb.consumerConfig.Registry = registry
return ccb
}

func (ccb *ConsumerConfigBuilder) SetRequestTimeout(requestTimeout string) *ConsumerConfigBuilder {
ccb.consumerConfig.RequestTimeout = requestTimeout
return ccb
}

func (ccb *ConsumerConfigBuilder) SetProxyFactory(proxyFactory string) *ConsumerConfigBuilder {
ccb.consumerConfig.ProxyFactory = proxyFactory
return ccb
}

func (ccb *ConsumerConfigBuilder) SetCheck(check bool) *ConsumerConfigBuilder {
ccb.consumerConfig.Check = check
return ccb
}

func (ccb *ConsumerConfigBuilder) SetReferences(references map[string]*ReferenceConfig) *ConsumerConfigBuilder {
ccb.consumerConfig.References = references
return ccb
}

func (ccb *ConsumerConfigBuilder) SetFilterConf(filterConf interface{}) *ConsumerConfigBuilder {
ccb.consumerConfig.FilterConf = filterConf
return ccb
}

func (ccb *ConsumerConfigBuilder) SetRootConfig(rootConfig *RootConfig) *ConsumerConfigBuilder {
ccb.consumerConfig.rootConfig = rootConfig
return ccb
}

func (ccb *ConsumerConfigBuilder) Build() *ConsumerConfig {
if err := ccb.consumerConfig.Init(ccb.consumerConfig.rootConfig); err != nil {
panic(err)
}
return ccb.consumerConfig
}
63 changes: 45 additions & 18 deletions config/dubbo_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/logger"
)

Expand All @@ -37,16 +38,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 All @@ -71,19 +72,45 @@ func (rc *RootConfig) Init() error {
if err := rc.Application.Init(); err != nil {
return err
}
if err := initProtocolsConfig(rc); err != nil {
return err

// init protocol
protocols := rc.Protocols
Mulavar marked this conversation as resolved.
Show resolved Hide resolved
if len(protocols) <= 0 {
protocol := &ProtocolConfig{}
protocols = make(map[string]*ProtocolConfig, 1)
protocols[constant.DUBBO] = protocol
rc.Protocols = protocols
}
if err := initRegistryConfig(rc); err != nil {
return err
for _, protocol := range protocols {
if err := protocol.Init(); err != nil {
return err
}
}
if err := initServiceDiscoveryConfig(rc); err != nil {
return err

// init registry
registries := rc.Registries
if registries != nil {
for _, reg := range registries {
if err := reg.Init(); err != nil {
return err
}
}
}

// init serviceDiscoveries
serviceDiscoveries := rc.ServiceDiscoveries
if serviceDiscoveries != nil {
for _, sd := range serviceDiscoveries {
if err := sd.Init(); 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 {
if err := rc.Metric.Init(); err != nil {
return err
}
if err := initRouterConfig(rc); err != nil {
Expand Down
37 changes: 37 additions & 0 deletions config/graceful_shutdown_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,40 @@ func (config *ShutdownConfig) GetStepTimeout() time.Duration {
}
return result
}

type ShutdownConfigBuilder struct {
shutdownConfig *ShutdownConfig
}

func NewShutDownConfigBuilder() *ShutdownConfigBuilder {
return &ShutdownConfigBuilder{shutdownConfig: &ShutdownConfig{}}
}

func (scb *ShutdownConfigBuilder) SetTimeout(timeout string) *ShutdownConfigBuilder {
scb.shutdownConfig.Timeout = timeout
return scb
}

func (scb *ShutdownConfigBuilder) SetStepTimeout(stepTimeout string) *ShutdownConfigBuilder {
scb.shutdownConfig.StepTimeout = stepTimeout
return scb
}

func (scb *ShutdownConfigBuilder) SetRejectRequestHandler(rejectRequestHandler string) *ShutdownConfigBuilder {
scb.shutdownConfig.RejectRequestHandler = rejectRequestHandler
return scb
}

func (scb *ShutdownConfigBuilder) SetRequestsFinished(requestsFinished bool) *ShutdownConfigBuilder {
scb.shutdownConfig.RequestsFinished = requestsFinished
return scb
}

func (scb *ShutdownConfigBuilder) SetRejectRequest(rejectRequest bool) *ShutdownConfigBuilder {
scb.shutdownConfig.RejectRequest = rejectRequest
return scb
}

func (scb *ShutdownConfigBuilder) Build() *ShutdownConfig {
return scb.shutdownConfig
}
26 changes: 26 additions & 0 deletions config/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,29 @@ func (lc *LoggerConfig) getUrlMap() url.Values {
}
return urlMap
}

type LoggerConfigBuilder struct {
loggerConfig *LoggerConfig
}

// nolint
func NewLoggerConfigBuilder() *LoggerConfigBuilder {
return &LoggerConfigBuilder{loggerConfig: &LoggerConfig{}}
}

// nolint
func (lcb *LoggerConfigBuilder) SetLumberjackConfig(lumberjackConfig *lumberjack.Logger) *LoggerConfigBuilder {
lcb.loggerConfig.LumberjackConfig = lumberjackConfig
return lcb
}

// nolint
func (lcb *LoggerConfigBuilder) SetZapConfig(zapConfig ZapConfig) *LoggerConfigBuilder {
lcb.loggerConfig.ZapConfig = zapConfig
return lcb
}

// nolint
func (lcb *LoggerConfigBuilder) Build() *LoggerConfig {
return lcb.loggerConfig
}
59 changes: 58 additions & 1 deletion config/metadata_report_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) {
common.WithParamsValue(constant.METADATATYPE_KEY, mc.MetadataType),
)
if err != nil || len(res.Protocol) == 0 {
return nil, perrors.New("Invalid MetadataReportConfig.")
return nil, perrors.New("Invalid MetadataReport Config.")
}
res.SetParam("metadata", res.Protocol)
return res, nil
Expand Down Expand Up @@ -122,3 +122,60 @@ func selectMetadataServiceExportedURL() *common.URL {
}
return selectedUrl
}

type MetadataReportConfigBuilder struct {
metadataReportConfig *MetadataReportConfig
}

// nolint
func NewMetadataReportConfigBuilder() *MetadataReportConfigBuilder {
return &MetadataReportConfigBuilder{metadataReportConfig: &MetadataReportConfig{}}
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetProtocol(protocol string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Protocol = protocol
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetAddress(address string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Address = address
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetUsername(username string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Username = username
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetPassword(password string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Password = password
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetTimeout(timeout string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Timeout = timeout
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetGroup(group string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.Group = group
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) SetMetadataType(metadataType string) *MetadataReportConfigBuilder {
mrcb.metadataReportConfig.MetadataType = metadataType
return mrcb
}

// nolint
func (mrcb *MetadataReportConfigBuilder) Build() *MetadataReportConfig {
// TODO Init
return mrcb.metadataReportConfig
}
Loading