Skip to content

Commit

Permalink
Merge pull request #1073 from cjphaha/develop
Browse files Browse the repository at this point in the history
Ftr: provide a default config
  • Loading branch information
AlexStocks authored Mar 21, 2021
2 parents 8623532 + 89c71ea commit 4c7adfe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ const (
const (
SERVICE_DISCOVERY_DEFAULT_GROUP = "DEFAULT_GROUP"
)

const (
DEFAULT_PROVIDER_CONF_FILE_PATH = "../profiles/dev/server.yml"
DEFAULT_CONSUMER_CONF_FILE_PATH = "../profiles/dev/client.yml"
DEFAULT_LOG_CONF_FILE_PATH = "../profiles/dev/log.yml"
DEFAULT_ROUTER_CONF_FILE_PATH = "../profiles/dev/router.yml"
)
3 changes: 3 additions & 0 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func init() {
for len(fs.Args()) != 0 {
fs.Parse(fs.Args()[1:])
}
if *logConfFile == "" {
*logConfFile = constant.DEFAULT_LOG_CONF_FILE_PATH
}
err := InitLog(*logConfFile)
if err != nil {
log.Printf("[InitLog] warn: %v", err)
Expand Down
50 changes: 50 additions & 0 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,25 @@ func init() {
for len(fs.Args()) != 0 {
fs.Parse(fs.Args()[1:])
}
// If user did not set the environment variables or flags,
// we provide default value
if confConFile == "" {
confConFile = constant.DEFAULT_CONSUMER_CONF_FILE_PATH
}
if confProFile == "" {
confProFile = constant.DEFAULT_PROVIDER_CONF_FILE_PATH
}
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
Expand All @@ -86,12 +100,48 @@ func init() {
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
}
}

// setDefaultValue set default value for providerConfig or consumerConfig if it is null
func setDefaultValue(target interface{}) {
registryConfig := &RegistryConfig{
Protocol: "zookeeper",
TimeoutStr: "3s",
Address: "127.0.0.1:2181",
}
switch target.(type) {
case ProviderConfig:
p := target.(*ProviderConfig)
if len(p.Registries) == 0 {
p.Registries["demoZK"] = registryConfig
}
if len(p.Protocols) == 0 {
p.Protocols["dubbo"] = &ProtocolConfig{
Name: "dubbo",
Port: "20000",
}
}
if p.ApplicationConfig == nil {
p.ApplicationConfig = NewDefaultApplicationConfig()
}
default:
c := target.(*ConsumerConfig)
if len(c.Registries) == 0 {
c.Registries["demoZK"] = registryConfig
}
if c.ApplicationConfig == nil {
c.ApplicationConfig = NewDefaultApplicationConfig()
}
}
}

func checkRegistries(registries map[string]*RegistryConfig, singleRegistry *RegistryConfig) {
if len(registries) == 0 && singleRegistry != nil {
registries[constant.DEFAULT_KEY] = singleRegistry
Expand Down

0 comments on commit 4c7adfe

Please sign in to comment.