diff --git a/common/constant/default.go b/common/constant/default.go index bbe022cb2efebf7dc716f324253743a5a5a0aff5..8afb5c7192300df9191abc163c9a586cac5f65cc 100644 --- a/common/constant/default.go +++ b/common/constant/default.go @@ -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" +) diff --git a/common/logger/logger.go b/common/logger/logger.go index 8519543c6a143c08d34d11dc8513a7294e48773f..655b364fb89e7c0953216070b2cb046a643966af 100644 --- a/common/logger/logger.go +++ b/common/logger/logger.go @@ -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) diff --git a/config/config_loader.go b/config/config_loader.go index 12f57ee7607b328ea069fb343316b43405703e5c..f90705166ac776c77b89e4a33c0cdb0bc3a9d626 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -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 @@ -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