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

Imp: load the configure file in another way #1099

Merged
merged 4 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config

import (
"bytes"
"github.com/apache/dubbo-go/common/yaml"
Copy link
Contributor

Choose a reason for hiding this comment

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

hey, guy, move it to the 3rd import block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's done

"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -52,6 +53,19 @@ type BaseConfig struct {
fileStream *bytes.Buffer
}

func BaseInit(confBaseFile string) error {
if confBaseFile == "" {
return perrors.Errorf("application configure(base) file name is nil")
}
baseConfig = &BaseConfig{}
fileStream, err := yaml.UnmarshalYMLConfig(confBaseFile, baseConfig)
if err != nil {
return perrors.Errorf("unmarshalYmlConfig error %v", perrors.WithStack(err))
}
baseConfig.fileStream = bytes.NewBuffer(fileStream)
return nil
}

// nolint
func (c *BaseConfig) GetServiceDiscoveries(name string) (config *ServiceDiscoveryConfig, ok bool) {
config, ok = c.ServiceDiscoveries[name]
Expand Down
53 changes: 12 additions & 41 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ var (

maxWait = 3
confRouterFile string
confBaseFile string
)

// loaded consumer & provider config from xxx.yml, and log config from xxx.xml
// Namely: dubbo.consumer.xml & dubbo.provider.xml in java dubbo
func init() {
func DefaultInit() []LoaderInitOption {
var (
confConFile string
confProFile string
Expand All @@ -83,30 +84,7 @@ func init() {
if confRouterFile == "" {
confRouterFile = constant.DEFAULT_ROUTER_CONF_FILE_PATH
}

if errCon := ConsumerInit(confConFile); errCon != nil {
log.Printf("[consumerInit] %#v", errCon)
consumerConfig = nil
} else {
// Check if there are some important key fields missing,
// if so, we set a default value for it
setDefaultValue(consumerConfig)
// Even though baseConfig has been initialized, we override it
// because we think read from config file is correct config
baseConfig = &consumerConfig.BaseConfig
}

if errPro := ProviderInit(confProFile); errPro != nil {
log.Printf("[providerInit] %#v", errPro)
providerConfig = nil
} else {
// Check if there are some important key fields missing,
// if so, we set a default value for it
setDefaultValue(providerConfig)
// Even though baseConfig has been initialized, we override it
// because we think read from config file is correct config
baseConfig = &providerConfig.BaseConfig
}
return []LoaderInitOption{RouterInitOption(confRouterFile), BaseInitOption(""), ConsumerInitOption(confConFile), ProviderInitOption(confProFile)}
}

// setDefaultValue set default value for providerConfig or consumerConfig if it is null
Expand Down Expand Up @@ -363,24 +341,17 @@ func initRouter() {

// Load Dubbo Init
func Load() {
options := DefaultInit()
LoadWithOptions(options...)
}

// init router
initRouter()

// init the global event dispatcher
extension.SetAndInitGlobalDispatcher(GetBaseConfig().EventDispatcherType)

// start the metadata report if config set
if err := startMetadataReport(GetApplicationConfig().MetadataType, GetBaseConfig().MetadataReportConfig); err != nil {
logger.Errorf("Provider starts metadata report error, and the error is {%#v}", err)
return
func LoadWithOptions(options ...LoaderInitOption) {
for _, option := range options {
option.init()
}
for _, option := range options {
option.apply()
}

// reference config
loadConsumerConfig()

// service config
loadProviderConfig()

// init the shutdown callback
GracefulShutdownInit()
Expand Down
142 changes: 142 additions & 0 deletions config/config_loader_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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 (
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"log"
)

type LoaderInitOption interface {
init()
apply()
}

type optionFunc struct {
initFunc func()
applyFunc func()
}

func (f *optionFunc) init() {
f.initFunc()
}

func (f *optionFunc) apply() {
f.applyFunc()
}

func ConsumerInitOption(confConFile string) LoaderInitOption {
return consumerInitOption(confConFile, false)
}

func ConsumerMustInitOption(confConFile string) LoaderInitOption {
return consumerInitOption(confConFile, true)
}

func consumerInitOption(confConFile string, must bool) LoaderInitOption {
return &optionFunc{
func() {
if consumerConfig != nil && !must {
return
}
if errCon := ConsumerInit(confConFile); errCon != nil {
log.Printf("[consumerInit] %#v", errCon)
consumerConfig = nil
} else if confBaseFile == "" {
// Check if there are some important key fields missing,
// if so, we set a default value for it
setDefaultValue(consumerConfig)
// Even though baseConfig has been initialized, we override it
// because we think read from config file is correct config
baseConfig = &consumerConfig.BaseConfig
}
},
func() {
loadConsumerConfig()
},
}
}

func ProviderInitOption(confProFile string) LoaderInitOption {
return providerInitOption(confProFile, false)
}

func ProviderMustInitOption(confProFile string) LoaderInitOption {
return providerInitOption(confProFile, true)
}

func providerInitOption(confProFile string, must bool) LoaderInitOption {
return &optionFunc{
func() {
if providerConfig != nil && !must {
return
}
if errPro := ProviderInit(confProFile); errPro != nil {
log.Printf("[providerInit] %#v", errPro)
providerConfig = nil
} else if confBaseFile == "" {
// Check if there are some important key fields missing,
// if so, we set a default value for it
setDefaultValue(providerConfig)
// Even though baseConfig has been initialized, we override it
// because we think read from config file is correct config
baseConfig = &providerConfig.BaseConfig
}
},
func() {
loadProviderConfig()
},
}
}

func RouterInitOption(crf string) LoaderInitOption {
return &optionFunc{
func() {
confRouterFile = crf
},
func() {
initRouter()
},
}
}

func BaseInitOption(cbf string) LoaderInitOption {
return &optionFunc{
func() {
if cbf == "" {
return
}
confBaseFile = cbf
if err := BaseInit(cbf); err != nil {
log.Printf("[BaseInit] %#v", err)
baseConfig = nil
}
},
func() {
// init the global event dispatcher
extension.SetAndInitGlobalDispatcher(GetBaseConfig().EventDispatcherType)

// start the metadata report if config set
if err := startMetadataReport(GetApplicationConfig().MetadataType, GetBaseConfig().MetadataReportConfig); err != nil {
logger.Errorf("Provider starts metadata report error, and the error is {%#v}", err)
return
}
},
}
}